标签:contains log out sea eof ber for proc ade
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10510 Accepted Submission(s):
2766
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxn 50001 using namespace std; struct TreeNodeType { int l,r,dis,mid,flag; void clear() { l=0,r=0,dis=0,mid=0,flag=0; } }; struct TreeNodeType tree[maxn<<2]; struct EdgeType { int to,next; }; struct EdgeType edge[maxn<<1]; int if_z,n,m,q,cnt,tot,Enum,deep[maxn],size[maxn],belong[maxn]; int flag[maxn],head[maxn],dis[maxn],dis_[maxn],f[maxn]; char Cget; inline void read_int(int &now) { now=0,if_z=1,Cget=getchar(); while(Cget<‘0‘||Cget>‘9‘) { if(Cget==‘-‘) if_z=-1; Cget=getchar(); } while(Cget>=‘0‘&&Cget<=‘9‘) { now=now*10+Cget-‘0‘; Cget=getchar(); } now*=if_z; } inline void edge_add(int from,int to) { edge[++Enum].to=from,edge[Enum].next=head[to],head[to]=Enum; edge[++Enum].to=to,edge[Enum].next=head[from],head[from]=Enum; } void search(int now,int fa) { int pos=tot++; deep[now]=deep[fa]+1,f[now]=fa; for(int i=head[now];i;i=edge[i].next) { if(edge[i].to==fa) continue; search(edge[i].to,now); } size[now]=tot-pos; } void search_(int now,int chain) { int pos=0; flag[now]=++tot,dis_[flag[now]]=dis[now]; belong[now]=chain; for(int i=head[now];i;i=edge[i].next) { if(flag[edge[i].to]) continue; if(size[edge[i].to]>size[pos]) pos=edge[i].to; } if(pos!=0) search_(pos,chain); else return ; for(int i=head[now];i;i=edge[i].next) { if(flag[edge[i].to]) continue; search_(edge[i].to,edge[i].to); } } inline void tree_up(int now) { tree[now].dis=tree[now<<1].dis+tree[now<<1|1].dis; } inline void tree_down(int now) { if(tree[now].l==tree[now].r) return ; tree[now<<1].dis+=(tree[now<<1].r-tree[now<<1].l+1)*tree[now].flag; tree[now<<1].flag+=tree[now].flag; tree[now<<1|1].dis+=(tree[now<<1|1].r-tree[now<<1|1].l+1)*tree[now].flag; tree[now<<1|1].flag+=tree[now].flag; tree[now].flag=0; } void tree_build(int now,int l,int r) { tree[now].clear(); tree[now].l=l,tree[now].r=r; if(l==r) { tree[now].dis=dis_[++tot]; return ; } tree[now].mid=(l+r)>>1; tree_build(now<<1,l,tree[now].mid); tree_build(now<<1|1,tree[now].mid+1,r); tree_up(now); } void tree_change(int now,int l,int r,int x) { if(tree[now].l==l&&tree[now].r==r) { tree[now].dis+=(r-l+1)*x; tree[now].flag+=x; return ; } if(tree[now].flag) tree_down(now); if(l>tree[now].mid) tree_change(now<<1|1,l,r,x); else if(r<=tree[now].mid) tree_change(now<<1,l,r,x); else { tree_change(now<<1,l,tree[now].mid,x); tree_change(now<<1|1,tree[now].mid+1,r,x); } tree_up(now); } int tree_query(int now,int to) { if(tree[now].l==tree[now].r&&tree[now].l==to) { return tree[now].dis; } if(tree[now].flag) tree_down(now); tree_up(now); if(to>tree[now].mid) return tree_query(now<<1|1,to); else return tree_query(now<<1,to); } inline void solve_change(int x,int y,int z) { while(belong[x]!=belong[y]) { if(deep[belong[x]]<deep[belong[y]]) swap(x,y); tree_change(1,flag[belong[x]],flag[x],z); x=f[belong[x]]; } if(deep[x]<deep[y]) swap(x,y); tree_change(1,flag[y],flag[x],z); } int main() { while(scanf("%d%d%d",&n,&m,&q)==3) { memset(head,0,sizeof(head)); memset(size,0,sizeof(size)); memset(flag,0,sizeof(flag)); tot=0,cnt=0,Enum=0; for(int i=1;i<=n;i++) read_int(dis[i]); int from,to,cur; for(int i=1;i<=m;i++) { read_int(from),read_int(to); edge_add(from,to); } search(1,0),tot=0,search_(1,1); tot=0,tree_build(1,1,n); char type; for(int i=1;i<=q;i++) { cin>>type; if(type==‘I‘) { read_int(from),read_int(to),read_int(cur); solve_change(from,to,cur); } if(type==‘D‘) { read_int(from),read_int(to),read_int(cur); solve_change(from,to,-cur); } if(type==‘Q‘) { read_int(from); printf("%d\n",tree_query(1,flag[from])); } } } return 0; }
AC日记——Aragorn's Story HDU 3966
标签:contains log out sea eof ber for proc ade
原文地址:http://www.cnblogs.com/IUUUUUUUskyyy/p/6363585.html