Codeforces 242e XOR on segment

Source: Internet
Author: User
E. XOR on segmenttime limit per test

4 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

You 've got an arrayA, ConsistingNIntegersA1, bytes,A2, middle..., middle ,...,AN.
You are allowed to perform two operations on this array:

  1. Calculate the sum of current array elements on the segment [L, Bytes,R], That is, Count valueALRegion + RegionALPipeline + pipeline 1 pipeline + pipeline... pipeline + PipelineAR.
  2. Apply the XOR operation with a given numberXTo each array element on the segment [L, Bytes,R],
    That is, execute.
    This operation changes exactlyRAccept-Encoding-LElements + elements 1 array elements.

Expression means
Applying bitwise XOR operation to numbersXAndY.
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 listMOperations of the indicated type. Your task is to perform all given operations, for each sum query you shoshould print
The result you get.

Input

The first line contains integerN(1 digit ≤ DigitNLimit ≤ limit 105)
-The size of the array. The second line contains space-separated IntegersA1, bytes,A2, middle..., middle ,...,AN(0 bytes ≤ bytesAILimit ≤ limit 106)
-The original array.

The third line contains integerM(1 digit ≤ DigitMLimit ≤ limit 5 · 104)
-The number of operations with the array.I-Th of the followingMLines
First contains an integerTI(1 digit ≤ DigitTILimit ≤ limit 2)
-The type ofI-Th query. IfTILimit = Limit 1,
Then this is the query of the sum, ifTILimit = Limit 2,
Then this is the query to change array elements. IfI-Th operation is of type 1,
Then next follow two integersLI, Bytes,RI(1 digit ≤ DigitLILimit ≤ limitRILimit ≤ limitN).
IfI-Th operation is of Type 2,
Then next follow three IntegersLI, Bytes,RI, Bytes,XI(1 digit ≤ DigitLILimit ≤ limitRILimit ≤ limitN, Memory 1 ≤ memoryXILimit ≤ limit 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
Queries go in the input.

Please, do not use the % LLD specifier to read or write 64-bit integers in bytes ++. It is preferred to use the CIN, coutstreams,
Or the % i64d specifier.

Sample test (s) Input
54 10 3 13 781 2 42 1 3 31 2 41 3 32 2 5 51 1 52 1 2 101 2 3
Output
262203411
Input
64 7 4 0 7 352 2 3 81 1 52 3 5 12 4 5 61 2 3
Output
3828
I used a one-dimensional line segment during the competition, and it timed out. This is because the shortest tree is found at each query. Later I saw that almost all of others use a two-dimensional line segment tree. The idea of the two-dimensional line segment tree is. Each one-dimensional line segment tree stores a single binary position .. Then the sum of [L, R] in any range is to count the number of 1 in this range and multiply it by the number of digits represented by this line segment. Then the implementation is the basic upload of the Line Segment tree, below.
#include<cstdio>using namespace std;const int maxn = 100000+100;typedef long long LL;int a[maxn];int T[22][maxn<<2];bool lazy[22][maxn<<2];void build(int k,int id,int l,int r){    if(l==r){ T[k][id]=(a[l]>>k)&1; return ;}    int m=(l+r)>>1;    build(k,id<<1,l,m);  build(k,id<<1|1,m+1,r);    T[k][id]=T[k][id<<1]+T[k][id<<1|1];}void update(int k,int id,int l,int r,int L,int R){    if(L==l&&R==r){        T[k][id]=r-l+1-T[k][id];        lazy[k][id]^=1;        return ;    }      int m=(L+R)>>1;      if(m>=r)  update(k,id<<1,l,r,L,m);      else if(l>m)  update(k,id<<1|1,l,r,m+1,R);      else{         update(k,id<<1,l,m,L,m);         update(k,id<<1|1,m+1,r,m+1,R);     }     T[k][id]=T[k][id<<1]+T[k][id<<1|1];     if(lazy[k][id])  T[k][id]=R-L+1-T[k][id];}LL query(int k,int id,int l,int r,int L,int R){    if(L==l&&R==r){        return T[k][id];    }    int m=(L+R)>>1,s;    if(m>=r)   s=query(k,id<<1,l,r,L,m);    else  if(l>m)  s=query(k,id<<1|1,l,r,m+1,R);    else{        s=query(k,id<<1,l,m,L,m)+query(k,id<<1|1,m+1,r,m+1,R);    }     if(lazy[k][id]) return r-l+1-s;     else  return s;}int main(){    int n,m;    scanf("%d",&n);    for(int i=1;i<=n;i++)  scanf("%d",&a[i]);    for(int i=0;i<20;i++)  build(i,1,1,n);    scanf("%d",&m);    int id,l,r,x;    while(m--){        scanf("%d",&id);        if(id==1){            scanf("%d%d",&l,&r);            long long sum=0;            for(int i=0;i<20;i++) sum+=query(i,1,l,r,1,n)<<i;            printf("%I64d\n",sum);        }        else{            scanf("%d%d%d",&l,&r,&x);            for(int i=0;i<20;i++){                if(x>>i&1)              update(i,1,l,r,1,n);            }        }    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.