标签:
树刨 一定要注意long long
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<queue> 7 #include<algorithm> 8 #include<vector> 9 #define M 2000009 10 #define EPS 1e-10 11 #define MO 10000 12 #define ll long long 13 using namespace std; 14 ll read() 15 { 16 char ch=getchar(); 17 ll x=0,f=1; 18 for(;ch<‘0‘||ch>‘9‘;ch=getchar()) 19 if(ch==‘-‘) 20 f=-1; 21 for(;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) 22 x=x*10+ch-‘0‘; 23 return x*f; 24 } 25 int cnt,head[M],next[M],u[M],n,m,a[M],R[M],size[M],lian[M],dui[M],T,b[M],fa[M]; 26 struct data 27 { 28 ll he,lazy; 29 }shu[M]; 30 void jiaa(int a1,int a2) 31 { 32 cnt++; 33 next[cnt]=head[a1]; 34 head[a1]=cnt; 35 u[cnt]=a2; 36 } 37 void dfs(int x) 38 { 39 size[x]=1; 40 for(int i=head[x];i;i=next[i]) 41 if(u[i]!=fa[x]) 42 { 43 44 fa[u[i]]=x; 45 dfs(u[i]); 46 size[x]+=size[u[i]]; 47 } 48 } 49 void dfs1(int x,int gen) 50 { 51 R[x]=dui[x]=++T; 52 b[dui[x]]=a[x]; 53 lian[x]=gen; 54 int k=0; 55 for(int i=head[x];i;i=next[i]) 56 if(u[i]!=fa[x]&&size[u[i]]>size[k]) 57 k=u[i]; 58 if(!k) 59 return; 60 dfs1(k,gen); 61 for(int i=head[x];i;i=next[i]) 62 if(u[i]!=fa[x]&&u[i]!=k) 63 dfs1(u[i],u[i]); 64 R[x]=T; 65 } 66 void xia(int x,int l,int r) 67 { 68 int mid=(l+r)>>1; 69 ll q=shu[x].lazy; 70 shu[x].lazy=0; 71 shu[x*2].he+=(ll)q*(mid-l+1); 72 shu[x*2+1].he+=(ll)q*(r-mid); 73 shu[x*2].lazy+=q; 74 shu[x*2+1].lazy+=q; 75 } 76 void jia(int x,int l,int r,int L,int R,int q) 77 { 78 if(l>=L&&r<=R) 79 { 80 shu[x].he+=(ll)q*(r-l+1); 81 shu[x].lazy+=q; 82 return; 83 } 84 int mid=(l+r)>>1; 85 if(shu[x].lazy) 86 xia(x,l,r); 87 if(L<=mid) 88 jia(x*2,l,mid,L,R,q); 89 if(R>mid) 90 jia(x*2+1,mid+1,r,L,R,q); 91 shu[x].he=shu[x*2].he+shu[x*2+1].he; 92 } 93 ll xxun(int x,int l,int r,int L,int R) 94 { 95 if(l>=L&&r<=R) 96 return shu[x].he; 97 int mid=(l+r)>>1; 98 ll sum=0; 99 if(shu[x].lazy) 100 xia(x,l,r); 101 if(L<=mid) 102 sum+=xxun(x*2,l,mid,L,R); 103 if(R>mid) 104 sum+=xxun(x*2+1,mid+1,r,L,R); 105 shu[x].he=shu[x*2].he+shu[x*2+1].he; 106 return sum; 107 } 108 ll xun(int x) 109 { 110 ll sum=0; 111 for(;x;x=fa[lian[x]]) 112 sum+=xxun(1,1,n,dui[lian[x]],dui[x]); 113 return sum; 114 } 115 int main() 116 { 117 n=read(); 118 m=read(); 119 for(int i=1;i<=n;i++) 120 a[i]=read(); 121 for(int i=1;i<n;i++) 122 { 123 int a1=read(),a2=read(); 124 jiaa(a1,a2); 125 jiaa(a2,a1); 126 } 127 dfs(1); 128 dfs1(1,1); 129 for(int i=1;i<=n;i++) 130 jia(1,1,n,dui[i],dui[i],a[i]); 131 for(int i=1;i<=m;i++) 132 { 133 int a1=read(),x=read(),a; 134 if(a1!=3) 135 a=read(); 136 if(a1==1) 137 jia(1,1,n,dui[x],dui[x],a); 138 if(a1==2) 139 jia(1,1,n,dui[x],R[x],a); 140 if(a1==3) 141 printf("%lld\n",xun(x)); 142 } 143 return 0; 144 }
标签:
原文地址:http://www.cnblogs.com/xiw5/p/5654687.html