1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> #include<iomanip> #include<queue> #include<stack> #include<map> #include<vector> #define gh() getchar() #define re register #define underMax(x,y) ((x)>(y)?(x):(y)) #define underMin(x,y) ((x)<(y)?(x):(y)) #define ls p<<1 #define rs p<<1|1 typedef long long ll; typedef unsigned long long ull; using namespace std; const int MAXN=1e5+1; template<class T> inline void underRead(T &x) { x=0; char ch=gh(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=gh(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh(); if(t) x=-x; } int N,M,u,v,op,Qf,Qs,Rt,Idx,Blo; struct edge { int next,to; }Edge[MAXN<<1]; int Head[MAXN],Total,cnt[MAXN],pos[MAXN],r[MAXN][401]; ull Val[MAXN],Tre[MAXN],sum[MAXN]; struct Segment { int l,r; }Seg[MAXN]; inline void underAddEdge(int u,int v) { Edge[++Total]=(edge){Head[u],v};Head[u]=Total; Edge[++Total]=(edge){Head[v],u};Head[v]=Total; } inline void underDfs(int x,int last) { Seg[x].l=++Idx;++cnt[pos[x]]; for(int i=1;i<=Blo;++i) r[x][i]=cnt[i]; for(int e=Head[x];e;e=Edge[e].next) { int v=Edge[e].to; if(v!=last) underDfs(v,x); } Seg[x].r=Idx;--cnt[pos[x]]; } inline int underLowbit(int x) { return x&(-x); } inline void underAdd(int x,ull val) { for(;x<=N;x+=underLowbit(x)) Tre[x]+=val; } inline ull underQuery(int x) { ull ans=0; for(;x>=1;x-=underLowbit(x)) ans+=Tre[x]; return ans; } inline ull underCalc(int l,int r) { return underQuery(r)-underQuery(l-1); } inline void underInitBlock() { int block=sqrt(N); Blo=(N%block)?(N/block+1):(N/block); for(int i=1;i<=N;++i) pos[i]=(i-1)/Blo+1; } inline void underUpdate(int x,ull k) { for(int i=1;i<=Blo;++i) sum[i]+=(ull)r[x][i]*k; underAdd(Seg[x].l,k); } inline ull underAns(int l,int r) { ull res=0; if(pos[r]-pos[l]<=1) { for(int i=l;i<=r;++i) res+=underCalc(Seg[i].l,Seg[i].r); return res; } for(int i=l;i<=pos[l]*Blo;++i) res+=underCalc(Seg[i].l,Seg[i].r); for(int i=pos[l]+1;i<=pos[r]-1;++i) res+=sum[i]; for(int i=(pos[r]-1)*Blo+1;i<=r;++i) res+=underCalc(Seg[i].l,Seg[i].r); return res; } int main() { freopen("common.in","r",stdin); freopen("common.out","w",stdout); underRead(N),underRead(M); underInitBlock(); for(re int i=1;i<=N;++i) underRead(Val[i]); for(re int i=1;i<=N;++i) { underRead(u),underRead(v); if(!u) Rt=v; else if(!u) Rt=u; else underAddEdge(u,v); } underDfs(Rt,-1); for(int i=1;i<=N;++i) underUpdate(i,Val[i]); while(M--) { underRead(op),underRead(Qf),underRead(Qs); if(op==1) { underUpdate(Qf,Qs-Val[Qf]); Val[Qf]=Qs; } else printf("%llu\n",underAns(Qf,Qs)); } return 0; }
|