CF 242E-XOR on Segment(线段树区间处理+二进制)
2014-08-08 22:45
399 查看
Description
You've got an array a, consisting of
n integers a1, a2, ..., an. You are allowed to perform two operations on this array:
Calculate the sum of current array elements on the segment
[l, r], that is, count value al + al + 1 + ... + ar.
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
Output
Input
Output
题意:本题的主要难度在于要将要求区间里的数异或一个数,一开始写的思路是从头到低找到那个数为止,果不其然的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;
}
You've got an array a, consisting of
n integers a1, a2, ..., an. You are allowed to perform two operations on this array:
Calculate the sum of current array elements on the segment
[l, r], that is, count value al + al + 1 + ... + ar.
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;
}
相关文章推荐
- CF242:XOR on Segment(线段树区间更新 & 二进制)
- Codeforce-242E-XOR on Segment(线段树区间更新)
- Codeforces Round #149 (Div. 2) E. XOR on Segment(21棵线段树处理每一位+区间异或)
- Codeforces-242E:XOR on Segment(20个线段树)
- Codeforces 242E- XOR on Segment(线段树)
- CodeForces 242E - XOR on Segment 二维线段树?
- CodeForces 242E - XOR on Segment 二维线段树?
- Codeforecs XOR On Segment 线段树(区间异或,求和)
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
- [CF#365 (Div. 2) Mishka and Interesting sum] 线段树离线处理区间不同数
- [CodeForces242E]XOR on Segment-线段树
- codeforces 242E - XOR on Segment
- CF149 E XOR on Segment(线段树成段更新)
- Codeforces #159 224 E E. XOR on Segment 二十几颗线段树 成段更新 成段求和
- CF-46D-Parking Lot(线段树,区间合并,点表示线段)
- Apache Segmentaion Fault故障处理案例分析
- 线段树离线处理(区间内不同的数的个数)hdu3333
- CF 46 D Parking Lot(线段树区间合并)
- Codeforces Round #218 (Div. 2) (线段树区间处理)
- UVA 11992(Fast Matrix Operations-线段树区间加&改)[Template:SegmentTree]