Operation

题目描述

There is an integer sequence a of length n and there are two kinds of operations:
0 l r: select some numbers from al...ar so that their xor sum is maximum, and print the maximum value.
1 x: append x to the end of the sequence and let n=n+1.

 

输入

There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.
For each test case: 
The first line contains two integers n,m(1≤n≤5×105,1≤m≤5×105), the number of integers initially in the sequence and the number of operations.
The second line contains n integers a1,a2,...,an(0≤ai<230), denoting the initial sequence.
Each of the next m lines contains one of the operations given above.
It's guaranteed that ∑n≤106,∑m≤106,0≤x<230.
And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero: 
For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.
For every type 1 operation, let x=x xor lastans.

 

 

输出

For each type 0 operation, please output the maximum xor sum in a single line.

 

样例输入

复制样例数据

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

样例输出

1
3

线性基模板题

https://blog.csdn.net/a_forever_dream/article/details/83654397

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int p=998244353;
const int maxn=1000050;
#define lowbit(x)  x&(-x)
#define lson root << 1
#define rson root << 1 | 1
#define INF 0x3f3f3f3f
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
ll qmod(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%mod;
        }
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
int a[maxn],b[32],pos[32],base[maxn][32],las[maxn][32];
bool add(int val,int pp)
{
    for(int i=30; i>=0; i--)
    {
        if(val&(1ll<<i))
        {
            if(!b[i])
            {
                pos[i]=pp;
                b[i]=val;
                break;
            }
            if(pos[i]<pp)
            {
                swap(b[i],val);
                swap(pos[i],pp);
            }
            val^=b[i];
        }
    }
    return val>0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(b,0,sizeof(b));
        memset(pos,0,sizeof(pos));
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            add(a[i],i);
            for(int j=30; j>=0; j--)
            {
                base[i][j]=b[j];
                las[i][j]=pos[j];
            }
        }
        int lastans=0;
        int op,l,r,x;
        while(m--)
        {
            scanf("%d",&op);
            if(op==1)
            {
                ++n;
                scanf("%d",&x);
                x^=lastans;
                add(x,n);
                for(int j=30; j>=0; j--)
                {
                    base[n][j]=b[j];
                    las[n][j]=pos[j];
                }
            }
            else
            {
                scanf("%d %d",&l,&r);
                l=(l^lastans)%n+1;
                r=(r^lastans)%n+1;
                if(l>r)
                {
                    swap(l,r);
                }
                lastans=0;
                for(int i=30; i>=0; i--)
                {
                    if(las[r][i]>=l&&(lastans^base[r][i])>lastans)
                    {
                        lastans^=base[r][i];
                    }
                }
                printf("%d\n",lastans);
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值