HDU.6186.CSCource.(前綴和數組和後綴和數組)

  明天后天是南昌賽了嚶嚶嚶,這幾天就先不更新每日題目了,之後補題嚶嚶嚶。php

今天和隊友作了一套2017年廣西邀請賽,5個題仍是有點膨脹......數組

  好了,先來講一下有意思的題目吧......spa

CS Course

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


code

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.

2n,q105

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

After that there are q positive integers p1,p2,,pqin q lines, 1pin 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
 

 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6543  6542  6541  6540  6539 
 
本題思路:我一開始上來就一發暴力,結果超時了,後來和隊友討論了一下,得出 ^ 能夠逆着運算,也就是若是x ^ y = z,那麼y 就能夠用 z ^ x 獲得,&和 | 咱們打算將全部位置出現0和1的次數統計和,而後刪除數字的時候刪除對應數字上的0和1的個數,判斷&和|便可。後來沒有實現這個,而是用後綴數組和前綴數組實現,接着進行相應的位運算就好了,第一次作到這類型的思惟題,因此今天先寫一發博客記錄。
參考代碼:
#include <cstdio>
using namespace std;

const int maxn = 100000 + 5;
int c[maxn], _and1[maxn], _and2[maxn], _or1[maxn], _or2[maxn], _xor1[maxn], _xor2[maxn];

int main() {
    int n, p, q, ans1, ans2, ans3;
    while(~scanf("%d %d", &n, &p)) {
        for(int i = 1; i <=n; i ++) {
            scanf("%d", &c[i]);
        }
        _and1[1] = _or1[1] = _xor1[1] = c[1];
        _and2[n] = _or2[n] = _xor2[n] = c[n];
        for(int i = 2; i <= n; i ++) {
            _and1[i] = _and1[i - 1] & c[i];
            _or1[i] = _or1[i - 1] | c[i];
            _xor1[i] = _xor1[i - 1] ^ c[i];
        }
        for(int i = n - 1; i >= 1; i --) {
            _and2[i] = _and2[i + 1] & c[i];
            _or2[i] = _or2[i + 1] | c[i];
            _xor2[i] = _xor2[i + 1] ^ c[i];
        }
        
        for(int i = 0; i < p; i ++) {
            scanf("%d", &q);
            if(q == 1) {
                ans1 = _and2[2];
                ans2 = _or2[2];
                ans3 = _xor2[2];            
            } else if(q == n) {
                ans1 = _and1[n - 1];
                ans2 = _or1[n - 1];
                ans3 = _xor2[n - 1];
            } else {
                ans1 = _and1[q - 1] & _and2[q + 1];
                ans2 = _or1[q - 1] | _or2[q + 1];
                ans3 = _xor1[q - 1] ^ _xor2[q + 1];
            }
            printf("%d %d %d\n", ans1, ans2, ans3);
        }
    }
    return 0;
}
相關文章
相關標籤/搜索