CS Course

Little A has come to college and majored in Computer and Science. 

Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework. 

Here is the problem: 

You are giving n non-negative integers  a1,a2,,ana1,a2,⋯,an, and some queries. 

A query only contains a positive integer p, which means you 
are asked to answer the result of bit-operations (and, or, xor) of all the integers except  apap
InputThere are no more than 15 test cases. 

Each test case begins with two positive integers n and p 
in a line, indicate the number of positive integers and the number of queries. 

2n,q1052≤n,q≤105 

Then n non-negative integers  a1,a2,,ana1,a2,⋯,an follows in a line,  0ai1090≤ai≤109 for each i in range[1,n]. 

After that there are q positive integers  p1,p2,,pqp1,p2,⋯,pqin q lines,  1pin1≤pi≤n for each i in range[1,q].OutputFor each query p, output three non-negative integers indicates the result of bit-operations(and, or, xor) of all non-negative integers except  apap in a line. 
Sample Input
3 3
1 1 1
1
2
3

Sample Output

1 1 0
1 1 0
1 1 0


这个题目如果使用单纯的写法必然会t,因为它是属于那种位运算的,所以最后采取使用前缀和后缀来进行处理。

#include<bits/stdc++.h>

using namespace std;

int a[100005];
int presumand[100005];
int sufsumand[100005];
int presumor[100005];
int sufsumor[100005];
int presumno[100005];
int sufsumno[100005];

int main(){
    int n,p;
    while(~scanf("%d%d",&n,&p)){
        for(int i=0;i<n;i++){
        scanf("%d",&a[i]);

        }
        presumand[0]=presumor[0]=presumno[0]=a[0];
        for(int i=1;i<n;i++){
            presumand[i]=a[i]&presumand[i-1];
            presumor[i]=presumor[i-1]|a[i];
            presumno[i]=presumno[i-1]^a[i];
        }
        sufsumand[n-1]=sufsumor[n-1]=sufsumno[n-1]=a[n-1];
        for(int i=n-2;i>=0;i--){
            sufsumand[i]=a[i]&sufsumand[i+1];
            sufsumor[i]=sufsumor[i+1]|a[i];
            sufsumno[i]=sufsumno[i+1]^a[i];
        }

        int k;
        while(p--){
            scanf("%d",&k);
            k--;
            if(k==0){
                printf("%d ",sufsumand[k+1]);
                printf("%d ",sufsumor[k+1]);
                printf("%d\n",sufsumno[k+1]);
            }else if(k==n-1){
                printf("%d ",presumand[k-1]);
                printf("%d ",presumor[k-1]);
                printf("%d\n",presumno[k-1]);
            }
            else{
                printf("%d ",presumand[k-1]&sufsumand[k+1]);
                printf("%d ",presumor[k-1]|sufsumor[k+1]);
                printf("%d\n",sufsumno[k+1]^presumno[k-1]);
            }
        }
    }

	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/81042748
cs