codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D
题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就是比较节点总数和有的数字数比较有限向左边转移。
然后这个异或其实可以利用一个数num与一个一个的x异或然后求异或的mex也是容易的只要判断当前二进制位是1那么左右节点拥有的数字数互换(不用真的互换
只要用转移体现出来就行了)。这里的01字典树写法是类似线段树且利用递归的方法。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 2e5 + ;
int ch[ * M][] , nodes[ * M];
int node_size , num;
void build(int pos , int node) {
if(pos < ) return;
for(int i = ; i < ; i++) {
ch[node][i] = ++node_size;
nodes[node_size] = ;
build(pos - , ch[node][i]);
}
}
void init() {
node_size = ;
nodes[] = ;
build( , );
}
void Insert(int node , int x , int pos) {
if(pos < ) {
nodes[node] = ;
return ;
}
int id = (x >> pos) & ;
Insert(ch[node][id] , x , pos - );
nodes[node] = nodes[ch[node][id]] + nodes[ch[node][id ^ ]];
}
int query(int node , int x , int pos) {
if(pos < ) return x;
int gg = (num >> pos) & ;
if(nodes[ch[node][gg]] < ( << pos)) return query(ch[node][gg] , x , pos - );
return query(ch[node][gg ^ ] , x | ( << pos) , pos - );
}
int main() {
int n , m , x;
num = ;
init();
scanf("%d%d" , &n , &m);
for(int i = ; i < n ; i++) {
scanf("%d" , &x);
Insert( , x , );
}
for(int i = ; i < m ; i++) {
scanf("%d" , &x);
num ^= x;
printf("%d\n" , query( , , ));
}
return ;
}
codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)的更多相关文章
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
- 51nod 1295 XOR key 可持久化01字典树
题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...
- 【cf842D】Vitya and Strange Lesson(01字典树)
D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)
给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- Codeforces 948D Perfect Security 【01字典树】
<题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...
- Codeforces 979 字符串强制N变换最多出现字母 DFS子树 暴力01字典树
A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ...
随机推荐
- Android Studio项目/Flutter 案例中Gradle报错通用解决方案(包括Unable to tunnel through proxy问题)
目录 Step 1:修改Gradle版本为本地版本 Step 2:修改classpath为Android Studio版本 Step 3:关闭代理 Step 1:修改Gradle版本为本地版本 ...
- Java统计代码行数
package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...
- 又拍云叶靖:OpenResty 在又拍云存储中的应用
2019 年 7 月 6 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙·上海站,又拍云平台开发部负责人叶靖在活动上做了<OpenRest ...
- Of efficiency and methodology
There are only too many articles and books which pertains to the discussion of efficiency and method ...
- java并发编程(四)----(JUC)Lock锁初探
首先我们来回忆一下上一节讲过的synchronized关键字,该关键字用于给代码段或方法加锁,使得某一时刻它修饰的方法或代码段只能被一个线程访问.那么试想,当我们遇到这样的情况:当synchroniz ...
- 自定义markdown代码高亮显示-cnblog
这个代码高亮..一点儿都不高亮...... cnblog里已经有闻道先者贴出代码了, https://www.cnblogs.com/liutongqing/p/7745413.html 效果大概是这 ...
- Cookie&Session
Cookie&Session 背景:Cookie和Session的原理.作用及如何设置和相关面试. 一.诞生背景 HTTP是无状态的,即服务器无法知道两个请求是否来自同一个浏览器,也就是服务器 ...
- SSM框架的详细解说
文章转载自:http://blog.csdn.net/zhshulin 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就 ...
- element ui 登录验证,路由守卫
<template> <!-- el-form :label-position="labelPosition" 设置label的位置 :model 用来给表单设置 ...
- 微信小程序项目总结-记账小程序(包括后端)
一.小程序部分 这是理财系统的前端,江苏海洋大学微信小程序比赛,最后获得了一等奖 GitHub:https://github.com/GeorgeLeoo/finance 1. 项目描述 (1). 此 ...