题目连接:E. Choosing The Commander

题意:士兵有个值,指挥官有两个p,l三种操作,第一种添加一个值为p的士兵,第二种删除一个值为p的士兵,(士兵pi只会尊敬pi^p<l的指挥官)第三种询问现有的士兵中有多少个尊敬这个指挥官。

题解:我们把士兵建一棵0,1字典树,然后在询问的时候,根据p,l,dfs整棵字典树,在加上满足条件的值,复杂度0(nlogp)

 1 #include<bits/stdc++.h>
 2 #include<set>
 3 #include<cstdio>
 4 #include<iomanip>
 5 #include<iostream>
 6 #include<string>
 7 #include<cstring>
 8 #include<algorithm>
 9 #define pb push_back
10 #define ll long long
11 #define fi first
12 #define se second
13 #define PI 3.14159265
14 #define ls l,m,rt<<1
15 #define rs m+1,r,rt<<1|1
16 #define eps 1e-7
17 #define pii pair<int,int>
18 typedef unsigned long long ull;
19 const int mod=1e3+5;
20 const ll inf=0x3f3f3f3f3f3f3f;
21 const int maxn=1e5+5;
22 using namespace std;
23 int n,s;
24 struct tire//字典树
25 {
26     int sz,rt,now;
27     struct node
28     {
29         int nxt[2],cnt;
30     }st[maxn*32];
31     int node()//构建新节点
32     {
33         st[sz].nxt[0]=st[sz].nxt[1]=-1;st[sz].cnt=0;
34         sz++;return sz-1;
35     }
36     void init()
37     {
38         sz=0;
39         rt=node();
40     }
41     void ins(int x)//插入
42     {
43         now=rt;st[now].cnt++;
44         for( int i=31;i>=0;i--)
45         {
46             int id=(x>>i)&1;
47             if(st[now].nxt[id]==-1)st[now].nxt[id]=node();
48              now=st[now].nxt[id];st[now].cnt++;
49         }
50     }
51     void erase(int x)
52     {
53         now=rt; st[now].cnt--;
54         for(int i=31;i>=0;i--)
55         {
56             int id=(x>>i)&1;
57            now=st[now].nxt[id];st[now].cnt--;
58         }
59     }
60     int ask(int x,int l,int v,int len=31)
61     {
62         if(v==-1||len<0)return 0;
63         int pp=(x&(1<<len))>0;
64         if(l&(1<<len)){
65            return (st[v].nxt[pp]==-1?0:st[st[v].nxt[pp]].cnt)+ask(x,l,st[v].nxt[pp^1],len-1);
66         }
67         else return ask(x,l,st[v].nxt[pp],len-1);
68     }
69 }ac;
70 int main()
71 {
72     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
73     ac.init();
74     cin>>n;
75     while(n--)
76     {
77         int op,p,l;
78         cin>>op>>p;
79         if(op==1)ac.ins(p);
80         else if(op==2)ac.erase(p);
81         else cin>>l,cout<<ac.ask(p,l,ac.rt)<<endl;
82     }
83     return 0;
84 }

View Code

转载于:https://www.cnblogs.com/lhclqslove/p/9381093.html

Educational Codeforces Round 23 E. Choosing The Commander 字典树相关推荐

  1. Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)

    Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...

  2. Educational Codeforces Round 17 E. Radio stations cdq分治 + 树状数组

    传送门 文章目录 题意 思路: 题意 有nnn个电台,对于每个电台iii有三个参数xi,ri,fix_i,r_i,f_ixi​,ri​,fi​,分别指他们的坐标.作用半径.频率.如果两个电台频率差值在 ...

  3. Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

    F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

  4. Educational Codeforces Round 22 E. Army Creation 【主席树】

    题目链接 题意 给出一个序列a[1..n]a[1..n]a[1..n],mmm 次询问区间 [L,R][L,R][L,R] 可以选多少个数(相同的数最多选 KKK 个),强制在线 题解 先求出每个 i ...

  5. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  6. Educational Codeforces Round 24 E. Card Game Again(双指针)

    题目链接:Educational Codeforces Round 24 E. Card Game Again 题意: 给你n个数和一个数k. 现在每次可以拿掉前x个数,后y个数,剩下的数的乘积要能被 ...

  7. Educational Codeforces Round 25 G. Tree Queries

    题目链接:Educational Codeforces Round 25 G. Tree Queries 题意: 给你一棵树,一开始所有的点全是黑色,有两种操作. 1 x 将x这个点变为黑色,保证第一 ...

  8. Educational Codeforces Round 40千名记

    人生第二场codeforces.然而遇上了Education场这种东西 Educational Codeforces Round 40 下午先在家里睡了波觉,起来离开场还有10分钟. 但是突然想起来还 ...

  9. Educational Codeforces Round 133 (Rated for Div. 2)(CD题解)

    Educational Codeforces Round 133 (Rated for Div. 2)CD题解 过AB补CD C. Robot in a Hallway 题意 题意:现有 2∗m 的方 ...

最新文章

  1. 简明docker教程
  2. 進階查詢,讓查詢條件精確到底...
  3. 智邦国际怎么样?企业管理软件好不好?
  4. 【bzoj5197】[CERC2017]Gambling Guide 期望dp+堆优化Dijkstra
  5. Spring Web Service 学习之Hello World篇
  6. 计算机互联网行业高校,9家互联网巨头最青睐的重点大学汇总,网友:比各种排名强太多了...
  7. c语言scanf函数隐藏的缓冲区,零基础学C语言 笔记四 Scanf函数清除缓冲区
  8. mysql实例详解_MySQL 多实例详解
  9. 《iOS9开发快速入门》——第2章,第2.1节Xcode 7.0的新特性
  10. SpringBoot + Eureka启动失败
  11. Swift开发:使用SwiftyJSON解析JSON数据
  12. MFC ScreenToClient坐标转换错误
  13. OpenLayers分屏联动对比
  14. 2008-2020年各省地方债务余额数据(wind)
  15. 计算机键盘句号,句号怎么打_键盘句号怎么打_句号怎么打在电脑上-Guide信息网...
  16. Spark中组件Mllib的学习16之分布式行矩阵的四种形式
  17. python中的pass是什么意思_Python中pass的作用与使用教程
  18. [JavaScript]基础知识复习题附答案
  19. 计算机科学与探索 sci,第一篇SCI论文投稿经历(计算机专业)
  20. npm ERR! errno ECONNRESET npm ERR! network This is a problem related to network connectivity.解决方法和步骤

热门文章

  1. printf参数的问题
  2. Java普通工程转换成标准的maven工程
  3. Linux运维系统工程师系列---17
  4. Web前端Javascript笔记(8)Ajax前后端交互
  5. 我的第一本算法书(图解算法)——什么是哈希表
  6. 计算机的原理两条分别是,微机原理习题答案4
  7. html 多页面合并,让多个HTML页面 使用 同一段HTML代码
  8. 电子助力方向机控制模块_【技师投稿】使用道通MS908PRO更换宝马F20底盘方向机...
  9. C++递归或非递归实现n的阶乘
  10. paramiko远程密码连接、批量连接主机