首页 > 其他分享 >Codeforces Round #779 D

Codeforces Round #779 D

时间:2022-08-25 21:44:51浏览次数:66  
标签:xor int 779 Codeforces son -- const Round define

D1

我们先来看D1啊 我最开始理解的就是一个翻转 但是只在0开始时才是正确的
这里就有一组hack 1 2
0 3
他会输出0 而不是3 为啥??? 这样一想好像是正确的 每次要是01数字不同了这一位就是1 要是不一样的话可记可不记
但是这是对于从0到r 来说 因为我们的r每次不记录 都有一个类似与互补的数来帮他 而l不为0之后就没有了
就比如说一个0到x的一个数组 你xor任何数他都是连续的 而l不等于0之后 就会有间断的
code:

#include <bits/stdc++.h>
using namespace std;
const int N =5e5+10;
const int M = 998244353;
const int mod = 998244353;
#define int long long
#define endl '\n'
#define Endl '\n'
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define inf 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
const int MAXN = 5e5 + 5;
int son[MAXN*30][2], idx;
int MAXBIT = 20,val[MAXN];
void init(){
    son[0][0] = son[0][1] = 0;
    idx = 1;
}
void add(int n){
    int p = 0;
    for (int i = MAXBIT; i >= 0; --i)
    {
        int u = (n >> i) & 1;
        if (!son[p][u]) {
            son[idx][0] = son[idx][1] = 0;
            son[p][u] = idx++;
        }
        p = son[p][u];
    }
    val[p] = n;
}
int query_min(int x){
    int p=0;
    for(int i=MAXBIT;i>=0;i--){
        int u=x>>i&1;
        if(son[p][u])p=son[p][u];
        else p=son[p][u^1];
    }
    return val[p]^x;
}
int query_max(int x){
    int p=0;
    for(int i=MAXBIT;i>=0;i--){
        int u=x>>i&1;
        if(son[p][u^1])p=son[p][u^1];
        else p=son[p][u];
    }
    return val[p]^x;
}
void solve() {
    init();
    int l,r;cin>>l>>r;
    int n=r-l+1;
    vector<int>a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
        add(a[i]);
    }
    for(int i=0;i<n;i++){
        int x=a[i]^l;
        if(query_min(x)==l&&query_max(x)==r){
            cout<<x<<endl;
            break;
        }
    }
}
signed main(){
    fast
    int T;cin>>T;
    while(T--) {
        solve();
    }
    return ~~(0^_^0);
}

D2

然后我们来看D2 要是没了如上性质 我们该如何做
我们考虑把a[i] 插入 建立一颗01trie
然后枚举x跑一边最大值最小值是否为l r即可
为啥这样就对了
因为不同数xor同一个数 结果都是不同的 可以考虑有一位不同 不管x为啥 他们最后都是不同的
又因为a[i]不同 a[i]再xor x 一遍 也是不同的
可是这样是n^2的
然后我们看到了数组长度不超过2^17 那我们变形一下即可 -> a[i] xor x = l 两边xor x xor l -> a[i] xor l = x ;
那我们枚举a[i]即可

#include <bits/stdc++.h>
using namespace std;
const int N =5e5+10;
const int M = 998244353;
const int mod = 998244353;
#define int long long
#define endl '\n'
#define Endl '\n'
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define inf 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
const int MAXN = 5e5 + 5;
int son[MAXN*30][2], idx;
int MAXBIT = 20,val[MAXN];
void init(){
    son[0][0] = son[0][1] = 0;
    idx = 1;
}
void add(int n){
    int p = 0;
    for (int i = MAXBIT; i >= 0; --i)
    {
        int u = (n >> i) & 1;
        if (!son[p][u]) {
            son[idx][0] = son[idx][1] = 0;
            son[p][u] = idx++;
        }
        p = son[p][u];
    }
    val[p] = n;
}
int query_min(int x){
    int p=0;
    for(int i=MAXBIT;i>=0;i--){
        int u=x>>i&1;
        if(son[p][u])p=son[p][u];
        else p=son[p][u^1];
    }
    return val[p]^x;
}
int query_max(int x){
    int p=0;
    for(int i=MAXBIT;i>=0;i--){
        int u=x>>i&1;
        if(son[p][u^1])p=son[p][u^1];
        else p=son[p][u];
    }
    return val[p]^x;
}
void solve() {
    init();
    int l,r;cin>>l>>r;
    int n=r-l+1;
    vector<int>a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
        add(a[i]);
    }
    for(int i=0;i<n;i++){
        int x=a[i]^l;
        if(query_min(x)==l&&query_max(x)==r){
            cout<<x<<endl;
            break;
        }
    }
}
signed main(){
    fast
    int T;cin>>T;
    while(T--) {
        solve();
    }
    return ~~(0^_^0);
}

标签:xor,int,779,Codeforces,son,--,const,Round,define
From: https://www.cnblogs.com/ycllz/p/16625854.html

相关文章

  • Educational Codeforces Round 106 (Rated for Div. 2) | CF1499
    E一个暴力是显然的,\(f(i,j,k)\)表示当前已经使用\(a\)的前\(i\)位,\(b\)的前\(j\)位,最后一位是\(a\)还是\(b\)的。然后\(O(n^2)\)枚举起点跑下去即可。为啥......
  • Codeforces Round #770 (Div. 2)
    CodeforcesRound#770(Div.2)VPABCDE7min30min63min131min+2+2排名:rk485基准分:\(\color{Purple}{1967}\)A\(\color{Gray}{800}\)CF1......
  • Codeforces Round #772 (Div. 2)
    CodeforcesRound#772(Div.2)VPABC3min12min52min+4排名:rk3893基准分:\(\color{ForestGreen}{1362}\)从天选到天崩A\(\color{Gray}{800}\)......
  • Codeforces Round #815 (Div. 2)
    https://codeforces.ml/contest/1720A:思维fst了。。分数,分子分母改变多少次,变一样题意:给a/b,c/d两个分数,问分子父母各乘多少次可以得到相同的数思路很简单,将所有数......
  • Codeforces Round #710 (Div. 3) D. Epic Transformation(优先队列)
    https://codeforces.com/contest/1506/problem/D鉴定完毕,这题卡映射数量到优先队列的那一步操作给你一个由整数组成的长度为n的数组a。您可以对阵列应用由几个步骤组成......
  • Codeforces Round #773 (Div. 2)
    CodeforcesRound#773(Div.2)VPABC24min31min48min+2+1A\(\color{Gray}{800}\)CF1642AHardWay观察题目样例外加手摸可知,只有满足三角形......
  • Codeforces Round #815 (Div. 2) E. Misha and Paintings
    人生中第一个AC的codeforces题,大概太难了,光是看答案就看了整整一下午,最后还是在b站上搜到讲解视频才明白的。俺们阿B真的是太厉害啦这道题首先容易看出当矩阵中数字个数......
  • Codeforces Round #783 (Div. 2) E (证明+构造)
    一般这种题我们都是先推导下界再来构造那我们假设我们当前放置了k位半皇后我们只考虑横竖被吃掉并且贪心的(类似于八皇后的选择)横竖都不重叠我们把他固定在左上角的kk......
  • Codeforces Round #292 (Div. 2) C. Drazil and Factorial(思维)
    https://codeforces.com/contest/515题目大意:给我们一个长度为n的数字a定义F(a)=a里面每一位数的阶层总乘积让我们求最大的x,需要满足F(x)=f(a)并且x中没有0和1input4......
  • Codeforces Round #638 (Div. 2) B. Phoenix and Beauty(构造/思维)
    https://codeforces.com/contest/1348/problem/B如果一个数组的所有长度为k的子数组的和相同,那么这个数组就是美丽的。数组的子数组是任何连续元素的序列。Phoenix目前......