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. 如何解决thinkphp5中验证码常见问题?

    对于thinkphp如何实现验证码,我这里就不介绍了.直接看之前的文章 http://www.cnblogs.com/qqblog/p/6639419.html.下面,我能想出来的是,我自己在开发过程 ...

  2. flask之route中的参数

    flask的路由中有一些参数 使用案例 from flask import Flask, render_template, url_for, session, request, redirect ap ...

  3. php post提交xml文件

    <?php header("Content-type: text/xml;"); // xml code demo $xmlData = '<?xml version= ...

  4. JQ 封装全选函数

    单击时触发效果: 如果有一个没有选中把全选的勾去了,如果select所有的都选中了,那就把全选勾上 html里: <div class="row cl"> <la ...

  5. mongodb的学习之旅一

    描述 作为一枚菜鸟级别的coder,刚接触nodejs没有多久.现在在学习微信公众号的开发,但是碰到了mongodb保存用户数据的时候,出现了DeprecationWarning: Mongoose: ...

  6. Rmarkdown:输出pdf设置

    输出pdf需要安装Ctex --- title: "first markdown" author: "name" date: "`r format(S ...

  7. 【Leetcode】804. Unique Morse Code Words

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

  8. git初始化仓库相关

    当我们需要新建一个git项目会遇到的问题 全局设置 git config --global user.name "名字" git config --global user.emai ...

  9. vs2013中将复制过来的文件或文件夹显示到解决方案管理

    先将文件夹和文件复制到VS程序所在的位置,在VS2013解决方案资源管理器中找到这些文件所在的上一级文件夹,先将那个上层文件夹收缩起来,然后再点击解决方案资源管理器上的“显示所有文件”按纽,展开这个文 ...

  10. 一些可能有点用处的C#开发经验

    前言: 下个月就要去进行Java开发了,以后C#碰的就少了(可惜去年买了三本C#的书,几乎还是全新的……),平时一些经验都记在OneNote里面,现在收集整理出来,因为只能利用交接工作的打酱油的时间, ...