CF817E Choosing The Commander

CF817E Choosing The Commander

Description

对一个可重集有Q次操作,支持加入,删除,以及询问集合中异或上一个值后比给定值小的数的个数

Solution

01Trie裸题

查询的时候顺着往下加一下每个前缀对应的答案就好了

#include<bits/stdc++.h>

using namespace std;

#define LL long long 

inline LL read()
{
    LL f = 1,x = 0;
    char ch;
    do
    {
        ch = getchar();
        if(ch == '-')f = -1;
    }while(ch < '0'||ch > '9');
    do
    {
        x = (x<<3) + (x<<1) + ch - '0';
        ch = getchar();
    }while(ch >= '0' && ch  <= '9');
    return f*x;
}

const int MAXN = 100000 + 10;

int Q;
int Trie[MAXN * 30][2];
int tot[MAXN * 30],cnt = 1;

inline void insert(int x,int v)
{
    int cur = 1;
    for(int i=30;i>=0;i--)
    {
        int res = 0;
        tot[cur]+=v;
        if(x&(1<<i)) res = 1;
        if(!Trie[cur][res]) Trie[cur][res] = ++cnt,cur = cnt;
        else cur = Trie[cur][res]; 
    }
    tot[cur]+=v;
    return;
}

inline int query(int p,int l)
{
    int cur = 1,ans = 0;
    for(int i=30;i>=0;i--)
    {
        bool v = (1<<i) & l,w = ((1<<i) & p);
        if(!v) cur = Trie[cur][w];
        else ans += tot[Trie[cur][w]],cur = Trie[cur][w^1];
    }
    return ans;
}

int main()
{
    Q = read();
    while(Q--)
    {
        int opt = read();
        if(opt == 1)
        {
            int p = read();
            insert(p,1);
        }
        else if(opt == 2)
        {
            int p = read();
            insert(p,-1);
        }
        else 
        {
            int p = read(),l = read();
            printf("%d\n",query(p,l));
        }
    }
}

 

posted @ 2020-11-12 22:07  wlzs1432  阅读(61)  评论(0编辑  收藏  举报