首页 > 其他分享 >选数异或

选数异或

时间:2023-01-25 11:00:39浏览次数:63  
标签:int 选数 pos 用例 异或 maxn Left

问题描述

给定一个长度为 �n 的数列 �1,�2,⋯,��A1​,A2​,⋯,An​ 和一个非负整数 �x, 给定 �m 次查 询, 每次询问能否从某个区间 [�,�][l,r] 中选择两个数使得他们的异或等于 �x 。

输入格式

输入的第一行包含三个整数 �,�,�n,m,x 。

第二行包含 �n 个整数 �1,�2,⋯,��A1​,A2​,⋯,An​ 。

接下来 �m 行,每行包含两个整数 ��,��li​,ri​ 表示询问区间 [��,��][li​,ri​] 。

输出格式

对于每个询问, 如果该区间内存在两个数的异或为 �x 则输出 yes, 否则输出 no。

样例输入

4 4 1
1 2 3 4
1 4
1 2
2 3
3 3

样例输出

yes
no
yes
no

样例说明

显然整个数列中只有 22, 33 的异或为 11。

评测用例规模与约定

对于 2020 的评测用例, 1≤�,�≤1001≤n,m≤100;

对于 4040 的评测用例, 1≤�,�≤10001≤n,m≤1000;

对于所有评测用例, 1≤�,�≤100000,0≤�<220,1≤��≤��≤�1≤n,m≤100000,0≤x<220,1≤li​≤ri​≤n, 0≤��<2200≤Ai​<220 。

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 512M
 一般查询(最后几组会超时)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int Left[maxn], pos[(1 << 20) + 10];
int a[maxn], n, m, x;

int main()
{
    cin >> n >> m >> x;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        Left[i] = pos[a[i] ^ x];
        pos[a[i]] = i;
    }
    while(m--)
    {
        int l, r;
        cin >> l >> r;
        int t=0;
        for(int i=l;i<=r;i++)
        {
            if(Left[i]>t)
            t=Left[i];
        }
        if(t>= l)
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }
    
    return 0;
}

线段树查询

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int tree[maxn << 2];
int Left[maxn], pos[(1 << 20) + 10];
int a[maxn], n, m, x;

//线段树模板
//1 1 n
void build(int o, int l, int r)
{
    if(l == r)
    {
        tree[o] = Left[l];
        cout<<"tree["<<o<<"]"<<"=Left["<<l<<"]"<<endl;
        return;
    }
    int mid = (l + r) >> 1;
    
    build(o << 1, l, mid);
    cout<<"build("<<(o<<1)<<","<<l<<","<<mid<<")"<<endl;
    
    build(o << 1 | 1, mid + 1, r);
    cout<<"build("<<(o<<1|1)<<","<<mid+1<<","<<r<<")"<<endl;
    
    tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
    cout<<"tree["<<o<<"]"<<"=max(tree["<<(o<<1)<<"],tree["<<(o<<1|1)<<"])"<<endl;
}
//查询区间[L,R]的最大值
//1 1 n l r
int query(int o, int l, int r, int L, int R)
{
    if(L <= l && r <= R)return tree[o];
    int mid = (l + r) >> 1;
    int ans = 0;
    if(L <= mid)ans = max(ans, query(o << 1, l, mid, L, R));
    if(R > mid)ans = max(ans, query(o << 1 | 1, mid + 1, r, L, R));//找最大的left[idx] 
    return ans;
}

int main()
{
    cin >> n >> m >> x;
    for(int i = 1; i <= n; i++) //预处理Left数组
    {
        cin >> a[i];
        Left[i] = pos[a[i] ^ x];
        pos[a[i]] = i;
    }
    build(1, 1, n);//线段树建树
    while(m--)
    {
        int l, r;
        cin >> l >> r;
        if(query(1, 1, n, l, r) >= l)//查询区间最值
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }
    cout<<"tree\n";
    for(int i=0;i<=8;i++)
    {
        cout<<tree[i]<<" ";
    }
    return 0;
}

 

 

标签:int,选数,pos,用例,异或,maxn,Left
From: https://www.cnblogs.com/weinan030416/p/17066751.html

相关文章

  • 选数异或
    问题描述给定一个长度为 �n 的数列 �1,�2,⋯,��A1​,A2​,⋯,An​ 和一个非负整数 �x,给定 �m 次查询,每次询问能否从某个区间 [�,�][l,r] 中选择两个数使得他们的异或......
  • P3226 [HNOI2012]集合选数
    简要题意给你一个\(n\)个元素的集合,它由前\(n\)个正整数构成。你需要求出它有多少个非空子集,满足若\(x\)在这个子集中,\(2x,3x\)不能在子集中。由于答案可能很大,你......
  • logsim 异或
    ~ab+~ba​​视屏链接​​......
  • 洛谷 P1036 选数
    原题链接题解:#include"iostream"#include"algorithm"#definelllonglongusingnamespacestd;llsum=0;boolprime(llx){intn=2;for(;x%n!=0;n++)......
  • c#同或,异或
                                                         ......
  • 区间异或和异或区间最大值异或区间最小值
    区间异或和异或区间最大值异或区间最小值关键分治思想。也可以选择固定左端点,然后选择右端点代码#include<bits/stdc++.h>usingnamespacestd;constintinf=0x3f......
  • 最大异或和
    最大异或和关键1.开头需要加个02.l和r这里需要减去1代码#include<bits/stdc++.h>usingnamespacestd;constintM=6e5+5;introt[M],cnt[M*24],ch[M*24][2],tot......
  • LeetCode刷题(44)~缺失数字【位运算:异或 】
    题目描述给定一个包含0,1,2,…,n中n个数的序列,找出0…n中没有出现在序列中的那个数。示例1:输入:[3,0,1]输出:2示例2:输入:[9,6,4,2,3,5,7,0,1]输出:8说......
  • LeetCode刷题(43)~汉明距离【异或+布赖恩·克尼根算法】
    题目描述两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。给出两个整数x和y,计算它们之间的汉明距离。注意:0≤x,y<231.示例:输入:x=1,y......
  • LeetCode刷题(81)~数组中数字出现的次数【分组异或】
    题目描述一个整型数组nums里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。示例1:输入:nums=[4,1,......