zoukankan      html  css  js  c++  java
  • 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
    ",query(p,l));
            }
        }
    }
  • 相关阅读:
    学期总结
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    作业02
    c语言 学习笔记之二 选择题2
  • 原文地址:https://www.cnblogs.com/wlzs1432/p/13966555.html
Copyright © 2011-2022 走看看