CS Course (位运算 思维)
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,⋯,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.
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,⋯,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.
Input
There 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.
2≤n,q≤10^5
Then n non-negative integers a1,a2,⋯,an follows in a line, 0≤ai≤1090≤ai≤109 for each i in range[1,n].
After that there are q positive integers p1,p2,⋯,pq in q lines, 1≤pi≤n for each i in range[1,q].
Output
For 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
题意:给你n个数p个询问,每次询问输入一个下标x,求这n个数除了a[x]的按位与,按位或,按位异或的值。
题解:
明明是一个巨水的题,不知道为啥脑卡。嘤嘤嘤
维护一个前缀和和一个后缀和,然后去除那个,就让x前面的x-1个数的和和x+1-n的和按位与,按位或就可以了,异或直接让总的值异或a[x]本身即可。
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 const int maxn=1e5+10;
7 int n,p;
8 int a[maxn];
9 int and1[maxn],and2[maxn];
10 int or1[maxn],or2[maxn];
11 int main()
12 {
13 while(~scanf("%d%d",&n,&p))
14 {
15 memset(and1,0,sizeof(and1));
16 memset(and2,0,sizeof(and2));
17 memset(or1,0,sizeof(or1));
18 memset(or2,0,sizeof(or2));
19 int sumxor=0;
20 for(int i=1;i<=n;i++)
21 {
22 scanf("%d",&a[i]);
23 if(i==1)
24 {
25 and1[i]=a[i];
26 or1[i]=a[i];
27 }
28 else
29 {
30 and1[i]=and1[i-1]&a[i];
31 or1[i]=or1[i-1]|a[i];
32 }
33 sumxor^=a[i];
34 }
35 and2[n]=a[n];
36 or2[n]=a[n];
37 for(int i=n-1;i>=1;i--)
38 {
39 and2[i]=and2[i+1]&a[i];
40 or2[i]=or2[i+1]|a[i];
41 }
42 for(int i=1;i<=p;i++)
43 {
44 int x;
45 scanf("%d",&x);
46 if(x==1)
47 {
48 printf("%d %d %d\n",and2[2],or2[2],sumxor^a[x]);
49 }
50 else if(x==n)
51 {
52 printf("%d %d %d\n",and1[n-1],or1[n-1],sumxor^a[x]);
53 }
54 else
55 {
56 printf("%d %d %d\n",and1[x-1]&and2[x+1],or1[x-1]|or2[x+1],sumxor^a[x]);
57 }
58 }
59 }
60 }