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
| #include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <map> #include <queue> #include <cstring> #include <cstdio> using namespace std; using ll=long long; using pii=pair<int,int>; const int MAXN=50010;
vector<int> g[MAXN]; int vlen,elen,qlen;
int tick=0; int fa[MAXN],sz[MAXN],dep[MAXN]; int son[MAXN],top[MAXN]; int dfn[MAXN],rnk[MAXN];
void dfs1(int u,int f){ fa[u]=f; dep[u]=dep[f]+1; sz[u]=1; son[u]=0; for(auto v:g[u]){ if(v==f)continue; dfs1(v,u); sz[u]+=sz[v]; if(sz[v]>sz[son[u]]){ son[u]=v; } } }
void dfs2(int u,int f){ top[u]=f; dfn[u]=++tick; rnk[tick]=u; if(son[u]==0)return; dfs2(son[u],f); for(auto v:g[u]){ if(v!=son[u] && v!=fa[u])dfs2(v,v); } } const int FTN=4*(MAXN+10); int ft[FTN]; int lowbit(int x){ return x&-x; } void ftadd(int pos,int x){ if(pos<=0)return; for(;pos<FTN;pos+=lowbit(pos))ft[pos]+=x; } int ftget(int pos){ if(pos<=0)return 0; int res=0; for(;pos;pos-=lowbit(pos))res+=ft[pos]; return res; }
int query(int pos){ return ftget(dfn[pos]); }
void modify(int l,int r,int x){ ftadd(l,x); ftadd(r+1,-x); }
void solve(int x,int y,int delta){ int fx=top[x],fy=top[y]; while(fx!=fy){ if(dep[fx]>dep[fy])modify(dfn[fx],dfn[x],delta),x=fa[fx]; else modify(dfn[fy],dfn[y],delta),y=fa[fy];
fx=top[x]; fy=top[y]; } if(x!=y){ if(dfn[x]<dfn[y])modify(dfn[x],dfn[y],delta); else modify(dfn[y],dfn[x],delta); }else modify(dfn[x],dfn[y],delta); }
int num[MAXN];
int main(){
while(~scanf("%d%d%d",&vlen,&elen,&qlen)){ for(int i=0;i<=vlen;i++)g[i].clear(); memset(ft,0,sizeof(ft)); memset(fa,0,sizeof(fa)); memset(sz,0,sizeof(sz)); memset(dep,0,sizeof(dep)); memset(son,0,sizeof(son)); memset(top,0,sizeof(top)); memset(dfn,0,sizeof(dfn)); memset(rnk,0,sizeof(rnk)); tick=0;
for(int i=1;i<=vlen;i++){ scanf("%d",num+i); } for(int i=0;i<elen;i++){ int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } dfs1(1,0); dfs2(1,1);
for(int i=1;i<=vlen;i++){ modify(dfn[i],dfn[i],num[i]); }
while(qlen--){ char opt[10]; scanf("%s",opt); if(opt[0]=='I' || opt[0]=='D'){ int l,r,x; scanf("%d%d%d",&l,&r,&x); if(opt[0]=='D')x=-x; solve(l,r,x); }else{ int u; scanf("%d",&u); printf("%d\n",query(u)); } } }
return 0; }
|