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));
            }
        }
    }
  • 相关阅读:
    跨域访问方法列举 jsonp 和 客户端
    session 垃圾回收机制
    php 根据数据权重,得分或者持有数量等进行均衡分配给定数量分配方法
    进程和线程比较
    redis 过期策略分析
    redis 基础知识详解
    tcp/ip 协议
    ip 协议详解
    php redis 分布式类
    nginx打开目录游览功能
  • 原文地址:https://www.cnblogs.com/wlzs1432/p/13966555.html
Copyright © 2011-2022 走看看