您的位置:首页 > 其它

位运算 FZU - 2105 纯暴力(水,本来应该是线段树)

2017-06-24 20:26 337 查看
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.

看到题目的时候感觉应该是线段树,然后不是很会,然后。。。。。。。。。。。。。。

马上带来线段树版

下面是水版

这个题了你10s的时间让你算出结果,一般的oj基本上是一秒钟计算1e8,撑死1e9,然而这个题的n最坏情况是1,000,000,m最坏情况是100,000,然后t是100

最坏情况 1e15,可以说是大了好几个数量级。然后看到有几个学长超时了我就不敢写了。但是!

tmd数据水的要死,纯暴力也能过,根本不是算法题目,哎。QAQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ。最后才五秒多- -!

#include<stdio.h>
using namespace std;
int save[1000008];
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
getchar();
for(int i=0; i<n; i++)
{
scanf("%d",&save[i]);
}
getchar();
char str[10];
while(m--)
{
scanf("%s",str);
if(str[0]=='S')
{
int a,b;
int sum=0;
scanf("%d%d",&a,&b);
for(int i=a; i<=b; i++)
{
sum+=save[i];
}
printf("%d\n",sum);
}
else if(str[0]=='X')
{
int a,b,w;
scanf("%d%d%d",&w,&a,&b);
for(int i=a; i<=b; i++)
save[i]^=w;
}
else if(str[0]=='O')
{
int a,b,w;
scanf("%d%d%d",&w,&a,&b);
for(int i=a; i<=b; i++)
save[i]|=w;
}
else if(str[0]=='A')
{
int a,b,w;
scanf("%d%d%d",&w,&a,&b);
for(int i=a; i<=b; i++)
save[i]&=w;
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: