UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214601#2376. 树与异或Super_Leo66Compile Error//C++3.3kb2024-11-20 20:01:342024-11-20 23:03:57

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5,MOD=1e9+7;
int n,m,x,y;
int head[N],tot,v[N];
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<<1)+(x<<3)+(ch^'0'),ch=getchar();
	return x*f;
}
void write(int x){
	if(x<0) x=-x,putchar('-');
	if(x>=10) write(x/10);
	putchar(x%10+'0');
}
vector<int> G[N];
int vis[N],D_fa[N],SZ,MX,sz[N],r;
int dep[N],f[N][25],a[N];
void dfs(int u,int fa){
    dep[u]=dep[fa]+1; f[u][0]=fa;
    for(int i=1;i<=21;i++) f[u][i]=f[f[u][i-1]][i-1];
    for(int v:G[u]){
        if(v==fa) continue;
        dfs(v,u);
    }
}
int cnt,p[N];
void findrt(int u,int fa){
    sz[u]=1;p[++cnt]=u;
    int mx=0;
    for(int v:G[u]){
        if(v==fa||vis[v]) continue;
        findrt(v,u);
        sz[u]+=sz[v];
        mx=max(mx,sz[v]);
    }
    mx=max(mx,SZ-sz[u]);
    if(mx<MX) MX=mx,r=u;
}

template<typename TT>
class BIT{
	vector<int> T;
	int lowbit(int x){
		return x&-x;
	}
public:
    void build(int x){
        for(int i=0;i<=x+2;i++)
			T.push_back(0);
    }
    void add(int x,int y){
        for(int i=x;i<T.size();i+=lowbit(i))
            T[i]^=y;
    }
    int query(int x){
        int res=0;
        x=min(x,(int)T.size()-1);
        for(int i=x;i;i-=lowbit(i))
            res^=T[i];
        return res;
    }
};
BIT<int> S1[N],S2[N];
int LCA(int x,int y){
    if(dep[x]<dep[y]) swap(x,y);
    for(int i=21;~i;i--)
    	if(dep[f[x][i]]>=dep[y])
            x=f[x][i];
    if(x==y) return x;
    for(int i=21;~i;i--)
        if(f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    return f[x][0];
}
int getdis(int x,int y){
    return dep[x]+dep[y]-2*dep[LCA(x,y)];
}
void dfs2(int r,int u,int fa,int DEP){
    S1[r].add(DEP,a[u]);
    for(int v:G[u]){
        if(v==fa||vis[v]) continue;
        dfs2(r,v,u,DEP+1);
    }
}
void solve(int u,int fa){
    vis[u]=1;D_fa[u]=fa;
    S2[u].build(cnt);
    if(fa)
        for(int i=1;i<=cnt;i++)
            S2[u].add(getdis(p[i],fa),a[p[i]]);
    S1[u].build(cnt);
    for(int v:G[u]){
        if(vis[v]) continue;
        dfs2(u,v,u,1);
    }
    for(int v:G[u]){
        if(vis[v]) continue;
        SZ=MX=sz[v],r=cnt=0;
        findrt(v,0),solve(r,u);
    }
}
int query(int x,int k){
    int res=0,u=x;
    res=res^S1[x].query(k)^a[x];
    while(D_fa[u]){
        int d=getdis(x,D_fa[u]);
        if(d<=k) res=res^a[D_fa[u]]^S1[D_fa[u]].query(k-d)^S2[u].query(k-d);
        u=D_fa[u];
    }
    return res;
}
void update(int x,int y){
    int u=x;
	while(D_fa[u]){
        int d=getdis(x,D_fa[u]);
        S2[u].add(d,a[x]),S2[u].add(d,y);
        u=D_fa[u];
        S1[u].add(d,a[x]),S1[u].add(d,y);
    }
    a[x]=y;
}
signed main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<n;i++){
        x=read(),y=read();
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1,0);
    SZ=MX=n,cnt=0;
    findrt(1,0),solve(r,0);
    int res=0;
    for(int i=1;i<=m;i++){
        cin>>x>>y;
        update(x,y);
        res=(res+query(x,2)*i%MOD*i%MOD)%MOD;
    }
    write(res),puts("");
    return 0;
}

详细

answer.code: In function 'void dfs(long long int, long long int)':
answer.code:25:15: error: range-based 'for' loops are not allowed in C++98 mode
     for(int v:G[u]){\x0d
               ^
answer.code: In function 'void findrt(long long int, long long int)':
answer.code:34:15: error: range-based 'for' loops are not allowed in C++98 mode
     for(int v:G[u]){\x0d
               ^
answer.code: In function 'void dfs2(long long int, long long int, long long int, long long int)':
answer.code:84:15: error:...