2017 ICPC 西安站现场赛 A.XOR (线段树+线性基)

Posted hinata_hajime

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017 ICPC 西安站现场赛 A.XOR (线段树+线性基)相关的知识,希望对你有一定的参考价值。

 XOR

Consider an array A with n elements. Each of its element is A[i] (1 ≤ i ≤ n). Then gives two integers
Q, K, and Q queries follow. Each query, give you L, R, you can get Z by the following rules.
 To get Z, at first you need to choose  some elements from A[L] to A[R], we call them A[i1], A[i2],
. . . , A[it], Then you can get number Z = K or (A[i1], A[i2], . . . , A[it]).
   Please calculate the maximum Z for each query .

Input
Several test cases.
First line an integer T (1 ≤ T ≤ 10). Indicates the number of test cases.
Then T  test cases follows. Each test case begins with three integer N, Q, K (1 ≤ N ≤ 10000,1 ≤
Q ≤ 100000, 0 ≤ K ≤ 100000). The next line has N integers indicate A[1] to A[N] (0 ≤ A[i] ≤ 108).
Then Q lines, each line two integer L, R (1 ≤ L ≤ R ≤ N).

Output
For each query, print the answer in a single line.

Sample Input
1
5 3 0
1 2 3 4 5
1 3
2 4
3 5

Sample Output
3
7
7

 

题意:给出一个1个长度为n的数组A,然后给出q个询问对于每个询问,每次在下标为[l,r]的数中,选取一部分数,使得其异或值OR上k后最大,输出这个最大值

分析:根据题意,能够想到需要用到线性基。然后因为是要求最后OR上k后最大,根据OR运算的性质,可以先将数组中的数的二进制位上,将k为1的位置都变为0.因为这些位置在最后OR上k后都会变为1,所以暂时不讨论它。比如现在数x为5(101),k为4(100),那么需要将x变为1.

转换完成后,线性基的最大值OR上k就是答案,又因为题目是区间查询,所以需要用到线段树来维护线性基

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=1e4+100;
typedef long long LL;
LL t,n,q,k,l,r,pd;
int vis[35];

long long read(){
    long long res = 0;
    int flag = 0;
    char ch;
    if ((ch = getchar()) == -){
        flag = 1;
    }
    else if(ch >= 0 && ch <= 9){
        res = ch - 0;
    }
    while ((ch = getchar()) >= 0 && ch <= 9){
        res = res * 10 + (ch - 0);
    }

    return flag ? -res : res;
}

struct L_B{
    long long d[61];
    int cnt;
    L_B()
    {
        memset(d,0,sizeof(d));
        cnt=0;
    }
    void clear()
    {
        memset(d,0,sizeof(d));
        cnt=0;
    }
    void insert(long long val)
    {
        for (int i=31;i>=0;i--)
            if (val&(1LL<<i))
            {
                if (!d[i])
                {
                    d[i]=val;
                    break;
                }
                val^=d[i];
            }
    }
    long long query_max()
    {
        long long ret=0;
        for (int i=31;i>=0;i--)
        {
            if ((ret^d[i])>ret)
                 ret^=d[i];
        }
        return ret;
    }
};

L_B R;
L_B merge(const L_B &n1,const L_B &n2)
{
    L_B ret=n1;
    for (int i=31;i>=0;i--)
        if (n2.d[i])
            ret.insert(n2.d[i]);
    return ret;
}

struct node
{
    int l;
    int r;
    L_B A;
}tree[MAXN<<2];
void PushUp(int rt)
{
  tree[rt].A=merge(tree[rt<<1].A,tree[rt<<1|1].A);
}
void BuildTree(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    if(l==r)
    {
      LL x;
      x=read();
      x=x&pd;//转换后,再插入到线性基中
      tree[rt].A.clear();
      tree[rt].A.insert(x);
      return;
    }
    int mid=(tree[rt].l+tree[rt].r)/2;
    BuildTree(l,mid,rt<<1);
    BuildTree(mid+1,r,rt<<1|1);
    PushUp(rt);
}
void Query(int l,int r,int rt)
{
    if(tree[rt].l==l&&tree[rt].r==r)
    {
      R=merge(R,tree[rt].A);
      return;
    }
    int mid=(tree[rt].l+tree[rt].r)/2;
    if(r<=mid)Query(l,r,rt<<1);
    else if(l>mid)Query(l,r,rt<<1|1);
    else
    {
        Query(l,mid,rt<<1);
        Query(mid+1,r,rt<<1|1);
    }
}

int main()
{
    t=read();
    while(t--)
    {
        pd=0;
        memset(vis,0,sizeof(vis));
       n=read(),q=read(),k=read();
        for (int i=31;i>=0;i--)
        {
          if(k&(1LL<<i))
          vis[i]=1;
          else
          pd+=(1LL<<i);// 用来进行转换,pd&x即为转换后的值
        }
        BuildTree(1,n,1);
        while(q--)
        {
           l=read(),r=read();
            R.clear();
            Query(l,r,1);
            LL ans=(R.query_max()|k);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 

 

        

以上是关于2017 ICPC 西安站现场赛 A.XOR (线段树+线性基)的主要内容,如果未能解决你的问题,请参考以下文章

2014ACM/ICPC亚洲区西安站现场赛 F color(二项式反演)

2017 ICPC区域赛(西安站)--- J题 LOL(DP)

2017 icpc 西安网络赛

2017 ACM-ICPC 亚洲区(西安赛区)网络赛C. Sum脑洞题

2017 ACM-ICPC 亚洲区(西安赛区)网络赛

2017 ACM-ICPC 亚洲区(西安赛区)网络赛: B. Coin 概率题