2019ICPC南昌邀请赛正赛 F(规律+树状数组维护动态区间异或和)

题目地址

这题的推导请看 其实就是网络赛的那题的弱化版
(感谢这位大佬的博客)

也可以打个表看看,虽然打表的话有点难看出规律

然后很明显, 查询的区间长度为偶数的时候,答案永远是0,如果是奇数,那么查询的区间左端点和右端点一定是奇偶性相同。
答案就是 下标间隔2的数进行异或。
那么我们就可以维护两个树状数组,分别表示下标为奇数的数和下标为偶数的数的前缀异或和。

然后注意一下细节

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector> 
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;  
#define debugint(name,x) printf("%s: %d\n",name,x);
#define debugstring(name,x) printf("%s: %s\n",name,x);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 1e5+5;
const int mod = 1e9+7;
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 _,n,m,a[maxn],b[maxn];
int c1[maxn],c2[maxn];
int t1,t2;
void update(int x,int val,int c[]){
    while(x < maxn){
        c[x] ^= val;
        x += lowbit(x);
    }
}
int query(int x,int c[]){
    int res = 0;
    while(x > 0){
        res ^= c[x];
        x -= lowbit(x);
    }
    return res;
}
int main() {
    int ca = 1;
    for(scanf("%d",&_);_;_--){
        scanf("%d%d",&n,&m);
        int t1 = 0,t2 = 0,x;
        for(int i = 1; i <= n; i++){
            scanf("%d",&x);
            if(i&1) a[++t1] = x,update(t1,x,c1);
            else b[++t2] = x,update(t2,x,c2);
        }
       /* build1(1,t1,1);
        for(int i = 1; i <= t1; i++)
            printf("%d ",sum1[i]);
        build2(1,t2,1);*/
        printf("Case #%d:\n",ca++);
        while(m--){
            int op,l,r;
            scanf("%d%d%d",&op,&l,&r);
            if(op == 0){
                if(l&1){
                    l = (l+1)/2;
                    r = query(l,c1)^query(l-1,c1)^r;
                    update(l,r,c1);
                }
                else{
                    l = l/2;
                    r = query(l,c2)^query(l-1,c2)^r;
                    update(l,r,c2);
                }
            }else{
                if((r-l+1)%2==0) puts("0");
                else{
                    if(l&1)
                        printf("%d\n",query((l+1)/2-1,c1)^query((r+1)/2,c1));
                    else
                        printf("%d\n",query(l/2-1,c2)^query(r/2,c2));
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值