萌新刚学OI连板子都RE了QAQ

P3038 [USACO11DEC] Grass Planting G

qian_shang @ 2020-08-25 15:18:43

RT


#define N 100005
#define M 300005
#define inf 0x7f7f7f
#define R register
#define LL long long
using namespace std;
inline int maxx(int A,int B){return A>B?A:B;}
inline int minn(int A,int B){return A<B?A:B;}
inline int abss(int A){return A<0?-A:A;}
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
    return x*f;
}
int n,m,cnt,hand[M],sum[N<<2],tag[N<<2],oo;
int son[N],siz[N],fa[N],dep[N],id[N],top[N],idx;
struct edge{int to,ne;}e[M];
inline void adda(int a,int b){
    e[cnt].ne=hand[a];
    e[cnt].to=b;
    hand[a]=cnt++;
}
inline void pushup(int o){sum[o]=sum[o<<1]+sum[o<<1|1];}
inline void pushdown(int o,int l,int r){
    if (!tag[o]) return ;
    int mid=(l+r)>>1;
    tag[o<<1]+=tag[o]; tag[o<<1|1]+=tag[o];
    sum[o<<1]+=tag[o]*(mid-l+1);
    sum[o<<1|1]+=tag[o]*(r-mid);
    tag[o]=0;
}
inline void change(int o,int l,int r,int ql,int qr){
    if (l>=ql&&r<=qr){sum[o]+=r-l+1; tag[o]++;return ;} 
    pushdown(o,l,r);
    int mid=(l+r)>>1;
    if (mid>=ql) change(o<<1,l,mid,ql,qr);
    if (mid<qr) change(o<<1|1,mid+1,r,ql,qr);
    pushup(o);
}
inline int query(int o,int l,int r,int ql,int qr){
    if (l>=ql&&r<=qr) return sum[o]; 
    pushdown(o,l,r);
    int mid=(l+r)>>1,ans=0;
    if (mid>=ql) ans+=query(o<<1,l,mid,ql,qr);
    if (mid<qr) ans+=query(o<<1|1,mid+1,r,ql,qr);
    pushup(o);
    return ans;
}
inline void update(int o,int l,int r,int q){
    if (l==r){sum[o]--;return ;}
    int mid=(l+r)>>1;
    if (mid>=q) update(o<<1,l,mid,q);
        else update(o<<1|1,mid+1,r,q);
    pushup(o);
}
inline void dfs1(int u,int fath){
    fa[u]=fath; dep[u]=dep[fa[u]]+1; siz[u]=1;
    int mx=0;
    for (R int i=hand[u];~i;i=e[i].ne){
        int v=e[i].to;
        if (v==fath) continue;
        dfs1(v,u);
        siz[u]+=siz[v];
        if (siz[v]>mx) son[u]=v,mx=siz[v];
    }
    return ;
}
inline void dfs2(int u,int tp){
    id[u]=++idx; top[u]=tp;
    if (!son[u]) return ;
    dfs2(son[u],tp);
    for (R int i=hand[u];~i;i=e[i].ne){
        int v=e[i].to;
        if (v==fa[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}
inline void change_lian(int x,int y){
    while (top[x]!=top[y]){
        if (dep[x]<dep[y]) swap(x,y);
        change(1,1,n,id[top[x]],id[x]);
        x=fa[top[x]];
    }
    if (dep[x]>dep[y]) swap(x,y);
    change(1,1,n,id[x],id[y]);
    update(1,1,n,id[x]);
    return ;
}
inline int query_lian(int x,int y){
    int res=0;
    while (top[x]!=top[y]){
        if (dep[x]<dep[y]) swap(x,y);
        res+=query(1,1,n,id[top[x]],id[x]);
        x=fa[top[x]];
    }
    if (dep[x]>dep[y]) swap(x,y);
    res+=query(1,1,n,id[x],id[y]);
    return res-query(1,1,n,id[x],id[x]);
}
int main(){
//  freopen("P3038_2 (1).in","r",stdin);
//  freopen("out.out","w",stdout);
    memset(hand,-1,sizeof(hand));
    n=read(); m=read();
    for (R int i=1;i<n;i++){
        int a=read(),b=read();
        adda(a,b); adda(b,a);
    }
    dfs1(1,0);
    dfs2(1,1);
    while (m--){
        char ch=getchar();
        while (ch!='P'&&ch!='Q') ch=getchar();
        int a=read(),b=read();
        if (ch=='P') change_lian(a,b);
            else printf("%d\n",query_lian(a,b));
    }
    return 0;
}```

by Remake_ @ 2020-08-25 15:22:06

您不是已经AC了吗


by qian_shang @ 2020-08-25 15:22:59

@Miracle_Creator 那是重来的,但我还不知道这份为什么RE


by grass8cow @ 2020-08-25 15:23:21

这个题单点修区间查用树状数组就行了吧


by qian_shang @ 2020-08-25 15:25:59

@grass8cow 打顺手了QAQ


|