2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)E - 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 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

线段树查询即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>
#define lson 2*x
#define rson 2*x+1


ll mul(ll a, ll b) {
    ll rt = 0;
    while (b) {
        if (b & 1)rt = (rt + a);
        b = b / 2;
        a = (a << 1);
    }
    return rt;
}


ll quickpow(ll a, ll b) {
    ll ans = 1, tmp = a;
    while (b > 0) {
        if (b % 2)ans = mul(ans, tmp);
        b = b / 2;
        tmp = mul(tmp, tmp);
    }
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}

ll power(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)ans = 1ll * ans * a;
        a = 1ll * a * a;
        b = b >> 1;
    }
    return ans;
}

inline int read() {
    int an = 0, x = 1; char c = getchar();
    while (c > '9' || c < '0') {
        if (c == '-') {
            x = -1; 
        }
        c = getchar();
    }
    while (c >= '0'&&c <= '9') {
        an = an * 10 + c - '0'; c = getchar();
    }
    return an * x;
}


int a[maxn];
struct node {
    int l, r;
    int q1, q2, q3;
}tree[maxn<<2];

int t1, t2, t3;

void pushUp(int x) {
    tree[x].q1 = tree[lson].q1&tree[rson].q1;
    tree[x].q2 = tree[lson].q2 | tree[rson].q2;
    tree[x].q3 = tree[lson].q3^tree[rson].q3;
}

void build(int l, int r, int x) {
    tree[x].l = l;
    tree[x].r = r;
    if (l == r) {
        cin >> tree[x].q1;
        tree[x].q2 = tree[x].q3 = tree[x].q1;
        return;
    }
    int mid = (tree[x].l + tree[x].r) >> 1;
    build(l, mid, lson);
    build(mid + 1, r, rson);
    pushUp(x);
}

void query(int l, int r, int x) {
    if (tree[x].l == l && tree[x].r == r) {
        t1 = tree[x].q1;
        t2 = tree[x].q2;
        t3 = tree[x].q3;
        return;
    }
    int mid = (tree[x].l + tree[x].r) >> 1;
    if (r <= mid)query(l, r, lson);
    else if (l > mid)query(l, r, rson);
    else {
        int tmp1, tmp2, tmp3;
        query(l, mid, lson);
        tmp1 = t1, tmp2 = t2, tmp3 = t3;
        query(mid + 1, r, rson);
        t1 &= tmp1;
        t2 |= tmp2;
        t3 ^= tmp3;
    }
}

int main() {
    ios::sync_with_stdio(false);
    int n, q;
    while (cin >> n >> q) {
        int i, j;
        int tmp1, tmp2, tmp3;
        build(1, n, 1);
        for (i = 0; i < q; i++) {
            int x; cin >> x;
            if (x == 1 || x == n) {
                if (x == 1)query(2, n, 1);
                else query(1, n - 1, 1);
                cout << t1 << ' ' << t2 << ' ' << t3 << endl;
            }
            else {
                query(1, x - 1, 1);
                tmp1 = t1; tmp2 = t2; tmp3 = t3;
                query(x + 1, n, 1);
                cout << (tmp1 & t1) << ' ' << (tmp2 | t2) << ' ' << (tmp3 ^ t3 )<< endl;
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值