CF1055F Tree and XOR
就是选择两个数找第k大对儿
第k大?二分+trie上验证
O(nlognlogn)
直接按位贪心
维护可能的决策点(a,b)表示可能答案的对儿在a和b的子树中选择
所有可能决策点都贡献这一位是0,看是否<=k
然后更新出下一层的决策点
但是空间太小,
所以要滚动
我的方法:
维护trie节点和控制区间,维护每个区间的元素,维护决策点
注意,(a,b)(b,a)算两个。考虑a!=b时候贡献*=2
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
// #define int long long
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} namespace Miracle{
const int N=1e6+;
ll k,a[N];
int b[][N],num[];
ll ans;
struct tr{
int ls,rs;
int l,r;
void init(){
ls=rs=l=r=;
}
void up(int ll,int rr){
l=ll;r=rr;
}
int sz(){
if(l&&r) return r-l+;
return ;
}
}t[][N];
int tot[];
int n;
pii s[][N];
int q[];
struct node{
int nxt,to;
ll val;
}e[N];
int hd[N],cnt;
void add(int x,int y,ll z){
e[++cnt].nxt=hd[x];
e[cnt].to=y;e[cnt].val=z;
hd[x]=cnt;
}
void dfs(int x,ll dis){
b[][++num[]]=x;a[x]=dis;
for(reg i=hd[x];i;i=e[i].nxt){
int y=e[i].to;
dfs(y,dis^e[i].val);
}
}
int tmp;
void build(int x,int d){
int las=tmp,now=tmp^;
int bc=t[las][x].r;
for(reg i=t[las][x].l;i<=t[las][x].r;++i){
if(!(a[b[las][i]]&(1LL<<d))){
b[now][++num[now]]=b[las][i];
}else{
b[now][bc--]=b[las][i];
}
}
if(num[now]>=t[las][x].l){
++tot[now];
t[las][x].ls=tot[now];
t[now][tot[now]].init();
t[now][tot[now]].up(t[las][x].l,num[now]);
}
if(num[now]<t[las][x].r){
++tot[now];
t[las][x].rs=tot[now];
t[now][tot[now]].init();
t[now][tot[now]].up(num[now]+,t[las][x].r);
}
num[now]=t[las][x].r;
}
int main(){
rd(n);rd(k);
int y;ll w;
for(reg i=;i<=n;++i){
rd(y);rd(w);add(y,i,w);
}
dfs(,);
t[][++tot[]].up(,num[]);
num[]=;tot[]=;
build(tot[],);
s[][++q[]]=mk(,);
for(reg d=;d>=;--d){
ll con=;
int las=tmp;
int now=tmp^;
for(reg i=;i<=q[las];++i){
if(s[las][i].fi==s[las][i].se){
con+=(ll)t[now][t[las][s[las][i].fi].ls].sz()*t[now][t[las][s[las][i].se].ls].sz()+
(ll)t[now][t[las][s[las][i].fi].rs].sz()*t[now][t[las][s[las][i].se].rs].sz();
}else{
con+=(ll)t[now][t[las][s[las][i].fi].ls].sz()*t[now][t[las][s[las][i].se].ls].sz()*+
(ll)t[now][t[las][s[las][i].fi].rs].sz()*t[now][t[las][s[las][i].se].rs].sz()*;
}
}
// cout<<" con "<<con<<endl;
if(k>con){//
k-=con;
ans+=(1LL<<d);
q[now]=;
for(reg i=;i<=q[las];++i){
if(t[las][s[las][i].fi].ls&&t[las][s[las][i].se].rs){
s[now][++q[now]]=mk(t[las][s[las][i].fi].ls,t[las][s[las][i].se].rs);
}
if(s[las][i].fi!=s[las][i].se){
if(t[las][s[las][i].fi].rs&&t[las][s[las][i].se].ls){
s[now][++q[now]]=mk(t[las][s[las][i].fi].rs,t[las][s[las][i].se].ls);
}
}
}
}else{
q[now]=;
for(reg i=;i<=q[las];++i){
if(t[las][s[las][i].fi].ls&&t[las][s[las][i].se].ls){
s[now][++q[now]]=mk(t[las][s[las][i].fi].ls,t[las][s[las][i].se].ls);
}
if(t[las][s[las][i].fi].rs&&t[las][s[las][i].se].rs){
s[now][++q[now]]=mk(t[las][s[las][i].fi].rs,t[las][s[las][i].se].rs);
}
}
}
tmp^=;
if(d){
swap(now,las);
tot[now]=;
num[now]=;
for(reg i=;i<=tot[las];++i){
build(i,d-);
}
}
}
ot(ans);
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
*/
然后这个代码又臭又长
一个很nb的写法:
1.首先不用建树,p<=i,直接v[i]=v[p]^w,一行搞定
2.不用维护决策两个点,只用维护每个元素,可能匹配的子树节点位置!以及自己的权值属于的位置
3.每次更新节点的size和a,s和k比较大小
4.更新b,
也根本不用0/1滚动,b和a数组已经区分了位置。
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} namespace Miracle{
const int N=1e6+;
ll k,s,t,v[N];
int n;
int a[N],b[N],ch[N][],sz[N];
int tot;
ll ans;
int get(int x,int c){
return ch[x][c]?ch[x][c]:ch[x][c]=++tot;
}
int main(){
rd(n);rd(k);int p;ll w;
for(reg i=;i<=n;++i) rd(p),rd(w),v[i]=v[p]^w;
for(reg i=;i<=n;++i) a[i]=b[i]=;
for(reg d=;d>=;--d){
for(reg i=;i<=tot;++i) ch[i][]=ch[i][]=sz[i]=;
tot=s=t=;
for(reg i=;i<=n;++i) sz[a[i]=get(a[i],v[i]>>d&)]++;
for(reg i=;i<=n;++i) s+=sz[ch[b[i]][v[i]>>d&]];
if(s<k) k-=s,t=,ans|=(1LL<<d);
for(reg i=;i<=n;++i) b[i]=ch[b[i]][(v[i]>>d&)^t];
}
ot(ans);
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
*/
tql!
CF1055F Tree and XOR的更多相关文章
- 解题:CF1055F Tree and XOR
题面 树上路径是可以通过到根的路径和LCA差出来的,所以建立一棵Trie树按位贪心即可......吗? 发现空间并不够,需要我们每层现建,要记录每个数和它异或答案之后在这一层插进去的编号 #inclu ...
- [atAGC052F]Tree Vertices XOR
结论 注意到如果$x$周围有偶数个1,对$x$操作显然不会改变$a_{x}$,因此不妨强制操作的点周围要有奇数个1,不难发现此时恰好会改变该点,即令$a_{x}=a_{x}\oplus 1$ 称$\{ ...
- [atAGC052B]Tree Edges XOR
定义两点的距离$d(x,y)$为$x$到$y$路径上边权异或和,则两棵树相同当且仅当$\forall 1\le i\le n$,$d(1,i)$相同 新建一个节点0,连边$(0,1)$,初始权值为0, ...
- bzoj2006 [NOI2010]超级钢琴 (及其拓展)
bzoj2006 [NOI2010]超级钢琴 给定一个序列,求长度在 \([L,\ R]\) 之间的区间和的前 \(k\) 大之和 \(n\leq5\times10^5,\ k\leq2\times1 ...
- CF241B Friends
CF241B Friends 和Tree and Xor思路一样CF1055F Tree and XOR 直接找到第k大val,可以直接建出trie,然后按位贪心 考虑比val大的数的和 还是用b[i ...
- BZOJ3282: Tree
传送门 又是权限题= =,过了NOIp我就要去当一只权限狗! LCT裸题,get到了两个小姿势. 1.LCA操作应该在access中随时updata 2.Link操作可以更简单 void Link(i ...
- luogu P2574 XOR的艺术 (线段树)
luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...
- 1014: [JSOI2008]火星人prefix - BZOJ
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- Mail.Ru Cup 2018 Round 2 Solution
A. Metro Solved. 题意: 有两条铁轨,都是单向的,一条是从左往右,一条是从右往左,Bob要从第一条轨道的第一个位置出发,Alice的位置处于第s个位置,有火车会行驶在铁轨上,一共有n个 ...
随机推荐
- PLAY2.6-SCALA(十二) 表单的处理
一.表单处理流程如下 1.定义一个表单,在这里表单最多有22个字段 import play.api.data._ import play.api.data.Forms._ //要使用验证和约束 imp ...
- concurrent模块
concurrent包 concurrent.futrues模块 3.2版本引入 异步并行任务模块,提供一个高级的异步可执行的便利接口. 提供了两个池执行器 ThreadPoolExecutor异步调 ...
- cocos2dx3.0项目创建流程
cocos2dx3.0不是beta,新增了wp项目创建的支持 但不知道为啥非beta版本号的tools文件夹中取消了project-creator,可能有更改吧 没有这个工具还挺麻烦.就自己手动创建c ...
- IOS 后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务
我们知道,到我们程序从前台退到后台(安home)键后,将执行程序的委托方法. // 当应用程序掉到后台时,执行该方法 - (void)applicationDidEnterBackground:(UI ...
- SAS信用评分之番外篇异常值的识别
SAS信用评分之番外篇异常值的识别 今天想分享给大家的是我早期建模的时候一个识别异常值的办法,也许你在"信用风险评分卡研究"看过,但是代码只能识别一个变量,我将这个代码作了改良,但 ...
- python 并发之线程
一.什么是线程 #指的是一条流水线的工作过程,关键的一句话:一个进程内最少自带一个线程,其实进程根本不能执行,进程不是执行单位,是资源的单位,分配资源的单位 #线程才是执行单位 #进程:做手机屏幕的工 ...
- json 2016-09-18 22:03 207人阅读 评论(18) 收藏
JSON:JavaScript 对象表示法(JavaScript Object Notation) JSON是什么? JSON(JavaScript Object Notation) 是一种轻量级的数 ...
- LeetCode103 Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- docker images列出镜像
命令:docker images Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Sh ...
- UVa 623 大整数乘法
UVa 623 计算N! n上限为1000自然不能直接算.所以可以开一个数组f[],f[]每一位存N!结果的6位.如果按进制来理解,就是10^6进制: 例如 11!=39916800=11*10!=1 ...