求大佬找错 RE0分

P3178 [HAOI2015] 树上操作

找到RE的原因了,现在全WA ```cpp #include<bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; inline int read() { char ch; int fl=1; int xx=0; do{ ch= getchar(); if(ch=='-') fl=-1; }while(ch<'0'||ch>'9'); do{ xx=(xx<<3)+(xx<<1)+ch-'0'; ch=getchar(); }while(ch>='0'&&ch<='9'); return xx*fl; } int n,m; int a[MAXN]; struct node { int to; int next; };node g[MAXN*2]; int fa[MAXN],head[MAXN],dep[MAXN],tot[MAXN],son[MAXN],cnt=0; inline void addedge(int x,int y){ ++cnt;g[cnt].to=y;g[cnt].next=head[x];head[x]=cnt;return; } inline void dfs(int x) { tot[x]=1,son[x]=0; for(int i=head[x];i>0;i=g[i].next) { int v=g[i].to; if(v!=fa[x]) { dep[v]=dep[x]+1; fa[v]=x; dfs(v); if(tot[v]>tot[son[x]]) son[x]=v; tot[x]+=tot[v]; } } return; } int num[MAXN],top[MAXN],z=0; inline void dfs2(int now,int t) { top[now]=t;++z;num[now]=z; if(son[now]) dfs2(son[now],t); for(int i=head[now];i>0;i=g[i].next) { int v=g[i].to; if(v!=son[now]&&v!=fa[now]) dfs2(v,v); } } struct TREE { int l; int r; long long sum; inline int mid(){ return (l+r)>>1; } }tree[MAXN*4]; long long add[MAXN*4]; #define lc o<<1 #define rc o<<1|1 inline void build(int o,int l,int r) { tree[o].l=l;tree[o].r=r; int mid=tree[o].mid(); if(l==r) { tree[o].sum=0; return; } else { build(lc,l,mid); build(rc,mid+1,r); tree[o].sum=tree[lc].sum+tree[rc].sum; } } inline void change(int o,int x,int y) { int l=tree[o].l,r=tree[o].r; int mid=tree[o].mid(); if(l==r) { tree[o].sum+=y; return; } else { if(x>=mid+1) change(rc,x,y); else change(lc,x,y); tree[o].sum=tree[lc].sum+tree[rc].sum; } } inline void pushup(int o) { add[lc]+=add[o]; add[rc]+=add[o]; tree[lc].sum+=(tree[lc].r-tree[lc].l+1)*add[lc]; tree[rc].sum+=(tree[rc].r-tree[rc].l+1)*add[rc]; add[o]=0; } inline void change2(int o,int x,int y,int z) { int l=tree[o].l,r=tree[o].r; if(x<=l&&y>=r) { add[o]+=z; tree[o].sum=(tree[o].r-tree[o].l+1)*z; return; } else if(l>y||x>r) return ; else { if(add[o]) pushup(o); change2(lc,x,y,z); change2(rc,x,y,z); return; } } inline long long query(int o,int x,int y) { int l=tree[o].l,r=tree[o].r; if(x<=l&&y>=r) { return tree[o].sum; } else if(l>y||x>r) return 0; else { if(add[o]) pushup(o); return query(lc,x,y)+query(rc,x,y); } } inline long long query_sum(int x,int y) { int tx=top[x],ty=top[y]; long long ans=0; while(tx!=ty) { if(dep[x]>dep[y]) { swap(x,y); swap(tx,ty); } ans+=query(1,num[ty],num[y]); y=fa[ty];ty=top[y]; } if(dep[x]>dep[y]) { swap(x,y); } ans+=query(1,num[x],num[y]); return ans; } int main() { n=read();m=read(); for(int i=1;i<=n;i++) a[i]=read(),head[i]=-1; for(int i=1;i<n;i++) { int x,y; x=read(); y=read(); addedge(x,y); addedge(y,x); } fa[1]=0;dep[1]=1;dfs(1); dfs2(1,1); build(1,1,z); for(int i=1;i<=n;i++) change(1,num[i],a[i]); for(int i=1;i<=m;i++) { int op,x,y; op=read(); if(op==1) { x=read(),y=read(); change(1,num[x],y); } if(op==2) { x=read();y=read(); change2(1,num[x],num[x]+tot[x]-1,y); } if(op==3) { x=read(); printf("%lld\n",query_sum(1,x)); } } return 0; } ```
by Randyhoads @ 2017-12-31 23:21:04


```cpp #include<bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; inline int read() { char ch; int fl=1; int xx=0; do{ ch= getchar(); if(ch=='-') fl=-1; }while(ch<'0'||ch>'9'); do{ xx=(xx<<3)+(xx<<1)+ch-'0'; ch=getchar(); }while(ch>='0'&&ch<='9'); return xx*fl; } int n,m; int a[MAXN]; struct node { int to; int next; };node g[MAXN*2]; int fa[MAXN],head[MAXN],dep[MAXN],tot[MAXN],son[MAXN],cnt=0; inline void addedge(int x,int y){ ++cnt;g[cnt].to=y;g[cnt].next=head[x];head[x]=cnt;return; } inline void dfs(int x) { tot[x]=1,son[x]=0; for(int i=head[x];i>0;i=g[i].next) { int v=g[i].to; if(v!=fa[x]) { dep[v]=dep[x]+1; fa[v]=x; dfs(v); if(tot[v]>tot[son[x]]) son[x]=v; tot[x]+=tot[v]; } } return; } int num[MAXN],top[MAXN],z=0; inline void dfs2(int now,int t) { top[now]=t;++z;num[now]=z; if(son[now]) dfs2(son[now],t); for(int i=head[now];i>0;i=g[i].next) { int v=g[i].to; if(v!=son[now]&&v!=fa[now]) dfs2(v,v); } } struct TREE { int l; int r; long long sum; inline int mid(){ return (l+r)>>1; } }tree[MAXN*4]; long long add[MAXN*4]; #define lc o<<1 #define rc o<<1|1 inline void build(int o,int l,int r) { tree[o].l=l;tree[o].r=r; int mid=tree[o].mid(); if(l==r) { tree[o].sum=0; return; } else { build(lc,l,mid); build(rc,mid+1,r); tree[o].sum=tree[lc].sum+tree[rc].sum; } } inline void change(int o,int x,int y) { int l=tree[o].l,r=tree[o].r; int mid=tree[o].mid(); if(l==r) { tree[o].sum+=y; return; } else { if(x>=mid+1) change(rc,x,y); else change(lc,x,y); tree[o].sum=tree[lc].sum+tree[rc].sum; } } inline void pushup(int o) { add[lc]+=add[o]; add[rc]+=add[o]; tree[lc].sum+=(tree[lc].r-tree[lc].l+1)*add[o]; tree[rc].sum+=(tree[rc].r-tree[rc].l+1)*add[o]; add[o]=0; } inline void change2(int o,int x,int y,int z) { int l=tree[o].l,r=tree[o].r; if(x<=l&&y>=r) { add[o]+=z; tree[o].sum=(tree[o].r-tree[o].l+1)*z; return; } else if(l>y||x>r) return ; else { if(add[o]) pushup(o); change2(lc,x,y,z); change2(rc,x,y,z); tree[o].sum=tree[lc].sum+tree[rc].sum; return; } } inline long long query(int o,int x,int y) { int l=tree[o].l,r=tree[o].r; if(x<=l&&y>=r) { return tree[o].sum; } else if(l>y||x>r) return 0; else { if(add[o]) pushup(o); return query(lc,x,y)+query(rc,x,y); } } inline long long query_sum(int x,int y) { int tx=top[x],ty=top[y]; long long ans=0; while(tx!=ty) { if(dep[tx]>dep[ty]) { swap(x,y); swap(tx,ty); } ans+=query(1,num[ty],num[y]); y=fa[ty];ty=top[y]; } if(dep[x]>dep[y]) { swap(x,y); } ans+=query(1,num[x],num[y]); return ans; } int main() { n=read();m=read(); for(int i=1;i<=n;i++) a[i]=read(),head[i]=-1; for(int i=1;i<n;i++) { int x,y; x=read(); y=read(); addedge(x,y); addedge(y,x); } fa[1]=0;dep[1]=1;dfs(1); dfs2(1,1); build(1,1,z); for(int i=1;i<=n;i++) change(1,num[i],a[i]); for(int i=1;i<=m;i++) { int op,x,y; op=read(); if(op==1) { x=read(),y=read(); change(1,num[x],y); } if(op==2) { x=read();y=read(); change2(1,num[x],num[x]+tot[x]-1,y); } if(op==3) { x=read(); printf("%lld\n",query_sum(1,x)); } } return 0; } ```
by Randyhoads @ 2017-12-31 23:31:38


@[WLZS](/space/show?uid=37408) 共有三处错误 117行的+=写成= 111行最后的z应为long long 以及线段树单点修改没有管懒标记(可以把100行写成tree[o].sum+=y;来解决)
by Night_Aurora @ 2018-01-01 00:36:03


@[Night\_Aurora](/space/show?uid=25508) 谢谢大佬
by Randyhoads @ 2018-01-01 09:17:59


|