1 条题解

  • 0
    @ 2023-7-21 11:17:55

    image

    #include<iostream>
    #define ll long long
    #define mod 998244353
    using namespace std;
    const int N=100005;
    int n,m,k,s,t,head[N],tot,a[N],c[N],x,sz[N],pos[N];
    struct node{
    	int y,nex;
    }e[N<<1];
    void adde(int s,int t){
    	e[++tot]=(node){t,head[s]};
    	head[s]=tot;
    }
    void dfs(int u,int fa){
    	pos[u]=++tot;sz[u]=1;
    	for (int i=head[u];i;i=e[i].nex){
    		int v=e[i].y;
    		if (v==fa) continue;
    		dfs(v,u);
    		sz[u]+=sz[v];
    	}
    }
    void add(int x,int k){
    	for (;x<=n;x+=x&(-x)) c[x]+=k;
    }
    int query(int x){
    	int tmp=0;
    	for (;x;x-=x&(-x)) tmp+=c[x];
    	return tmp;
    }
    int main()
    {
    	cin>>n;
    	for (int i=1;i<n;++i){
    		cin>>s>>t;
    		adde(s,t);
    		adde(t,s);
    	}
    	tot=0;
    	dfs(1,0);
    	cin>>m;
    	char ch;
    	for (int i=1;i<=n;++i) c[i]=i&(-i),a[i]=1;
    	while (m--){
    		cin>>ch>>x;
    		if (ch=='Q') //区间查询
    			cout<<query(pos[x]+sz[x]-1)-query(pos[x]-1)<<"\n";
    		else {//单点修改
    			if (a[x]) add(pos[x],-1);
    			else add(pos[x],1);
    			a[x]^=1;
    		}
    	}
    }
    
    • 1

    信息

    ID
    310
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    107
    已通过
    32
    上传者