• Educational Codeforces Round 23 E. Choosing The Commander trie数


    E. Choosing The Commander
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires.

    Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors.

    Each warrior is represented by his personality — an integer number pi. Each commander has two characteristics — his personality pj and leadership lj (both are integer numbers). Warrior i respects commander j only if ( is the bitwise excluding OR of x and y).

    Initially Vova's army is empty. There are three different types of events that can happen with the army:

    • pi — one warrior with personality pi joins Vova's army;
    • pi — one warrior with personality pi leaves Vova's army;
    • pi li — Vova tries to hire a commander with personality pi and leadership li.

    For each event of the third type Vova wants to know how many warriors (counting only those who joined the army and haven't left yet) respect the commander he tries to hire.

    Input

    The first line contains one integer q (1 ≤ q ≤ 100000) — the number of events.

    Then q lines follow. Each line describes the event:

    • pi (1 ≤ pi ≤ 108) — one warrior with personality pi joins Vova's army;
    • pi (1 ≤ pi ≤ 108) — one warrior with personality pi leaves Vova's army (it is guaranteed that there is at least one such warrior in Vova's army by this moment);
    • pi li (1 ≤ pi, li ≤ 108) — Vova tries to hire a commander with personality pi and leadership li. There is at least one event of this type.
    Output

    For each event of the third type print one integer — the number of warriors who respect the commander Vova tries to hire in the event.

    Example
    Input
    5
    1 3
    1 4
    3 6 3
    2 4
    3 6 3
    Output
    1
    0
    Note

    In the example the army consists of two warriors with personalities 3 and 4 after first two events. Then Vova tries to hire a commander with personality 6 and leadership 3, and only one warrior respects him (, and 2 < 3, but , and 5 ≥ 3). Then warrior with personality 4 leaves, and when Vova tries to hire that commander again, there are no warriors who respect him.

    题意:1增加一个数,2删除一个数,3求所有数^k<l的个数

    思路:trie数,按位贪心即可;

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    #include<bitset>
    #include<time.h>
    using namespace std;
    #define LL long long
    #define pi (4*atan(1.0))
    #define eps 1e-4
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e6+10,M=4e6+10,inf=2147483647,mod=1e9+7;
    const LL INF=1e18+10,MOD=1e9+7;
    
    int a[M][3],sum[M],len;
    void insertt(int x)
    {
        int num[31];
        memset(num,0,sizeof(num));
        int flag=0;
        while(x)
        {
            num[flag++]=x%2;
            x/=2;
        }
        int u=0,n=29;
        for(int i=n; i>=0; i--)
        {
            if(!a[u][num[i]])a[u][num[i]]=++len;
            u=a[u][num[i]];
            sum[u]++;
        }
    }
    void del(int x)
    {
        int num[31];
        memset(num,0,sizeof(num));
        int flag=0;
        while(x)
        {
            num[flag++]=x%2;
            x/=2;
        }
        int u=0,n=29;
        for(int i=n; i>=0; i--)
        {
            if(!a[u][num[i]])a[u][num[i]]=++len;
            u=a[u][num[i]];
            sum[u]--;
        }
    }
    
    int getans(int x,int z)
    {
        int num[31],flag=0;
        memset(num,0,sizeof(num));
        while(x)
        {
            num[flag++]=x%2;
            x/=2;
        }
        int l[31];flag=0;
        memset(l,0,sizeof(l));
        while(z)
        {
            l[flag++]=z%2;
            z/=2;
        }
        int u=0,n=29;
        int ans=0;
        for(int i=n;i>=0;i--)
        {
            if(l[i]==0)
            {
                if(!a[u][num[i]])break;
                u=a[u][num[i]];
            }
            else
            {
                if(a[u][num[i]])
                ans+=sum[a[u][num[i]]];
                if(!a[u][!num[i]])break;
                u=a[u][!num[i]];
            }
        }
        return ans;
    }
    
    int main()
    {
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int t,p;
            scanf("%d%d",&t,&p);
            if(t==1)insertt(p);
            else if(t==2)del(p);
            else
            {
                int x;
                scanf("%d",&x);
                printf("%d
    ",getans(p,x));
            }
        }
        return 0;
    }
  • 相关阅读:
    java中的变量
    小程序获取设备信息
    小游戏学习1
    live-server
    Babel安装在本地并用webstrom由ES6转Es5
    测试目录
    使用Java模拟一个简单的Dos学生成绩管理系统:
    RabbitMQ——开源稳定的消息队列
    wpf读写app.config中的数据
    C# Arc Gis实例教程——网络链接
  • 原文地址:https://www.cnblogs.com/jhz033/p/7075041.html
Copyright © 2020-2023  润新知