@OceanEye7年前
06/4
13:26
简单的树链剖分,分轻重链之后依然满足DFS序的性质所以可以用线段树的区间加来表示子树加法
挂代码
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
#include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <cstdlib> #define ll long long #define INF 1000000000 #define p(x) ('0'<=x&&x<='9') #define For(i,l,r) for(int i=l;i<=r;i++) char cc; int C; template <class T> void read( T &x ) { x=0; cc=getchar(); C=1; while(!p(cc)) { if(cc=='-') C=-1; cc=getchar(); } while(p(cc)) { x=x*10+cc-48; cc=getchar(); } x*=C; } using namespace std; #define N 100010 int val[N],Arr[N],n,m; struct node { int c[2],l,r; ll val,tag; int operator [] (const int &x) { return c[x]; } }pool[N<<2]; #define L pool[x].l #define R pool[x].r #define VAL pool[x].val #define TAG pool[x].tag #define Lc pool[x].c[0] #define Rc pool[x].c[1] #define SIZ (R-L+1) int pcnt=1; inline void PushUP(int x) { VAL=pool[Lc].val+pool[Rc].val+TAG*SIZ; } void build(int x,int l,int r) { L=l; R=r; if(l==r) { TAG=Arr[l]; PushUP(x); return; } int mid=(l+r)>>1; build(Lc=++pcnt,l,mid); build(Rc=++pcnt,mid+1,r); PushUP(x); } void add(int x,int l,int r,int val) { if(l<=L&&R<=r) { TAG+=val; PushUP(x); return; } int mid=(L+R)>>1; if(mid>=l) add(Lc,l,r,val); if(mid <r) add(Rc,l,r,val); PushUP(x); } void add(int x,int pos,int val) { if(L==R) { TAG+=val; PushUP(x); return; } if(pos>((L+R)>>1)) add(Rc,pos,val); else add(Lc,pos,val); PushUP(x); } ll query(int x,int l,int r) { if(l<=L&&R<=r) return VAL; int mid=(L+R)>>1; ll ret=0; if(l<=mid) ret+=query(Lc,l,r); if(r >mid) ret+=query(Rc,l,r); ret+=1LL*TAG*(min(R,r)-max(L,l)+1); return ret; } int mxson[N],siz[N],up[N],fa[N],id[N],l[N],r[N],tmp; vector <int> G[N]; #define TO G[x][i] void dfs(int x,int f) { int node; siz[x]=1; for(int i=0;i<G[x].size();i++) { node=TO; if(node==f) continue; dfs(node,x); siz[x]+=siz[node]; if(siz[node]>siz[mxson[x]]) mxson[x]=node; } } void ddfs(int x,int f) { l[x]=id[x]=++tmp; int node=mxson[x]; if(!node) { r[x]=tmp; return; } fa[node]=fa[x]; up[node]=up[x]; ddfs(mxson[x],x); for(int i=0;i<G[x].size();i++) { node=TO; if(node==f||node==mxson[x]) continue; fa[node]=node; up[node]=x; ddfs(node,x); } r[x]=tmp; } void init() { int _,__; read(n); read(m); For(i,1,n) read(val[i]); For(i,1,n-1) { read(_); read(__); G[_].push_back(__); G[__].push_back(_); } dfs(1,0); fa[1]=1; up[1]=0; ddfs(1,0); For(i,1,n) Arr[id[i]]=val[i]; build(1,1,n); } int main() { freopen("BZOJ4034.in","r",stdin); int a,x,val; ll ans; init(); while(m--) { read(a); read(x); if(a==1) { read(val); add(1,id[x],val); } if(a==2) { read(val); add(1,l[x],r[x],val); } if(a==3) { ans=0; while(x) { ans+=query(1,id[fa[x]],id[x]); x=up[x]; } printf("%lld\n",ans); } } return 0; } |