[HAOI2015]T2
【題目描述】
有一棵點數(shù)為N的樹,以點1為根,且樹點有邊權(quán)。然后有M個操作,分為三種:
操作1:把某個節(jié)點x的點權(quán)增加a。
操作2:把某個節(jié)點x為根的子樹中所有點的點權(quán)都增加a。
操作3:詢問某個節(jié)點x到根的路徑中所有點的點權(quán)和。
【輸入格式】
第一行兩個整數(shù)N,M,表示點數(shù)和操作數(shù)。
接下來一行N個整數(shù),表示樹中節(jié)點的初始權(quán)值。
接下來N-1行每行兩個正整數(shù)fr,to,表示該樹中存在一條邊(fr,to)。
再接下來M行,每行分別表示一次操作。其中第一個數(shù)表示該操作的種類(1~3),之后接這個操作的參數(shù)(x或者x a)。
【輸出格式】
對于每個詢問操作,輸出該詢問的答案。答案之間用換行隔開。
【樣例輸入】
5 5
1 2 3 4 5
1 2
1 4
2 3
2 5
3 3
1 2 1
3 5
2 1 2
3 3
【樣例輸出】
6
9
13
【提示】
對于30%的數(shù)據(jù),N,M<=1000
對于50%的數(shù)據(jù),N,M<=100000且數(shù)據(jù)隨機。
對于100%的數(shù)據(jù),N,M<=100000,且所有輸入數(shù)據(jù)的絕對值都不會超過10^6。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 #include<vector> 8 #include<queue> 9 using namespace std; 10 typedef long long LL; 11 const LL maxn=100010; 12 inline LL read(){ 13 LL x=0,f=1;char ch=getchar(); 14 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 15 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 16 return x*f; 17 } 18 vector<LL> to[maxn]; 19 LL N,M; 20 LL a[maxn]; 21 LL dep[maxn],fa[maxn],son[maxn],top[maxn],siz[maxn],id[maxn]; 22 LL val[maxn]; 23 LL num; 24 inline void dfs1(LL rt,LL fath,LL deep){ 25 dep[rt]=deep; siz[rt]=1; fa[rt]=fath; 26 for(LL i=0;i<to[rt].size();i++){ 27 LL y=to[rt][i]; 28 if(y!=fa[rt]){ 29 dfs1(y,rt,deep+1); 30 siz[rt]+=siz[y]; 31 if(siz[son[rt]]<siz[y]) son[rt]=y; 32 } 33 } 34 } 35 inline void dfs2(LL rt,LL tp){ 36 top[rt]=tp; id[rt]=++num; 37 if(son[rt]!=0) dfs2(son[rt],tp); 38 for(LL i=0;i<to[rt].size();i++){ 39 LL y=to[rt][i]; 40 if(y!=fa[rt]&&y!=son[rt]){ 41 dfs2(y,y); 42 } 43 } 44 } 45 struct node{ 46 LL l,r; 47 LL sum,lazy; 48 }tree[maxn*8]; 49 inline void build(LL rt,LL l,LL r){ 50 tree[rt].l=l; tree[rt].r=r; 51 if(l==r){ 52 tree[rt].sum=val[l]; 53 tree[rt].lazy=0; 54 return ; 55 } 56 LL mid=(l+r)>>1; 57 build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); 58 tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; 59 } 60 inline void update_son(LL rt){ 61 LL d=tree[rt].lazy; 62 if(d!=0){ 63 tree[rt<<1].sum+=(tree[rt<<1].r-tree[rt<<1].l+1)*d; 64 tree[rt<<1|1].sum+=(tree[rt<<1|1].r-tree[rt<<1|1].l+1)*d; 65 tree[rt<<1].lazy+=d; tree[rt<<1|1].lazy+=d; 66 tree[rt].lazy=0; 67 } 68 } 69 inline void change(LL rt,LL l,LL r,LL delta){ 70 if(l<=tree[rt].l&&tree[rt].r<=r){ 71 tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*delta; 72 tree[rt].lazy+=delta; 73 return ; 74 } 75 update_son(rt); 76 LL mid=(tree[rt].l+tree[rt].r)>>1; 77 if(l<=mid) change(rt<<1,l,r,delta); 78 if(mid+1<=r) change(rt<<1|1,l,r,delta); 79 tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; 80 } 81 inline LL query(LL rt,LL l,LL r){ 82 if(l<=tree[rt].l&&tree[rt].r<=r){ 83 return tree[rt].sum; 84 } 85 update_son(rt); 86 LL ans=0; 87 LL mid=(tree[rt].l+tree[rt].r)>>1; 88 if(l<=mid) ans+=query(rt<<1,l,r); 89 if(mid+1<=r) ans+=query(rt<<1|1,l,r); 90 return ans; 91 } 92 inline void ASK(LL x){ 93 LL tp=top[x],ans=0; 94 while(x!=0&&tp!=0){ 95 ans+=query(1,id[tp],id[x]); 96 x=fa[tp]; tp=top[x]; 97 } 98 printf("%lld\n",ans); 99 } 100 int main(){ 101 N=read(); M=read(); 102 for(LL i=1;i<=N;i++) a[i]=read(); 103 for(LL i=1,u,v;i<=N-1;i++){ 104 u=read(); v=read(); 105 to[u].push_back(v); to[v].push_back(u); 106 } 107 dis[1]=a[1]; 108 dfs1(1,0,1); dfs2(1,1); 109 for(LL i=1;i<=N;i++) val[id[i]]=a[i]; 110 build(1,1,num); 111 for(LL i=1,kin;i<=M;i++){ 112 kin=read(); 113 if(kin==1){ 114 LL x=read(),d=read(); 115 change(1,id[x],id[x],d); 116 } 117 else if(kin==2){ 118 LL x=read(),d=read(); 119 change(1,id[x],id[x]+siz[x]-1,d); 120 } 121 else if(kin==3){ 122 LL x=read(); 123 ASK(x); 124 } 125 } 126 return 0; 127 }?
轉(zhuǎn)載于:https://www.cnblogs.com/CXCXCXC/p/5022114.html
總結(jié)
以上是生活随笔為你收集整理的[HAOI2015]T2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《FPGA入门教程》看书随笔——RTL设
- 下一篇: Creating Apps With M