D、Vitya and Strange Lesson(字典树)

题意:

给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\)

\(1<=n<=3e5\)

\(0<=a_i<=3e5\)

\(0<=x<=3e5\)

思路:求\(mex\)可以通过按从高位到低位建字典树,判断左子树是否满,若未满,贪心往走,否则往右走,这样查询是\(log\)的

现在就是异或修改,考虑x的第i位,若为1,则以第i-1层结点为根的左右子树都需要交换

如果每次暴力修改所有是会超时的,使用线段树区间更新那样的技巧,要查询某段区间的时候再做更新。

所以用打标记的方法下传就好了

#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
#define ls(i) seg[i].lc
#define rs(i) seg[i].rc
using namespace std; void read(int &x){
char c = getchar();
x = 0;
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar();
}
const int N = 3e5 + 10; int base[20];
struct T{
int lc,rc,cnt,col;
}seg[N * 25];
int tot,root;
void Insert(int &rt,int x,int dep){
if(!rt) rt = ++tot;
if(dep == 0){
seg[rt].cnt = 1;
return ;
}
if(x & base[dep - 1]) Insert(rs(rt),x,dep - 1);
else Insert(ls(rt),x,dep - 1);
seg[rt].cnt = seg[ls(rt)].cnt && seg[rs(rt)].cnt;
}
void pushdown(int rt,int dep){
if(dep < 0) return ;
int col = seg[rt].col;
seg[ls(rt)].col ^= col, seg[rs(rt)].col ^= col;
if(col & base[dep]) swap(seg[rt].lc,seg[rt].rc);
seg[rt].col = 0;
}
int query(int rt,int dep){
if(dep == 0) return 0;
pushdown(rt,dep - 1);
if(!seg[ls(rt)].cnt) return query(ls(rt),dep - 1);///左子树未满,高位到低位尽量往左边走
else return query(rs(rt),dep - 1) + base[dep - 1];///只能往右走
}
int main(){ int x,n,m;
read(n),read(m);
tot = root = 0;
base[0] = 1;
for(int i = 1;i <= 20;i++) base[i] = base[i-1] * 2;
for(int i = 1;i <= n;i++){
read(x);
Insert(root,x,20);
}
while(m--){
read(x);
seg[root].col ^= x;
printf("%d\n",query(root,20));
}
return 0;
}

Codeforces Round #430 (Div. 2) Vitya and Strange Lesson的更多相关文章

  1. Codeforces Round #430 (Div. 2) 【A、B、C、D题】

    [感谢牛老板对D题的指点OTZ] codeforces 842 A. Kirill And The Game[暴力] 给定a的范围[l,r],b的范围[x,y],问是否存在a/b等于k.直接暴力判断即 ...

  2. 【Codeforces Round #430 (Div. 2) A C D三个题】

    ·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意:    给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...

  3. D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...

  4. Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson

    因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...

  5. 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson

    [链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...

  6. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  7. Codeforces Round #710 (Div. 3) Editorial 1506A - Strange Table

    题目链接 https://codeforces.com/contest/1506/problem/A 原题 1506A - Strange Table Example input 5 1 1 1 2 ...

  8. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

  9. Codeforces Round #430 (Div. 2) C. Ilya And The Tree

    地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...

随机推荐

  1. laravel 基础 --内置函数

    简介 Laravel 自带了一系列 PHP 辅助函数,很多被框架自身使用,如果你觉得方便的话也可以在代码中使用它们. https://laravelacademy.org/post/8967.html ...

  2. Python基于jieba的中文词云

    今日学习了python的词云技术 from os import path from wordcloud import WordCloud import matplotlib.pyplot as plt ...

  3. MySQL 主从服务器配置

    在主服务器Ubuntu上进行备份,执行命令: mysqldump -uroot -p --all-databases --lock-all-tables > ~/master_db.sql -u ...

  4. HDU1301 Jungle Roads(Kruskal)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  5. 初步学习pg_control文件之十

    接前文 初步学习pg_control文件之九 看下面这个 XLogRecPtr checkPoint; /* last check point record ptr */ 看看这个pointer究竟保 ...

  6. 永无BUG 注释

    /** *                   _ooOoo_ *                  o8888888o *                  88" . "88 ...

  7. javascript对象转为字符串

    function getStringTime(time){ //年 year = time.getFullYear(); //月 month = time.getMonth() if(String(m ...

  8. 「学习记录」《数值分析》第二章计算实习题(Python语言)

    在假期利用Python完成了<数值分析>第二章的计算实习题,主要实现了牛顿插值法和三次样条插值,给出了自己的实现与调用Python包的实现--现在能搜到的基本上都是MATLAB版,或者是各 ...

  9. Linux 文件属性及修改权限

    输入 ll 或 ls -l 命令显示当前目录中文件的属性及文件所属的用户和组 root@user:/home/www# ll test total 880 drwxr-xr-x 2 root root ...

  10. 第二十一篇 json,picklz,xml模块

    Json模块 Json模块比较简单,仅有四个方法dumps()和loads()方法,dump()和load()方法,但是却非常的常用,实用性极强. 如果要在不同的编程语言之间传递对象,就必须把对象序列 ...