Accept: 562 Submit: 2532
Time Limit: 10000 mSec Memory Limit : 262144 KB
Problem 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
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.
Source
“高教社杯”第三届福建省大学生程序设计竞赛#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n,q,p,y,z;
int a[1000009],x[1000009*4],lazy[1000009*4];
char ch[10];
void Merge(int k)
{
if(lazy[k<<1]&&lazy[k<<1|1]&&x[k<<1]==x[k<<1|1]) lazy[k]=1,x[k]=x[k<<1];
else lazy[k]=0;
}
void build(int k,int l,int r)
{
if(l==r) {x[k]=a[l],lazy[k]=1;return;}
int mid=(l+r)>>1;
build(k<<1,l,mid),build(k<<1|1,mid+1,r);
Merge(k);
}
void update(int k,int l,int r,int ll,int rr,int val,int type)
{
if(l>=ll&&r<=rr&&lazy[k])
{
if(type==1) x[k]&=val;
else if(type==2) x[k]|=val;
else x[k]^=val;
return ;
}
if(lazy[k])
{
lazy[k<<1]=lazy[k<<1|1]=1;
lazy[k]=0;
x[k<<1]=x[k<<1|1]=x[k];
}
int mid=(l+r)>>1;
if(ll<=mid) update(k<<1,l,mid,ll,rr,val,type);
if(rr>mid) update(k<<1|1,mid+1,r,ll,rr,val,type);
Merge(k);
}
int query(int k,int l,int r,int ll,int rr)
{
if(l>=ll&&r<=rr&&lazy[k]) return x[k]*(r-l+1);
int mid=(l+r)>>1,ans=0;
if(lazy[k])
{
lazy[k<<1]=lazy[k<<1|1]=1;
x[k<<1]=x[k<<1|1]=x[k];
lazy[k]=0;
}
if(ll<=mid) ans+=query(k<<1,l,mid,ll,rr);
if(rr>mid) ans+=query(k<<1|1,mid+1,r,ll,rr);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
build(1,1,n);
while(q--)
{
scanf("%s%d%d",ch,&y,&z);
if(ch[0]=='S') printf("%d\n",query(1,1,n,y+1,z+1));
else
{
scanf("%d",&p);
if(ch[0]=='A') update(1,1,n,z+1,p+1,y,1);
else if(ch[0]=='O') update(1,1,n,z+1,p+1,y,2);
else update(1,1,n,z+1,p+1,y,3);
}
}
}
return 0;
}