CodeForces 587 E.Duff as a Queen(线性基+线段树+树状数组)

59 篇文章 0 订阅
54 篇文章 0 订阅

Description

给出一个长度为 n 的序列ai,有两种操作

1 l r k : 表示将 al,al+1,...,ar 每一个数都异或上 k

2 l r: 查询 al,al+1,...,ar 这些数选取子集异或可以得到的不同数字个数

Input

第一行两整数 n,q 表示序列长度和操作数,之后输出 n 个整数ai表示该序列,最后 q 行每行一个操作

(1n2105,1q4104,0ai,k109)

Output

对于每个查询操作,输出结果

Sample Input

5 5
1 2 3 4 2
2 1 5
1 2 2 8
2 1 5
1 1 3 10
2 2 2

Sample Output

8
16
1

Solution

对于查询,只要知道该区间数字的线性基,设其维数为 m ,则答案即为2m,问题在于区间更新之后线性基完全改变,故考虑 b 序列,其中bi=ai^ ai+1 ,一个显然的结论是 al,al+1,...,ar 所能异或得到的数字集合和 al,bl,bl+1,...,br1 所能异或得到的数字集合相等,而对 a 序列的[l,r]区间更新对 b 序列的影响只有bl1,br,故只要用线段树维护 b 序列的区间线性基,支持单点修改区间查询即可,然后用树状数组维护对a序列的修改(类似区间加的前缀和优化操作),每次查询只需要在线段树中查询 b 序列属于区间[l,r1]的线性基,然后在树状数组中求一个前缀和得到对 al 的累计修改值异或原先的值得到最新的 al 值将其加到线性基中即可

Code

#include<cstdio>
#include<cstring>
using namespace std;
namespace fastIO 
{
    #define BUF_SIZE 100000
    //fread -> read
    bool IOerror=0;
    inline char nc() 
    {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) 
        {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) 
            {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) 
    {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    inline void read(int &x) 
    {
        char ch;
        while(blank(ch=nc()));
        if(IOerror)return;
        for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
    }
    #undef BUF_SIZE
};
using namespace fastIO;
const int maxn=200005;
#define ls (t<<1)
#define rs ((t<<1)|1)
int base[maxn<<2][30],num[maxn<<2],n,q,a[maxn],b[maxn],A[66],B[31];
void Unite(int x,int y,int z)
{
    int res=0;
    for(int i=1;i<=num[x];i++)A[res++]=base[x][i];
    for(int i=1;i<=num[y];i++)A[res++]=base[y][i];
    memset(B,0,sizeof(B));
    for(int i=0;i<res;i++)
        for(int j=29;j>=0;j--)
            if(A[i]>>j&1)
            {
                if(!B[j])
                {
                    B[j]=A[i];
                    break;
                }
                else A[i]^=B[j];
            }
    num[z]=0;
    for(int i=0;i<=29;i++)
        if(B[i])base[z][++num[z]]=B[i];
}
void build(int l,int r,int t)
{
    if(l==r)
    {
        if(b[l])base[t][++num[t]]=b[l];
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,ls);build(mid+1,r,rs);
    Unite(ls,rs,t); 
}
void update(int x,int l,int r,int t,int val)
{
    if(l==r)
    {
        base[t][1]^=val;
        num[t]=(base[t][1]>0);
        return ;
    }
    int mid=(l+r)/2;
    if(x<=mid)update(x,l,mid,ls,val);
    else update(x,mid+1,r,rs,val);
    Unite(ls,rs,t);
}
void query(int L,int R,int l,int r,int t)
{
    if(L>R)return ;
    if(L<=l&&r<=R)
    {
        Unite(0,t,0);
        return ;
    }
    int mid=(l+r)/2;
    if(L<=mid)query(L,R,l,mid,ls);
    if(R>mid)query(L,R,mid+1,r,rs);
}
#define lowbit(x) (x&(-x))
int bit[maxn];
void add(int x,int val)
{
    while(x<=n)
    {
        bit[x]^=val;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int ans=0;
    while(x)
    {
        ans^=bit[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    read(n);read(q);
    //scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)read(a[i]);//scanf("%d",&a[i]);
    for(int i=1;i<n;i++)b[i]=a[i]^a[i+1];
    if(n>1)build(1,n-1,1);
    while(q--)
    {
        int op,l,r,k;
        read(op);read(l);read(r);
        //scanf("%d%d%d",&op,&l,&r);
        if(op==1)
        {
            read(k);//scanf("%d",&k);
            if(l>1)update(l-1,1,n-1,1,k);
            if(r<n)update(r,1,n-1,1,k);
            add(l,k);add(r+1,k);
        }
        else
        {
            num[0]=0;
            int temp=sum(l)^a[l];
            if(temp)base[0][++num[0]]=temp;
            if(n>1)query(l,r-1,1,n-1,1);
            printf("%d\n",1<<num[0]);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值