HDU6186CS Course前缀和

来源:互联网 发布:高中封闭式管理 知乎 编辑:程序博客网 时间:2024/05/29 18:39

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186

6186 CS Course

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

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 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≤10^5

Then n non-negative integers a1,a2,⋯,an follows in a line, 0≤ai≤10
^9 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].

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

题意:给一个序列a,求除去pk个的与,或,异或

思路:分别求前缀数组和后缀数组,在pi的两边分别进行运算,再将这两个结果进行运算即可。

附上ac代码

#include<stdio.h>using namespace std;const int maxn = 100005;int a[maxn];int x1[maxn], y1[maxn], z1[maxn];int x2[maxn], y2[maxn], z2[maxn];int main(){     int n, m;     while(scanf("%d%d", &n, &m) != EOF){          for(int i = 1; i <= n; i++)               scanf("%d", &a[i]);          //前缀和          x1[1] = y1[1] = z1[1] = a[1];          for(int i = 2; i <= n; i++){               x1[i] = a[i] & x1[i-1];               y1[i] = a[i] | y1[i-1];               z1[i] = a[i] ^ z1[i-1];          }          //后缀和          x2[n] = y2[n] = z2[n] = a[n];          for(int i = n - 1; i >= 1; i--){               x2[i] = a[i] & x2[i+1];               y2[i] = a[i] | y2[i+1];               z2[i] = a[i] ^ z2[i+1];          }          for(int i = 1; i <= m; i++){               int q;               scanf("%d", &q);               if(q == 1)                    printf("%d %d %d\n", x2[q+1], y2[q+1], z2[q+1]);               else if(q == n)                    printf("%d %d %d\n", x1[q-1], y1[q-1], z1[q-1]);               else{                    printf("%d %d %d\n", x1[q-1] & x2[q+1],                                         y1[q-1] | y2[q+1],                                         z1[q-1] ^ z2[q+1]);               }          }     }     return 0;}
原创粉丝点击