HDU 6186-CS Course(位运算+前缀合)

CS Course

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


 

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

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

题目大意:给你n个数,q次查询x,每次查询输出,所有n个数,除了查询的那一个数a[x]之外的所有数的,&, |, ^,的结果。

解题思路:对于^运算,我们采用下x^a^a = x的性质,先算出所有数的 ^ 值,然后^上a[x]就是结果。

对于 & 和 | 运算,我们处理下前缀和与后缀和,因为 &,| 的运算也满足结合律,所以最后算除a[x]外的前缀和与后缀和即可。

写这一题其实就是搞清楚位运算也满足结合律。。。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1010;
const double pi = 3.1415926;
const int maxn=1e8+1;
using namespace std;

int n, q;
int a[100010], sum, sum1[100010], sum2[100010], sum3[100010], sum4[100010];

int main()
{
    while(~scanf("%d%d", &n, &q)) {
        sum = 0;
        for(int i = 1; i <= n; i ++) {
            scanf("%d", &a[i]);
            sum ^= a[i];
        }
        sum1[0] = sum2[n+1] = (1<<31)-1;
        for(int i = 1; i <= n; i ++) {
            sum1[i] = (sum1[i-1]&a[i]);
        }
        for(int i = n; i >= 1; i --) {
            sum2[i] = (sum2[i+1]&a[i]);
        }
        sum3[0] = sum4[n+1] = 0;
        for(int i = 1; i <= n; i ++) {
            sum3[i] = (sum3[i-1]|a[i]);
        }
        for(int i = n; i >= 1; i --) {
            sum4[i] = (sum4[i+1]|a[i]);
        }
        int x;
        while(q --) {
            scanf("%d", &x);
            printf("%d %d %d\n", sum1[x-1]&sum2[x+1], sum3[x-1]|sum4[x+1], sum^a[x]);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值