FZU2105-Digits Count(线段树区间)
2014-08-11 02:20
381 查看
Description
Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations:
Operation 1: AND opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] AND opn (here "AND" is bitwise operation).
Operation 2: OR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] OR opn (here "OR" is bitwise operation).
Operation 3: XOR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] XOR opn (here "XOR" is bitwise operation).
Operation 4: SUM L R
We want to know the result of A[L]+A[L+1]+...+A[R].
Now can you solve this easy problem?
Input
The first line of the input contains an integer T, indicating the number of test cases. (T≤100)
Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.
Then one line follows n integers A[0], A[1], ..., A[n-1] (0≤A[i]<16,0≤i<n).
Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)
Output
For each test case and for each "SUM" operation, please output the result with a single line.
Sample Input
Sample Output
Hint
A = [1 2 4 7]
SUM 0 2, result=1+2+4=7;
XOR 5 0 0, A=[4 2 4 7];
OR 6 0 3, A=[6 6 6 7];
SUM 0 2, result=6+6+6=18.
四种操作:且,或,异或,求和
线段树区间操作。
各种坑点,WA了n多遍~
1.mat 数组没有清零
2.且 和 或其实是同一种操作,set 1,和 set 0,异或相当于加的操作。所以且 和 或的操作 优先于异或。
3.在AND 处 v=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=1000006;
int mat[4][Max];
void change(int n,int ii)
{
int k=0;
while(n>0){
mat[k++][ii]=n%2;
n/=2;
}
}
struct segmentree
{
int sum[Max<<2];
int seto[Max<<2];
int setx[Max<<2];
void pushup(int pos){
sum[pos]=sum[pos*2]+sum[pos*2+1];
}
void clean(int pos,int l,int r,int ii){
seto[pos]=-1;
setx[pos]=0;
if(l==r) sum[pos]=0;
else{
int mid=(l+r)/2;
clean(pos*2,l,mid,ii);
clean(pos*2+1,mid+1,r,ii);
pushup(pos);
}
}
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);
pushup(pos);
}
}
void pushdown_xor(int pos,int l,int r){
if(setx[pos]!=0){
setx[pos*2+1]^=1;
setx[pos*2]^=1;
int mid=(l+r)/2;
sum[pos*2]=(mid-l+1)-sum[pos*2];
sum[pos*2+1]=(r-mid)-sum[pos*2+1];
setx[pos]=0;
//seto[pos*2]=seto[pos*2+1]=-1;
}
}
void pushdown_or_and(int pos,int l,int r){
if(seto[pos]!=-1){
seto[pos*2]=seto[pos*2+1]=seto[pos];
int mid=(l+r)/2;
sum[pos*2]=(mid-l+1)*seto[pos*2];
sum[pos*2+1]=(r-mid)*seto[pos*2+1];
seto[pos]=-1;
setx[pos*2]=setx[pos*2+1]=0;
}
}
void update_and(int pos,int l,int r,int x,int y){
if(x<=l && y>=r){
seto[pos]=0;
sum[pos]=0;
setx[pos]=0;
}
else{
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) update_and(pos*2,l,mid,x,y);
if(y>mid) update_and(pos*2+1,mid+1,r,x,y);
pushup(pos);
}
}
void update_or(int pos,int l,int r,int x,int y){
if(x<=l && y>=r){
seto[pos]=1;
sum[pos]=(r-l+1)*seto[pos];
setx[pos]=0;
}
else{
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) update_or(pos*2,l,mid,x,y);
if(y>mid) update_or(pos*2+1,mid+1,r,x,y);
pushup(pos);
}
}
void update_xor(int pos,int l,int r,int x,int y){
if(x<=l && y>=r){
setx[pos]^=1;
sum[pos]=(r-l+1)-sum[pos];
//seto[pos]=-1;
}
else{
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) update_xor(pos*2,l,mid,x,y);
if(y>mid) update_xor(pos*2+1,mid+1,r,x,y);
pushup(pos);
}
}
int query(int pos,int l,int r,int x,int y,int ii){
if(x<=l && y>=r) return sum[pos]*(1<<ii);
else{
int ans=0;
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) ans+=query(pos*2,l,mid,x,y,ii);
if(y>mid) ans+=query(pos*2+1,mid+1,r,x,y,ii);
return ans;
}
}
}tree[4];;
int main()
{
//freopen("in.in","r",stdin);
int T;
scanf("%d",&T);
while(T--){
memset(mat,0,sizeof(mat));
int N,M;
scanf("%d %d",&N,&M);
for(int i=0;i<4;i++){
tree[i].clean(1,1,N,i);
}
for(int i=1;i<=N;i++){
int n;
scanf("%d",&n);
change(n,i);
}
for(int i=0;i<4;i++){
tree[i].build(1,1,N,i);
}
char s[10];
while(M--){
getchar();
int v,xx,yy;
scanf("%s",s);
if(s[0]=='S'){
scanf("%d%d",&xx,&yy);
xx++;yy++;
int ans=0;
for(int i=0;i<4;i++)
ans+=tree[i].query(1,1,N,xx,yy,i);
printf("%d\n",ans);
}
else{
scanf("%d%d%d",&v,&xx,&yy);
xx++;yy++;
if(s[0]=='A'){
int k=0;
while(k<4){
if(v%2==0) tree[k].update_and(1,1,N,xx,yy);
v/=2;
k++;
}
}
if(s[0]=='X'){
int k=0;
while(k<4){
if(v%2) tree[k].update_xor(1,1,N,xx,yy);
v/=2;
k++;
}
}
if(s[0]=='O'){
int k=0;
while(k<4){
if(v%2) tree[k].update_or(1,1,N,xx,yy);
v/=2;
k++;
}
}
}
}
}
return 0;
}
感谢BMan 帮我debug>.<
Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations:
Operation 1: AND opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] AND opn (here "AND" is bitwise operation).
Operation 2: OR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] OR opn (here "OR" is bitwise operation).
Operation 3: XOR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] XOR opn (here "XOR" is bitwise operation).
Operation 4: SUM L R
We want to know the result of A[L]+A[L+1]+...+A[R].
Now can you solve this easy problem?
Input
The first line of the input contains an integer T, indicating the number of test cases. (T≤100)
Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.
Then one line follows n integers A[0], A[1], ..., A[n-1] (0≤A[i]<16,0≤i<n).
Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)
Output
For each test case and for each "SUM" operation, please output the result with a single line.
Sample Input
1 4 4 1 2 4 7 SUM 0 2 XOR 5 0 0 OR 6 0 3 SUM 0 2
Sample Output
7 18
Hint
A = [1 2 4 7]
SUM 0 2, result=1+2+4=7;
XOR 5 0 0, A=[4 2 4 7];
OR 6 0 3, A=[6 6 6 7];
SUM 0 2, result=6+6+6=18.
四种操作:且,或,异或,求和
线段树区间操作。
各种坑点,WA了n多遍~
1.mat 数组没有清零
2.且 和 或其实是同一种操作,set 1,和 set 0,异或相当于加的操作。所以且 和 或的操作 优先于异或。
3.在AND 处 v=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=1000006;
int mat[4][Max];
void change(int n,int ii)
{
int k=0;
while(n>0){
mat[k++][ii]=n%2;
n/=2;
}
}
struct segmentree
{
int sum[Max<<2];
int seto[Max<<2];
int setx[Max<<2];
void pushup(int pos){
sum[pos]=sum[pos*2]+sum[pos*2+1];
}
void clean(int pos,int l,int r,int ii){
seto[pos]=-1;
setx[pos]=0;
if(l==r) sum[pos]=0;
else{
int mid=(l+r)/2;
clean(pos*2,l,mid,ii);
clean(pos*2+1,mid+1,r,ii);
pushup(pos);
}
}
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);
pushup(pos);
}
}
void pushdown_xor(int pos,int l,int r){
if(setx[pos]!=0){
setx[pos*2+1]^=1;
setx[pos*2]^=1;
int mid=(l+r)/2;
sum[pos*2]=(mid-l+1)-sum[pos*2];
sum[pos*2+1]=(r-mid)-sum[pos*2+1];
setx[pos]=0;
//seto[pos*2]=seto[pos*2+1]=-1;
}
}
void pushdown_or_and(int pos,int l,int r){
if(seto[pos]!=-1){
seto[pos*2]=seto[pos*2+1]=seto[pos];
int mid=(l+r)/2;
sum[pos*2]=(mid-l+1)*seto[pos*2];
sum[pos*2+1]=(r-mid)*seto[pos*2+1];
seto[pos]=-1;
setx[pos*2]=setx[pos*2+1]=0;
}
}
void update_and(int pos,int l,int r,int x,int y){
if(x<=l && y>=r){
seto[pos]=0;
sum[pos]=0;
setx[pos]=0;
}
else{
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) update_and(pos*2,l,mid,x,y);
if(y>mid) update_and(pos*2+1,mid+1,r,x,y);
pushup(pos);
}
}
void update_or(int pos,int l,int r,int x,int y){
if(x<=l && y>=r){
seto[pos]=1;
sum[pos]=(r-l+1)*seto[pos];
setx[pos]=0;
}
else{
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) update_or(pos*2,l,mid,x,y);
if(y>mid) update_or(pos*2+1,mid+1,r,x,y);
pushup(pos);
}
}
void update_xor(int pos,int l,int r,int x,int y){
if(x<=l && y>=r){
setx[pos]^=1;
sum[pos]=(r-l+1)-sum[pos];
//seto[pos]=-1;
}
else{
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) update_xor(pos*2,l,mid,x,y);
if(y>mid) update_xor(pos*2+1,mid+1,r,x,y);
pushup(pos);
}
}
int query(int pos,int l,int r,int x,int y,int ii){
if(x<=l && y>=r) return sum[pos]*(1<<ii);
else{
int ans=0;
pushdown_or_and(pos,l,r);
pushdown_xor(pos,l,r);
int mid=(l+r)/2;
if(x<=mid) ans+=query(pos*2,l,mid,x,y,ii);
if(y>mid) ans+=query(pos*2+1,mid+1,r,x,y,ii);
return ans;
}
}
}tree[4];;
int main()
{
//freopen("in.in","r",stdin);
int T;
scanf("%d",&T);
while(T--){
memset(mat,0,sizeof(mat));
int N,M;
scanf("%d %d",&N,&M);
for(int i=0;i<4;i++){
tree[i].clean(1,1,N,i);
}
for(int i=1;i<=N;i++){
int n;
scanf("%d",&n);
change(n,i);
}
for(int i=0;i<4;i++){
tree[i].build(1,1,N,i);
}
char s[10];
while(M--){
getchar();
int v,xx,yy;
scanf("%s",s);
if(s[0]=='S'){
scanf("%d%d",&xx,&yy);
xx++;yy++;
int ans=0;
for(int i=0;i<4;i++)
ans+=tree[i].query(1,1,N,xx,yy,i);
printf("%d\n",ans);
}
else{
scanf("%d%d%d",&v,&xx,&yy);
xx++;yy++;
if(s[0]=='A'){
int k=0;
while(k<4){
if(v%2==0) tree[k].update_and(1,1,N,xx,yy);
v/=2;
k++;
}
}
if(s[0]=='X'){
int k=0;
while(k<4){
if(v%2) tree[k].update_xor(1,1,N,xx,yy);
v/=2;
k++;
}
}
if(s[0]=='O'){
int k=0;
while(k<4){
if(v%2) tree[k].update_or(1,1,N,xx,yy);
v/=2;
k++;
}
}
}
}
}
return 0;
}
感谢BMan 帮我debug>.<
相关文章推荐
- [FZU 2105 Digits Count] 线段树区间的复合操作
- FZU 2105 Digits Count(线段树区间修改)
- fzu 2105 Digits Count (线段树区间更新)
- FZU-2105 Digits Count(线段树)
- FZU 2105 Digits Count 区间更新
- fzu 2105 Digits Count ( 线段树 ) from 第三届福建省大学生程序设计竞赛
- FZU 2105 Digits Count(按位维护线段树)
- FZU 2105 Digits Count(线段树)
- fzu 2105 Digits Count 线段树
- 【FZU】2105 Digits Count 线段树
- FZU2105 Digits Count(经典 线段树)
- FZU 2105 Digits Count(线段树)
- FZU Problem 2105 Digits Count线段树之延迟更新(插线取线)
- FZU - 2105 Digits Count (线段树成段更新)
- FZU 2105-Digits Count(线段树延时标记)
- FZU-2105 Digits Count (两种标记成段更新)
- fzu 2105——Digits Count
- FZU/FOJ 2105 Problem 2105 Digits Count 成段更新
- FZU 2105 Digits Count(AC)
- fzu 2105 Digits Count(成段更新)