HDU.6186.CSCource. (Prefix and suffix array array)

  Tomorrow is the day after the race Nanchang trained, trained, trained, these days are not updated daily on the first topic, and later trained, trained, trained up questions.

Today and teammates did a 2017 tournament in Guangxi, five questions still a little swelling ......

  Well, first look for an interesting topic it ......

CS Course

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


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 
 
This question ideas: I started to come out of violence, the result of a timeout, and later his teammates and discuss a bit, you can come ^ against the operation, that is, if x ^ y = z, then y can be obtained by z ^ x, & and | we intend to position all occurrences 0 and 1 count and then deleted when deleting digital number 0 and 1 on the corresponding figure, judge & and | can be. Later not realize this, but with the prefix and suffix array arrays to achieve, and then make the appropriate bit operation on the line for the first time to do this type of thinking problems, so today out of the first to write blog records.
Reference Code:
#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;
}

 

Guess you like

Origin www.cnblogs.com/bianjunting/p/10958255.html