CodeForces 242E XOR on Segment 二维线段树

题目链接:http://codeforces.com/problemset/problem/242/E

题意:
给定一个长度为n的数组,然后有两种操作。
1:查询操作,求区间[l,r]的值的和;
2:更新操作,区间[l,r]内的每个值与x异或。

分析:
很显然,要用线段树,可是更新时,怎么维护呢?求和很简单,难就难在更新,因为是异或操作,不同于加减操作,可以直接用一维维护。异或操作,需要转换为二进制来计算,那么就再开一维用来记录每个数的二进制情况,然后更新时,只用看对应于x的二进制位1的位置更新,更新时也只需0变1,1变0。就整段区间而言,进行二进制下不进位加法,可以很清晰的记录该区间内每个位置上有多少1。

源代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<cmath>
#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100005;
typedef __int64 LL;
const LL MOD=1000000007;

int tre[N<<2][22],laz[N<<2][22];

void build(int o,int l,int r)
{
    if(l==r)
    {

        int x;
        scanf("%d",&x);
        for(int i=0; i<20; i++)
        {
            if(x&(1<<i))
            {
                tre[o][i]=1;
                laz[o][i]=0;
            }
        }
        return;
    }
    int mid=(l+r)/2;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
    for(int i=0; i<20; i++)
        tre[o][i]=tre[o<<1][i]+tre[o<<1|1][i];
}

struct Node
{
    int a[22];
};


void pushdown(int o,int mm)
{
    for(int i=0; i<20; i++)
    {
        if(laz[o][i])
        {
            tre[o<<1][i]=(mm-mm/2)-tre[o<<1][i];
            tre[o<<1|1][i]=mm/2-tre[o<<1|1][i];
            laz[o<<1][i]=1-laz[o<<1][i];
            laz[o<<1|1][i]=1-laz[o<<1|1][i];
            laz[o][i]=0;
        }
    }
}


void update(int o,int l,int r,int x,int y,int c)
{
    if(x<=l&&y>=r)
    {
        for(int i=0; i<20; i++)
        {
            if(c&(1<<i))
            {
                tre[o][i]=r-l+1-tre[o][i];
                laz[o][i]=1-laz[o][i];
            }
        }
        return;
    }
    pushdown(o,r-l+1);
    int mid=(l+r)/2;
    if(x<=mid)
        update(o<<1,l,mid,x,y,c);
    if(y>mid)
        update(o<<1|1,mid+1,r,x,y,c);
    for(int i=0; i<20; i++)
        tre[o][i]=tre[o<<1][i]+tre[o<<1|1][i];
}

Node query(int o,int l,int r,int x,int y)
{
    Node h;
    memset(h.a,0,sizeof(h.a));
    if(x<=l&&y>=r)
    {
        for(int i=0; i<20; i++)
        {
            h.a[i]=tre[o][i];
        }
        return h;
    }
    pushdown(o,r-l+1);
    int mid=(l+r)/2;
    if(x<=mid)
    {
        Node p=query(o<<1,l,mid,x,y);
        for(int i=0; i<20; i++)
            h.a[i]+=p.a[i];

    }
    if(y>mid)
    {
        Node p=query(o<<1|1,mid+1,r,x,y);
        for(int i=0; i<20; i++)
            h.a[i]+=p.a[i];

    }

    return h;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(laz,0,sizeof laz);
        memset(tre,0,sizeof tre);
        build(1,1,n);
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int op,l,r;
            scanf("%d%d%d",&op,&l,&r);
            if(op==1)
            {
                Node h=query(1,1,n,l,r);
                LL ans=0;
                for(int i=0; i<20; i++)
                {
                    ans+=(LL)h.a[i]*(1<<i);
                }
                printf("%I64d\n",ans);
            }
            else if(op==2)
            {
                int c;
                scanf("%d",&c);
                update(1,1,n,l,r,c);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值