无端RE 0pts 救救MnZn吧

P3038 [USACO11DEC] Grass Planting G

ask_silently @ 2024-08-06 18:27:06

#include <bits/stdc++.h>
using namespace std;

const int N=2e5+10;

int n,m,idx,cnt;
int a[4*N],lan[4*N],val[N];
int head[N],nxt[2*N],ver[2*N];
int id[N],top[N],son[N],fa[N],siz[N],d[N]; 

inline int read(){
    int t=0,f=1;
    register char c=getchar();
    while (c<48||c>57) f=(c=='-')?(-1):(f),c=getchar();
    while (c>=48&&c<=57)t=(t<<1)+(t<<3)+(c^48),c=getchar();
    return f*t;
}

void add(int u,int v){
    nxt[++idx]=head[u];
    head[u]=idx;
    ver[idx]=v;
}

void dfs1(int u,int f){
    int maxx=0;
    d[u]=d[f]+1,siz[u]=1,fa[u]=f;
    for(int i=head[u];i;i=nxt[i]){
        if(ver[i]==f) continue;
        dfs1(ver[i],u);
        siz[u]+=siz[ver[i]];
        if(siz[ver[i]]>maxx) maxx=siz[ver[i]],son[u]=ver[i];
    }
}

void dfs2(int u,int to){
    id[u]=++cnt,top[u]=to;
    if(!son[u]) return;
    dfs2(son[u],to);
    for(int i=head[u];i;i=nxt[i]){
        int dao=ver[i];
        if(dao==fa[u]||dao==son[u]) continue;
        dfs2(dao,dao);
    }
}

void init(){
    dfs1(1,0);
    dfs2(1,1);
}

void pushdown(int bian,int l,int r){
    int mid=(l+r)>>1;
    a[bian<<1]+=(mid-l+1);
    a[bian<<1|1]+=(r-mid);
    lan[bian<<1]+=lan[bian];
    lan[bian<<1|1]+=lan[bian];
    lan[bian]=0;
}

void update(int bian,int l,int r,int L,int R){
    if(L<=l&&R>=r){
        lan[bian]++;
        a[bian]+=r-l+1;
        return; 
    }
    if(lan[bian]) pushdown(bian,l,r);
    int mid=(l+r)>>1;
    if(L<=mid) update(bian<<1,l,mid,L,R);
    if(R>mid) update(bian<<1|1,mid+1,r,L,R);
    a[bian]=a[bian<<1]+a[bian<<1|1];
}

int query(int bian,int l,int r,int C){
    if(l==r) return a[bian];
    if(lan[bian]) pushdown(bian,l,r);
    int mid=(l+r)>>1;
    if(mid<C) query(bian<<1|1,mid+1,r,C);
    else query(bian<<1,l,mid,C);
}

int solve2(int x){return query(1,1,n,id[x]);}

void solve1(int l,int r){
//  bool flag=false;
//  if(l==2&&r==3) flag=true;
    while(top[l]!=top[r]){
        if(d[top[l]]<d[top[r]]) swap(l,r);
        update(1,1,n,id[top[l]],id[l]);
        l=fa[top[l]];
    }
//  if(flag){
//      cout<<"l:"<<l<<" r:"<<r<<endl;
//      for(int i=1;i<=n;i++) cout<<solve2(id[i])<<" ";
//      cout<<endl;
//  }
    if(d[l]>d[r]) swap(l,r);
    if(l!=r) update(1,1,n,id[l]+1,id[r]);
}

void debug(){
    for(int i=1;i<=n;i++) cout<<solve2(id[i])<<" ";
    cout<<endl;
}

int main(){
    n=read(),m=read();
    for(int i=1;i<n;i++){
        int u=read(),v=read();
        add(u,v);add(v,u);
    }
    init();
    while(m--){
        char opt;
        cin>>opt;
        int u=read(),v=read();
        if(d[u]>d[v]) swap(u,v);
        if(opt=='P') solve1(u,v);
        else cout<<solve2(v)<<"\n";
//      debug();
    }
    return 0;
}

by zk_y @ 2024-08-06 18:37:23

@dldlzqfs query没有返回值


by ask_silently @ 2024-08-07 08:29:12

@zk_y 还得靠你%%%


by ask_silently @ 2024-08-07 08:32:44

@zk_y 然后就31pts了


by zk_y @ 2024-08-07 08:54:44

@dldlzqfs 额


by ask_silently @ 2024-08-07 09:50:08

调出来了,此贴结


|