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. phpspider 的简单使用

    phpspider 的简单使用 phpspider是一款PHP开发蜘蛛爬虫框架. 官方github下载地址:https://github.com/owner888/phpspider官方文档下载地址: ...

  2. mysql数据库的基本使用命令总结

    mysql数据库是一个常用的关系型数据库 关系型数据库核心元素有哪些? 主键:特殊字段,用来唯一标识记录的唯一性 字段:数据列 记录:数据行 数据表:数据行的集合 数据库:数据表的集合 安装.启动.停 ...

  3. python之三元运算

    三元运算(三目运算):用于较简单的判断(if   else). if True: return s = "aaaa" else: return s = "bbbb&quo ...

  4. 【ajax】ajax异步实现用户注册验证

    从前台到后台实现简单用户注册检查用户是否存在 1.编写domain public class User { private String username; private String passwo ...

  5. ADB工具的安装

    1.Windows ADB工具下载地址: https://developer.android.google.cn/studio/releases/platform-tools ADB工具官网教程: h ...

  6. docker学习(三) 安装docker的web可视化管理工具

    1.docker是一个一款很轻便的应用容器引擎,为了更好的管理和使用docker,使用web可视化管理工具似乎更符合大多数人的需求.在这里,我给大家分享下自己使用过的几款web工具:docker UI ...

  7. Django-Content-type用法

    from django.db import models from django.contrib.contenttypes.models import ContentType from django. ...

  8. 【C#】 反射

    [C#] 反射 目录 : http://msdn.microsoft.com/zh-cn/library/System.Reflection(v=vs.110).aspx System.Reflect ...

  9. Django笔记 —— 模板高级进阶

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  10. android中的AIDL学习笔记

    一.定义 AIDL是用来解决进程间通信的(一般有四种方式:Activity.Service.ContentProvider.Broadcast Receiver),两个进程间无法直接通信,所以要用AI ...