标签:des style blog http color java os io
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2393 Accepted Submission(s): 641
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <cstring> using namespace std; #define lson rt<<1 #define rson rt<<1|1 typedef long long LL; const int N = 50005; int n,m,q; int tim; int num[N],size[N],top[N],son[N]; int dep[N],tid[N],rnk[N],fa[N]; int eh[N],to[2*N],nxt[2*N],edge; void addedge(int u,int v) { to[edge]=v;nxt[edge]=eh[u];eh[u]=edge++; to[edge]=u;nxt[edge]=eh[v];eh[v]=edge++; } void dfs1(int u,int father,int d) { dep[u]=d; fa[u]=father; size[u]=1; for(int i = eh[u] ; ~i ;i = nxt[i] ) { int v=to[i]; if( v != father ){ dfs1( v, u ,d+1 ); size[u] += size[v]; if( son[u] == -1 || size[v] >size[son[u]] ) son[u]=v; } } } void dfs2(int u,int tp) { top[u]=tp; tid[u] = ++tim; rnk[ tid[u] ]=u; if( son[u] == -1 )return ; dfs2( son[u],tp); for(int i=eh[u];~i;i=nxt[i]) { int v=to[i]; if( v != son[u] && v != fa[u] ) dfs2(v,v); } } LL c[N]; int lowbit(int x){ return x&-x; } void add(int pos,int key) { while(pos <= n + 3) { c[pos] += key; pos += lowbit(pos); } } void update(int u,int v,int val) { int f1=top[u],f2=top[v]; while(f1 != f2){ if(dep[f1] < dep[f2]){ swap(f1,f2); swap(u,v); } add(tid[f1],val); add(tid[u]+1,-val); u=fa[f1]; f1=top[u]; } if( dep[u] > dep[v] )swap(u,v); add(tid[u],val); add(tid[v]+1,-val); } LL query(int pos){ LL res=0; while(pos>0){ res += c[pos]; pos -= lowbit(pos); } return res; } void init() { memset(c,0,sizeof(c)); memset(eh,-1,sizeof(eh)); memset(son,-1,sizeof(son)); tim=1; //树状数组坐标从1开始!! edge=0; } int main(void) { char op[5]; int u,v,c; freopen("in.txt","r",stdin); while(~scanf("%d%d%d",&n,&m,&q)) { init(); for(int i=1;i<=n;++i) scanf("%d",&num[i]); for(int i=1;i<=m;++i){ scanf("%d%d",&u,&v); addedge(u,v); } dfs1(1,0,0); dfs2(1,1); for(int i=1;i<=n;++i){ add(tid[i],num[i]); add(tid[i]+1,-num[i]); } while(q--){ scanf("%s",op); if( op[0] == ‘Q‘){ scanf("%d",&u); printf("%lld\n",query(tid[u])); continue; } scanf("%d%d%d",&u,&v,&c); if(op[0]==‘D‘)c=-c; update(u,v,c); } } return 0; }
HDU 3966 Aragorn's Story,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/YRETSIM/p/3917082.html