HDU6186(线段树)

时间:2021-09-18 13:37:18

CS Course

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 112    Accepted Submission(s): 66

Problem Description

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 ap.

 

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≤105

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

After that there are q positive integers p1,p2,⋯,pqin 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 ap in a line.
 

Sample Input

3 3
1 1 1
1
2
3
 

Sample Output

1 1 0
1 1 0
1 1 0
 

Source

 
 //2017-08-31
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define mid ((st[id].l+st[id].r)>>1)
#define lson (id<<1)
#define rson ((id<<1)|1) using namespace std; const int N = ;
int arr[N];
struct Node{
int l, r, OR, XOR, AND;
}st[N<<]; void build(int id, int l, int r)
{
st[id].l = l; st[id].r = r;
if(l == r){
st[id].OR = arr[l];
st[id].AND = arr[l];
st[id].XOR = arr[l];
return;
}
build(lson, l, mid);
build(rson, mid+, r);
st[id].OR = st[lson].OR | st[rson].OR;
st[id].XOR = st[lson].XOR ^ st[rson].XOR;
st[id].AND = st[lson].AND & st[rson].AND;
} int query(int id, int l, int r, int op){
if(st[id].l == l && st[id].r == r){
if(op == )return st[id].OR;
if(op == )return st[id].XOR;
if(op == )return st[id].AND;
}
if(l > mid)return query(rson, l, r, op);
else if(r <= mid)return query(lson, l, r, op);
else{
if(op == )return query(lson, l, mid, op) | query(rson, mid+, r, op);
if(op == )return query(lson, l, mid, op) ^ query(rson, mid+, r, op);
if(op == )return query(lson, l, mid, op) & query(rson, mid+, r, op);
}
} int main()
{
int n, q;
while(scanf("%d%d", &n, &q)!=EOF)
{
for(int i = ; i <= n; i++)
scanf("%d", &arr[i]);
build(, , n);
int p;
while(q--){
scanf("%d", &p);
if(p == )
printf("%d %d %d\n", query(, , n, ), query(, , n, ), query(, , n, ));
else if(p == n)
printf("%d %d %d\n", query(, , n-, ), query(, , n-, ), query(, , n-, ));
else
printf("%d %d %d\n", query(, , p-, )&query(, p+, n, ), query(, , p-, )|query(, p+, n, ), query(, , p-, )^query(, p+, n, ));
}
} return ;
}