ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

I - Vitya and Strange Lesson CodeForces - 842D

2022-04-02 20:01:08  阅读:150  来源: 互联网

标签:ll 异或 son 842D Strange 序列 Lesson include define


 

【题目意思】:

  找出修改后的序列中,没出现的最小正整数。

  修改操作是 将x与序列中所有数异或。(每次异或之后替代原序列的值)。

【解题思路】:

  题目要求是找出不在序列里的的最小值。而字典树正好是解决异或最值问题的,所以我们将不在序列里的所有数放进字典树里面,进行异或操作,求出其中的最小值。

  字典树求最小值 是 从最高位开始,判断每一位(2进制)上的值是否与自身在这一位上的值相等(异或是相等为0,不相等为1),如果相等,则走下去,如果不相等,只能走那一条路。

  如何存储异或后的序列:

    根据异或的性质。 a⊕b = c => a = b⊕c => b = a ⊕ c ;

    所以记录异或后的序列,只要将每次的x异或起来,在与序列异或。

【代码】:

 

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>

#define ms(a, b) memset(a,b,sizeof(a))
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define rep(i, a, b)  for(ll i=a;i<=b;i++)
#define lep(i, a, b)  for(ll i=a;i>=b;i--)
#define endl '\n'
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi  vector<ll>
#define vpi vector<pii>
#define vpl vector<pll>
#define mi  map<ll,ll>
#define all(a)  (a).begin(),(a).end()
#define gcd __gcd
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound

#define ff first
#define ss second
#define test4(x, y, z, a) cout<<"x is "<<x<<"        y is "<<y<<"        z is "<<z<<"        a is "<<a<<endl;
#define test3(x, y, z) cout<<"x is "<<x<<"        y is "<<y<<"        z is "<<z<<endl;
#define test2(x, y) cout<<"x is "<<x<<"        y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
using namespace std;
const int N = 3e5 + 10;
const int maxx = 0x3f3f3f;
const int mod = 1e9 + 7;
const int minn = -0x3f3f3f;
const int M = 2 * N;
ll T, n, m;
ll a[N];
ll son[N*32][2],idx,cnt[N*32];
ll vis[N*2 ];
void insert(ll n ){
    int p=0;
    for ( int i=32;i>=0;i--){
        int u = (n>>i)&1 ;
        if ( !son[p][u] )   son[p][u] = ++idx;
        p = son[p][u];
    }
    cnt[p] = n;
}
ll query(ll n){
    int p = 0;
    for ( int i=32;i>=0;i--){
        int u = (n>>i)&1;
        if ( son[p][u] ){
             p = son[p][u];
        }else{
            p = son[p][u^1];
        }
    }
    return n^cnt[p];
}
void solve() {
    cin>>n>>m;
    rep(i,1,n)  {cin>>a[i]; vis[a[i]]=1 ;}
    rep(i,0,2*N){
        if ( !vis[i] ) {
            insert(i);
        }
    }
    ll h = 0;
    ll x;
    rep(i,1,m){
        cin>>x;
        h = h^x;
        cout<<query(h)<<endl;
    }
}

int main() {
    fast;
    solve();
    return 0;
}
View Code

 

标签:ll,异或,son,842D,Strange,序列,Lesson,include,define
来源: https://www.cnblogs.com/opanc/p/16093732.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有