zoukankan      html  css  js  c++  java
  • [POI2007]MEG-Megalopolis 树状数组 + dfs序前缀和 好题

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=250000+6;
    int C[N<<1],head[N<<1],to[N<<1],nex[N<<1],idx[N],cnt,idx2[N];
    int nums=0;
    void add_edge(int u,int v){
        nex[++cnt]=head[u],head[u]=cnt,to[cnt]=v;
    }
    int lowbit(int t){
        return t&(-t);
    }
    void update(int t,int delta){
        while(t<(N<<1))C[t]+=delta,t+=lowbit(t);
    }
    int query(int t){
        int tmp=0;
        while(t>0)tmp+=C[t],t-=lowbit(t);
        return tmp;
    }
    void dfs(int u,int fa)
    {
        ++nums;
        if(u!=1)update(nums,1);
        idx[u]=nums;
        for(int v=head[u];v;v=nex[v])
            if(to[v]!=fa)dfs(to[v],u);
        ++nums;
        if(u!=1)update(nums,-1);
        idx2[u]=nums;
    }
    int main()
    {
       // freopen("in.txt","r",stdin);
        int n,m;
        scanf("%d",&n);
        for(int i=1;i<n;++i){
            int a,b;
            scanf("%d%d",&a,&b);
            add_edge(a,b);
            add_edge(b,a);
        }
        dfs(1,-1);
        scanf("%d",&m);
        for(int i=1;i<=m+n-1;++i)
        {
            char s[4];
            scanf("%s",s);
            if(s[0]=='W')
            {
                int a;
                scanf("%d",&a);
                printf("%d
    ",query(idx[a]));
            }
            else if(s[0]=='A')
            {
                int a,b;
                scanf("%d%d",&a,&b);
                int cur=max(idx[a],idx[b]);
                int cur2=min(idx2[a],idx2[b]);
                update(cur,-1);
                update(cur2+1,1);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    10.21SQL注入
    10.15计网相关
    10.11php+mysql
    10.10 接在10.8随笔中
    10.9 利用微信dll反弹shell复现
    10.8php续
    9.29 接9.27PHP相关
    java泛型
    java集合之Map接口
    java集合之Collection接口
  • 原文地址:https://www.cnblogs.com/guangheli/p/9845215.html
Copyright © 2011-2022 走看看