CF 242E-XOR on Segment(线段树区间处理+二进制)

Description

You've got an array a, consisting of n integers a1, a2, ..., an. You are allowed to perform two operations on this array:

  1. Calculate the sum of current array elements on the segment [l, r], that is, count value al + al + 1 + ... + ar.
  2. Apply the xor operation with a given number x to each array element on the segment [l, r], that is, execute . This operation changes exactly r - l + 1 array elements.

Expression means applying bitwise xor operation to numbers x and y. The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as "^", in Pascal — as "xor".

You've got a list of m operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 106) — the original array.

The third line contains integer m (1 ≤ m ≤ 5·104) — the number of operations with the array. The i-th of the following m lines first contains an integer ti (1 ≤ ti ≤ 2) — the type of the i-th query. If ti = 1, then this is the query of the sum, if ti = 2, then this is the query to change array elements. If the i-th operation is of type 1, then next follow two integers li, ri (1 ≤ li ≤ ri ≤ n). If the i-th operation is of type 2, then next follow three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106). The numbers on the lines are separated by single spaces.

Output

For each query of type 1 print in a single line the sum of numbers on the given segment. Print the answers to the queries in the order in which the queries go in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.

Sample Input

Input
5
4 10 3 13 7
8
1 2 4
2 1 3 3
1 2 4
1 3 3
2 2 5 5
1 1 5
2 1 2 10
1 2 3
Output
26
22
0
34
11
Input
6
4 7 4 0 7 3
5
2 2 3 8
1 1 5
2 3 5 1
2 4 5 6
1 2 3
Output
38
28

                                                                   

题意:本题的主要难度在于要将要求区间里的数异或一个数,一开始写的思路是从头到低找到那个数为止,果不其然的T了>.<

所以正确的姿势是: 将所有数化成二进制形式,每一列代表一个数,则第n行就可以表示成一棵树,表示2的(n-1)次方上有多少个1。输入要异或的数V ,同样将这个数做二进制处理,若此位为1 ,则异或那一行。

如图:

4 10  3  13  7   //样例

0   0  1    1   1

0    1  1   0    1

1   0    0   1   1

0    1   0   1   0

0    0    0   0   0    // 二进制表

CODE:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;

const int Max=100005;
int N,M;
int mat[30][Max];

void mate(int nn,int ii)
{
    int k=0;
    while(nn>0){
        mat[k++][ii]=nn%2;
        nn/=2;
    }
}
struct segmentree
{
    ll sum[Max<<2];
    int setv[Max<<2];

    void build(int pos,int l,int r,int ii){
        if(l==r) sum[pos]=mat[ii][l];
        else{
            int mid=(l+r)/2;
            build(pos*2,l,mid,ii);
            build(pos*2+1,mid+1,r,ii);
            sum[pos]=sum[pos*2]+sum[pos*2+1];
        }
    }

    void pushdown(int pos,int l,int r){
        int mid=(l+r)/2;
        setv[pos*2]^=1;
        setv[pos*2+1]^=1;
        sum[pos*2]=(mid-l+1)-sum[pos*2];
        sum[pos*2+1]=(r-mid)-sum[pos*2+1];
        setv[pos]=0;
    }

    void update(int pos,int l,int r,int x,int y){
        if(x<=l && y>=r) {
            setv[pos]^=1;
            sum[pos]=(r-l+1)-sum[pos];
        }
        else{
            if(setv[pos]!=0) pushdown(pos,l,r);
            int mid=(l+r)/2;
            if(x<=mid) update(pos*2,l,mid,x,y);
            if(y>mid) update(pos*2+1,mid+1,r,x,y);
            sum[pos]=sum[pos*2]+sum[pos*2+1];
        }
    }

    ll query(int pos,int l,int r,int x,int y,int ii){
        ll ans=0;
        if(x<=l && y>=r) return sum[pos]*(int)pow(2.0,ii*1.0);
        else{
            if(setv[pos]!=0) pushdown(pos,l,r);
            int mid=(l+r)/2;
            if(x<=mid) ans+=(ll)query(pos*2,l,mid,x,y,ii);
            if(y>mid) ans+=(ll)query(pos*2+1,mid+1,r,x,y,ii);
        }
        return ans;
    }
}tree[30];

int main()
{
    //freopen("in.in","r",stdin);
    while(~scanf("%d",&N)){
        memset(mat,0,sizeof(mat));
        for(int i=1;i<=N;i++){
            int n;
            scanf("%d",&n);
            mate(n,i);
        }

        for(int i=0;i<30;i++){
            tree[i].build(1,1,N,i);
        }
        scanf("%d",&M);
        while(M--){
            int q,x,y,v;
            scanf("%d%d%d",&q,&x,&y);
            if(q==1){
                ll ans=0;
                for(int i=0;i<30;i++){
                    ans+=(ll)tree[i].query(1,1,N,x,y,i);
                }
                printf("%lld\n",ans);
            }
            else{
                scanf("%d",&v);
                int t=0;
                while(v>0){
                    if(v%2) tree[t].update(1,1,N,x,y);
                    v/=2;
                    t++;
                }
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 3-8译码器设计: 首先打开Logisim软件,新建一个电路,然后从左边的工具栏中选择“Wiring”中的“Pin”,将8个输入引脚和3个输出引脚分别拖到电路面板中。 接下来,从左边的工具栏中选择“Gates”中的“Multiplexer”,将它拖到电路面板中。 然后右键点击Multiplexer,选择“Edit Properties”,在“Number of inputs”中输入8,表示我们要将8个输入引脚连接到Multiplexer上。 接着,从左边的工具栏中选择“Wiring”中的“Pin”,将8个输入引脚依次连接到Multiplexer的8个输入端口上。 接下来,从左边的工具栏中选择“Gates”中的“Decoder”,将它拖到电路面板中。 然后右键点击Decoder,选择“Edit Properties”,在“Number of outputs”中输入8,表示我们要将Decoder的8个输出端口连接到Multiplexer上。 接着,从左边的工具栏中选择“Wiring”中的“Pin”,将3个输出引脚依次连接到Multiplexer的3个输出端口上。 最后,从左边的工具栏中选择“Gates”中的“NOT Gate”,将它拖到电路面板中,将Decoder的Enable端口连接到NOT Gate的输入端口上,将NOT Gate的输出端口连接到Multiplexer的Select端口上。 至此,3-8译码器的设计完成。 2. 一位二进制数据比较器设计: 首先打开Logisim软件,新建一个电路,然后从左边的工具栏中选择“Wiring”中的“Pin”,将2个4位二进制数的输入引脚和1个输出引脚分别拖到电路面板中。 接下来,从左边的工具栏中选择“Gates”中的“XOR Gate”和“AND Gate”,将它们分别拖到电路面板中。 然后将输入引脚依次连接到XOR Gate的两个输入端口上,将XOR Gate的输出端口连接到AND Gate的一个输入端口上。 接着,从左边的工具栏中选择“Gates”中的“NOT Gate”,将它拖到电路面板中,将其中一个输入引脚连接到NOT Gate的输入端口上,将NOT Gate的输出端口连接到AND Gate的另一个输入端口上。 最后,将AND Gate的输出端口连接到输出引脚上。 至此,一位二进制数据比较器的设计完成。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值