标签:astar hang 一个 while seve 返回值 tput pid chm
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3593 Accepted Submission(s): 835
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 100010 using namespace std; struct nond{ int l,r; long long dis,falg; }tree[MAXN*8]; int T,n,m,sz,tot,num; long long val[MAXN*2]; int idl[MAXN*2],idr[MAXN*2],dad[MAXN*2]; int to[MAXN*2],net[MAXN*2],head[MAXN*2]; void add(int u,int v){ to[++tot]=v;net[tot]=head[u];head[u]=tot; to[++tot]=u;net[tot]=head[v];head[v]=tot; } void up(int now){ tree[now].dis=max(tree[now*2].dis,tree[now*2+1].dis); } void build(int now,int l,int r){ tree[now].l=l;tree[now].r=r; if(tree[now].l==tree[now].r) return ; int mid=(tree[now].l+tree[now].r)/2; build(now*2,l,mid); build(now*2+1,mid+1,r); } void down(int now){ tree[now*2].falg+=tree[now].falg; tree[now*2+1].falg+=tree[now].falg; tree[now*2].dis+=tree[now].falg; tree[now*2+1].dis+=tree[now].falg; tree[now].falg=0; } void change(int now,int l,int r,long long k){ if(tree[now].l==l&&tree[now].r==r){ tree[now].dis+=k; tree[now].falg+=k; return ; } if(tree[now].falg) down(now); int mid=(tree[now].l+tree[now].r)/2; if(r<=mid) change(now*2,l,r,k); else if(l>mid) change(now*2+1,l,r,k); else{ change(now*2,l,mid,k); change(now*2+1,mid+1,r,k); } up(now); } long long query(int now,int l,int r){ if(tree[now].l==l&&tree[now].r==r) return tree[now].dis; if(tree[now].falg) down(now); int mid=(tree[now].l+tree[now].r)/2; if(r<=mid) return query(now*2,l,r); else if(l>mid) return query(now*2+1,l,r); else return max(query(now*2,l,mid),query(now*2+1,mid+1,r)); } void dfs(int now){ idl[now]=++sz; for(int i=head[now];i;i=net[i]) if(to[i]!=dad[now]){ dad[to[i]]=now; dfs(to[i]); } idr[now]=++sz; } int main(){ scanf("%d",&T); while(T--){ sz=0;tot=0;num++; memset(val,0,sizeof(val)); memset(dad,0,sizeof(dad)); memset(idl,0,sizeof(idl)); memset(idr,0,sizeof(idr)); memset(head,0,sizeof(head)); memset(tree,0,sizeof(tree)); cout<<"Case #"<<num<<":"<<endl; scanf("%d%d",&n,&m); for(int i=1;i<n;i++){ int x,y; scanf("%d%d",&x,&y); add(x,y); } dfs(0); build(1,1,sz); for(int i=0;i<n;i++){ scanf("%lld",&val[i]); change(1,idl[i],idr[i],val[i]); } for(int i=1;i<=m;i++){ int opt,x;long long y; scanf("%d%d",&opt,&x); if(opt==0){ scanf("%lld",&y); change(1,idl[x],idr[x],y-val[x]); val[x]=y; } else if(opt==1) cout<<query(1,idl[x],idr[x])<<endl; } } }
标签:astar hang 一个 while seve 返回值 tput pid chm
原文地址:http://www.cnblogs.com/cangT-Tlan/p/7593735.html