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;
    }
    
  • 相关阅读:
    GX转账站点无法访问的问题

    .NET易忘备留 ORACLE存储过程调用
    Oracle 字符串函数
    Oracle 数值函数
    AJAX.JSONP 跨域
    机器人部署的注意事项
    IE6、7绝对定位层被遮挡的原因(主要是父层决定的)
    Oracle 新手问答
    字符设备驱动范例
  • 原文地址:https://www.cnblogs.com/guangheli/p/9845215.html
Copyright © 2011-2022 走看看