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.
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≤1052≤n,q≤105
Then n non-negative integers a1,a2,⋯,ana1,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,⋯,pqp1,p2,⋯,pqin q lines, 1≤pi≤n1≤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
思路:维护前缀和+后缀和,当询问第i个时,将1~i-1的结果与i+1~n的结果进行运算即是答案。
//#include<bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
typedef vector<ll> vec;
typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
int n,q,m;
int arr[maxn];
int And[maxn][5];
int Or[maxn][5];
int Xor[maxn][5];
int main()
{
std::ios::sync_with_stdio(false);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(cin >> n >> m){
rep(i, 1, n){
cin >> arr[i];
if(i == 1){
And[i][0] = arr[i];
Or[i][0] = arr[i];
Xor[i][0] = arr[i];
}
else{
And[i][0] = And[i-1][0] & arr[i];
Or[i][0] = Or[i-1][0] | arr[i];
Xor[i][0] = Xor[i-1][0] ^ arr[i];
}
}
per(i, n, 1){
if(i == n){
And[i][1] = arr[i];
Or[i][1] = arr[i];
Xor[i][1] = arr[i];
}
else{
And[i][1] = And[i+1][1] & arr[i];
Or[i][1] = Or[i+1][1] | arr[i];
Xor[i][1] = Xor[i+1][1] ^ arr[i];
}
}
int ta;
while(m--){
cin >> ta;
if(ta == 1){
cout << And[2][1] << " " << Or[2][1] << " " << Xor[2][1] << endl;
continue;
}
if(ta == n){
cout << And[n-1][0] << " " << Or[n-1][0] << " " << Xor[n-1][0] << endl;
continue;
}
cout << (And[ta-1][0] & And[ta+1][1]) << " " << (Or[ta-1][0] | Or[ta+1][1]) << " " << (Xor[ta-1][0] ^ Xor[ta+1][1]) << endl;
}
}
return 0;
}