4334: CS Course
时间限制: 2 Sec 内存限制: 512 MB
题目描述
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.
输入
There are no more than 15 test cases.
Each test case begins with two positive integers n(2 ≤ n ≤ 105) and p(2 ≤ p ≤ 105) in a line, indicate the number of positive integers and the number of queries.
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, ...,pq in q lines, 1 ≤ pi ≤ n for each i in range [1,q].
输出
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.
样例输入
3 3
1 1 1
1
2
3
样例输出
1 1 0
1 1 0
时间限制: 2 Sec 内存限制: 512 MB
题目描述
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.
输入
There are no more than 15 test cases.
Each test case begins with two positive integers n(2 ≤ n ≤ 105) and p(2 ≤ p ≤ 105) in a line, indicate the number of positive integers and the number of queries.
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, ...,pq in q lines, 1 ≤ pi ≤ n for each i in range [1,q].
输出
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.
样例输入
3 3
1 1 1
1
2
3
样例输出
1 1 0
1 1 0
1 1 0
题意:大致意思给你n个数分别为a1,a2,a3...an,然后进行m次询问,每次询问给你一个数p,将除了ap以外的所有数进行and,or,xor三种位运算,输出三种位运算的结果。
分析:数据量很大,直接暴力模拟肯定会超时,用线段树查询区间,每次查询时间为o(logn),效率大大提高。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lson (u<<1)
#define rson (u<<1|1)
using namespace std;
const int maxn=100010;
int _xor, _and, _or;
int read() { //快读读入
int x = 0;
char c = getchar();
while (c < '0' || c > '9')c = getchar();
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
struct T {
int l, r, _and, _or, _xor;
} node[maxn << 2];
void pushup(int u) {
node[u]._and = node[lson]._and & node[rson]._and;
node[u]._or = node[lson]._or | node[rson]._or;
node[u]._xor = node[lson]._xor ^ node[rson]._xor;
}
void build(int u, int l, int r) {
node[u].l = l;
node[u].r = r;
if (l == r) {
node[u]._xor = node[u]._or = node[u]._and = read();
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
pushup(u);
}
void query(int u, int l, int r) {
if (l <= node[u].l && node[u].r <= r) {
_xor ^= node[u]._xor;
_and &= node[u]._and;
_or |= node[u]._or;
return;
}
int mid = (node[u].l + node[u].r) >> 1;
if (r <= mid) query(lson, l, r);
else if (l > mid) query(rson, l, r);
else {
query(lson, l, mid);
query(rson, mid + 1, r);
}
}
int main() {
//freopen("in.txt", "r", stdin);
int n, m, p;
while (~scanf("%d%d", &n, &m)) {
build(1, 1, n);
while (m--) {
_xor = _or = 0;
_and = -1;
scanf("%d", &p);
if (p == 1) {
query(1, 2, n);
} else if (p == n) {
query(1, 1, n - 1);
} else {
query(1, 1, p - 1);
query(1, p + 1, n);
}
printf("%d %d %d\n", _and, _or, _xor);
}
}
return 0;
}