日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tree HDU - 6547 (树链剖分,线段树)

發(fā)布時間:2023/12/18 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tree HDU - 6547 (树链剖分,线段树) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

wls 有三棵樹,樹上每個節(jié)點都有一個值 ai,現(xiàn)在有 2 種操作:

  • 將一條鏈上的所有節(jié)點的值開根號向下取整;
  • 求一條鏈上值的和;
    鏈的定義是兩點之間的最短路。
    Input
    第一行兩個數(shù) n, q 分別代表樹上點的數(shù)量和操作數(shù)量。
    第二行 n 個整數(shù),第 i 個數(shù)代表第 i 個點的值 ai。
    接下來 n ? 1 行, 每行兩個整數(shù) u, v 代表 u,v 之間有一條邊。數(shù)據(jù)保證點兩兩聯(lián)通。
    接下來 q 行,每行有個整數(shù) op, u, v,op = 0 表示將 u, v 這條鏈上所有的點的值開根號向下取整,op = 1表示詢問 u,v 這條鏈上的值的和。
    1 ≤ n, q ≤ 100, 000
    0 ≤ ai ≤ 1, 000, 000, 000
    Output
    對于每一組 op = 2 的詢問,輸出一行一個值表示答案。
    Sample Input
    4 4
    2 3 4 5
    1 2
    2 3
    2 4
    0 3 4
    0 1 3
    1 2 3
    1 1 4
    Sample Output
    2
    4
  • 思路:

    樹鏈剖分裸題,

    如何處理區(qū)間 開方,區(qū)間求和,可以看這個博客:

    https://www.cnblogs.com/qieqiemin/p/11306562.html

    細節(jié)見代碼:

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int* p); const int maxn = 100010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int n, m; int root; ll a[maxn];// 初始點權(quán) ll wt[maxn];// 新建編號點權(quán)。 int cnt;// 編號用的變量 int top[maxn];// 所在重鏈的頂點編號 int id[maxn];//節(jié)點的新編號。 std::vector<int> son[maxn]; int SZ[maxn];// 子數(shù)大小 int wson[maxn];// 重兒子 int fa[maxn];// 父節(jié)點 int dep[maxn];// 節(jié)點的深度void dfs1(int id, int pre, int step) // 維護出sz,wson,fa,dep {dep[id] = step;fa[id] = pre;SZ[id] = 1;int maxson = -1;for (auto x : son[id]){if (x != pre){dfs1(x, id, step + 1);SZ[id] += SZ[x];if (SZ[x] > maxson){maxson = SZ[x];wson[id] = x;}}}}//處理出top[],wt[],id[] void dfs2(int u,int topf) {id[u] = ++cnt;wt[cnt]=a[u];top[u]=topf;if(!wson[u]) // 沒兒子時直接結(jié)束{return ;}dfs2(wson[u],topf);// 先處理重兒子for(auto x:son[u]){if(x==wson[u]||x==fa[u])//只處理輕兒子{continue;}dfs2(x,x);// 每個輕兒子以自己為top} }struct node {int l,r;ll sum;ll laze; }segment_tree[maxn<<2];void pushup(int rt) {segment_tree[rt].sum=(segment_tree[rt<<1].sum+segment_tree[rt<<1|1].sum); } void build(int rt,int l,int r) {segment_tree[rt].l=l;segment_tree[rt].r=r;segment_tree[rt].laze=0;if(l==r){segment_tree[rt].sum=wt[l];return;}int mid=(l+r)>>1;build(rt<<1,l,mid);build(rt<<1|1,mid+1,r);pushup(rt); }void update(int rt,int l,int r) {if((segment_tree[rt].l>=l&&segment_tree[rt].r<=r)&&segment_tree[rt].sum==(segment_tree[rt].r-segment_tree[rt].l+1)){return ;}if(segment_tree[rt].l==segment_tree[rt].r){segment_tree[rt].sum=sqrt(segment_tree[rt].sum);return ;}int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;if(mid>=l){update(rt<<1,l,r);}if(mid<r){update(rt<<1|1,l,r);}pushup(rt); } ll query(int rt,int l,int r) {if(segment_tree[rt].l>=l&&segment_tree[rt].r<=r){ll res=0ll;res+=segment_tree[rt].sum;return res;}int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;ll res=0ll;if(mid>=l){res+=query(rt<<1,l,r);}if(mid<r){res+=query(rt<<1|1,l,r);}return res;}void uprange(int x,int y) {while(top[x]!=top[y]){if(dep[top[x]]<dep[top[y]])// 使x的top深度較大{swap(x,y);}update(1,id[top[x]],id[x]);// 處理x到top[x] 那段鏈x=fa[top[x]];// x向上爬到top[x]的父節(jié)點}if(dep[x]>dep[y])//使x的深度較小swap(x,y);update(1,id[x],id[y]);//更新x到y(tǒng)這段鏈,根據(jù)上面的處理,他們一定是在同一條鏈上 }ll qrange(int x,int y) {ll ans=0ll;while(top[x]!=top[y]){if(dep[top[x]]<dep[top[y]]){swap(x,y);}ans+=query(1,id[top[x]],id[x]);x=fa[top[x]];}if(dep[x]>dep[y])swap(x,y);ans+=query(1,id[x],id[y]);return ans; } void upson(int x,ll val) {update(1,id[x],id[x]+SZ[x]-1);//子樹區(qū)間右端點為id[x]+siz[x]-1 } ll qson(int x) {ll res=0ll;res+=query(1,id[x],id[x]+SZ[x]-1);return res; } int main() { // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); // freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);gbtb;cin >> n >> m ;;repd(i, 1, n){cin >> a[i];}int u, v;repd(i, 2, n){cin >> u >> v;son[u].pb(v);son[v].pb(u);}root=1;dfs1(root,0,1);dfs2(root,root);build(1,1,n);int op,x,y,z; // 操作1: 格式: 1 x y z 表示將樹從x到y(tǒng)結(jié)點最短路徑上所有節(jié)點的值都加上z // // 操作2: 格式: 2 x y 表示求樹從x到y(tǒng)結(jié)點最短路徑上所有節(jié)點的值之和 // // 操作3: 格式: 3 x z 表示將以x為根節(jié)點的子樹內(nèi)所有節(jié)點值都加上z // // 操作4: 格式: 4 x 表示求以x為根節(jié)點的子樹內(nèi)所有節(jié)點值之和while(m--){cin>>op;if(op==0){cin>>x>>y;uprange(x,y);}else if(op==1){cin>>x>>y;cout<<qrange(x,y)<<endl;}}return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }

    轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11306597.html

    總結(jié)

    以上是生活随笔為你收集整理的Tree HDU - 6547 (树链剖分,线段树)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。