当前位置:网站首页>Codeforces Round #765 (Div. 2) D. Binary Spiders
Codeforces Round #765 (Div. 2) D. Binary Spiders
2022-06-26 13:58:00 【__ LazyCat__】
Any XOR greater than or equal to k
link :Problem - D - Codeforces
The question : Given n Number , Find the largest set , Make the XOR of any two numbers in the set greater than or equal to k, Find the largest set and output the subscript of the number in the set .
Answer key : First , about k k k highest 1 1 1 Bits above , The XOR of any two higher bits that are different must be greater than k k k Of , So you can put all the numbers in order first , From the highest to k k k Highest bit enumeration , In this case, the numbers with different bit states during enumeration can be divided into two sets , How to choose the number in two sets will not make the number between two sets different or produce less than k k k Number of numbers . But to k k k be equal to 1 1 1 when , The number in the current set can only be selected at most 2 2 2 The number of current binary bits in different states ensures that the current bit XOR is 1 1 1 , But even if XOR is 1 1 1 There is no guarantee that greater than k k k, So we can traverse the maximum XOR number of each number in this interval , If there is more than k k k Then choose these two numbers , If not, just choose one ( Only when the rest of the intervals have access to data, you can choose any one to ensure that the number exclusive or with the rest of the intervals is greater than or equal to k k k). As for how to find the number with the largest XOR value between a number and a number in a certain interval , It can be persistent 01 t r i e 01trie 01trie For maintenance , Of course, you can also build a tree for each searched interval 01 t r i e 01trie 01trie , However, it is not recommended to build trees in search .
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<functional>
#include<queue>
#include<unordered_map>
#include<map>
#include<set>
using namespace std;
using ll=long long;
using P=pair<int,int>;
const ll inf=1e18;
const int maxn=3e5+5;
struct trie{
int now=1;
int t[maxn*40][2],val[maxn*40],ct[maxn*40],rt[maxn];
inline void insert(int a,int b,int x,int y)
{
rt[b]=++now;
int l=rt[a],r=rt[b];
for(int i=30;i>=0;i--)
{
int p=x>>i&1;
ct[r]=ct[l]+1;
t[r][p^1]=t[l][p^1];
t[r][p]=++now;
l=t[l][p],r=t[r][p];
}
val[r]=y,ct[r]=ct[l]+1; return;
}
inline int query(int l,int r,int x,int k)
{
l=rt[l],r=rt[r];
for(int i=30;i>=0;i--)
{
int p=x>>i&1;
if(k>>i&1)
{
if(ct[t[r][p^1]]-ct[t[l][p^1]])
{
l=t[l][p^1],r=t[r][p^1];
}
else return 0;
}
else
{
if(ct[t[r][p^1]]-ct[t[l][p^1]])
{
r=t[r][p^1],l=t[l][p^1];
for(int j=i-1;j>=0;j--)
{
if(ct[t[r][p^1]]-ct[t[l][p^1]])
{
r=t[r][p^1],l=t[l][p^1];
}
else
{
r=t[r][p],l=t[l][p];
}
}
break;
}
else r=t[r][p],l=t[l][p];
}
}
return val[r];
}
}t;
void solve()
{
int n,k; cin>>n>>k;
vector<int>ans;
vector<P>a(n+1);
for(int i=1;i<=n;i++)
{
cin>>a[i].first,a[i].second=i;
}
sort(a.begin()+1,a.end());
for(int i=1;i<=n;i++)
{
t.insert(i-1,i,a[i].first,a[i].second);
}
function<void(int,int,int,int,bool)>dfs=[&](int l,int r,int sum,int dep,bool lim)
{
if(l>r)return;
if(k>>dep&1)
{
int flag=0;
for(int i=l;i<=r;i++)
{
int p=t.query(l-1,r,a[i].first,k);
if(p)
{
ans.push_back(a[i].second);
ans.push_back(p);
flag=1; break;
}
}
if(!flag&&lim)ans.push_back(a[l].second);
}
else
{
int p=lower_bound(a.begin()+l,a.begin()+1+r,P(sum+(1<<dep),0))-a.begin();
dfs(l,p-1,sum,dep-1,p<=r||lim);
dfs(p,r,sum+(1<<dep),dep-1,l<p||lim);
}
return;
};
if(k)
{
dfs(1,n,0,30,0);
if(!ans.size()&&a[n].first>=k)
{
ans.push_back(a[n].second);
}
if(ans.size())
{
cout<<ans.size()<<"\n";
for(auto i:ans)cout<<i<<" ";
}
else cout<<-1;
}
else
{
cout<<n<<"\n";
for(int i=1;i<=n;i++)cout<<i<<" ";
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t=1; //cin>>t;
while(t--)solve();
return 0;
}
边栏推荐
- 7-2 picking peanuts
- Input text to automatically generate images. It's fun!
- Embedded virlog code running process
- AGCO AI frontier promotion (6.26)
- Awk tools
- Linear basis
- Use performance to see what the browser is doing
- [jsoi2015] string tree
- Es common grammar I
- Zero basics of C language lesson 7: break & continue
猜你喜欢

Es snapshot based data backup and restore

NVM installation tutorial

Detailed sorting of HW blue team traceability process

Wechat applet Registration Guide

网络远程访问的方式使用树莓派

Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value

Included angle of 3D vector

程序员必备,一款让你提高工作效率N倍的神器uTools

Mongodb series window environment deployment configuration

hands-on-data-analysis 第三单元 模型搭建和评估
随机推荐
Taishan Office Technology Lecture: four cases of using bold font
Wechat applet - bind and prevent event bubble catch
ICML 2022 | limo: a new method for rapid generation of targeted molecules
计算两点之间的距离(二维、三维)
[path of system analyst] Chapter 15 double disk database system (database case analysis)
7-1 n queen problem
团队管理的最关键因素
去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程
windows版MySQL软件的安装与卸载
输入文本自动生成图像,太好玩了!
A primary multithreaded server model
虫子 类和对象 上
C language ---getchar() and putchar()
李航老师新作《机器学习方法》上市了!附购买链接
Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
What is the use of index aliases in ES
网络远程访问的方式使用树莓派
es常用语法一
Wechat applet Registration Guide
Mongodb series window environment deployment configuration