标签:
Description
Input
Output
Sample Input
Sample Output
Hint
1.The number of enemies may be negative. 2.Huge input, be careful.
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> #include<string> #include<queue> #include<stack> #include<vector> using namespace std; #define N 50010 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 int sum[N<<2],cal[N<<2]; vector<int> e[N]; int n,m,p; int tim; char s[5]; int num[N],siz[N],top[N],son[N]; int deep[N],tid[N],rnk[N],fa[N]; void init() { memset(son,-1,sizeof(son)); tim=0; for(int i=0;i<=n;i++) e[i].clear(); } void dfs1(int u,int father,int d) { deep[u]=d; fa[u]=father; siz[u]=1; for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(v!=father) { dfs1(v,u,d+1); siz[u]+=siz[v]; if(son[u]==-1||siz[v]>siz[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=0;i<e[u].size();i++) { int v=e[u][i]; if(v!=son[u]&&v!=fa[u]) { dfs2(v,v); } } } void pushdown(int rt,int m) { if(cal[rt]) { cal[rt<<1]+=cal[rt]; cal[rt<<1|1]+=cal[rt]; sum[rt<<1]+=(m-(m>>1))*cal[rt]; sum[rt<<1|1]+=(m>>1)*cal[rt]; cal[rt]=0; } } void build(int l,int r,int rt) { cal[rt]=0; if(l==r) { sum[rt]=num[rnk[l]]; return ; } int mid=(l+r)>>1; build(lson); build(rson); } void update(int L,int R,int c,int l,int r,int rt) { if(L<=l&&r<=R) { cal[rt]+=c; sum[rt]+=(r-l+1)*c; return ; } pushdown(rt,r-l+1); int mid=(l+r)>>1; if(L<=mid) update(L,R,c,lson); if(R>mid) update(L,R,c,rson); } int query(int pos,int l,int r,int rt) { if(l==r) { return sum[rt]; } pushdown(rt,r-l+1); int mid=(l+r)>>1,ret=0; if(pos<=mid) ret=query(pos,lson); else ret=query(pos,rson); return ret; } void change(int x,int y,int val) { while(top[x]!=top[y]) { if(deep[top[x]]<deep[top[y]]) swap(x,y); update(tid[top[x]],tid[x],val,1,n,1); x=fa[top[x]]; } if(deep[x]>deep[y]) swap(x,y); update(tid[x],tid[y],val,1,n,1); } int main() { int a,b,c; while(scanf("%d%d%d",&n,&m,&p)!=EOF) { init(); for(int i=1;i<=n;i++) scanf("%d",&num[i]); for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); e[a].push_back(b); e[b].push_back(a); } dfs1(1,0,1); dfs2(1,1); build(1,n,1); while(p--) { scanf("%s",s); if(s[0]==‘Q‘) { scanf("%d",&a); printf("%d\n",query(tid[a],1,n,1)); } else { scanf("%d%d%d",&a,&b,&c); if(s[0]==‘D‘) c=-c; change(a,b,c); } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4375063.html