NOI.AC CSP-S national simulation game wherever he goes fourth segment and sub-segment

Title Description

For a sequence a1, a2, ..., an, sub-segment means that it is a continuous part, i.e. al, al + 1, ..., ar
be easily found, a length n sequences $ \ frac {n (n + 1 )} {2} $
subsections. E.g. 3,7,4 sequence following subsections:

(3),(3,7),(3,7,4),(7),(7,4),(4)

Mia desired sub-segments were determined and the exclusive OR, XOR them together again. But Cierra think this is too simple, so she made inquiry of q, each time a given interval [L, R], I hope you will intercept the corresponding sub-index range segment out, answer the above questions.

Specifically, the inquiry [L, R], you need to answer aL, aL + 1, ..., all the sub-segments and aR XOR and XOR.

Input Format

The first line contains two space-separated integers n, q
The second line contains n space-separated integers a1, a2, ..., an
after q rows, each row comprising two integers separated by a space L , R, q represent, in order to ask the group

Output Format

For each query output line, comprising an integer section for all sub-segments of the exclusive OR and exclusive OR and

Sample input

5 3
1 2 3 4 5
1 3
2 4
2 5

Sample Output

2
6
0

Data limitations

For 30% of the data, n, q≤500

For 60% of the data, n, q≤5000

To 100% of the data, 1≤n, q≤200000,0≤ai≤109, each set of L, R are satisfied 1≤L≤R≤n

Time and space constraints

1000ms, 512MB

Push formulas title

We used here \ (\ SUM \) represents an exclusive OR section (i.e., substituting (+)).

Let us find the title \ (\ sum_ {L} = ^ {I} R & lt \ sum_ {J} = I R & lt ^ {} \ sum_ K = {I}} ^ {a_i J \) .

First we look at the prefix Yihuo pretreatment and \ (S [] \) , then it is \ (\ sum_ {i = l } ^ {r} \ sum_ {j = i} ^ {r} s_ {j} \) ^ \ (s_ {i-1} \) , found \ (s_ {i-1} \) and (J \) \ independent, then we propose is to

$
\sum_{i=l}^{r} s_{i-1} \times$ ( (r - i + 1 )&1) ^
\(\sum_{j=i}^{r} s_{j}\)

Again we S [] array is set to do a prefix and t [], the equation can be further into
$
\ sum_ {L} = ^ {I} R & lt S_ {I}. 1-\ Times $ ((R & lt - +. 1 I) &. 1) ^
$ R & lt T_ {} $ ^ \ (. 1-T_ {I} \)

Then there was the 60 points.

We found that the first half of s and not taken is taken and (r-i + 1) parity about, what we are doing and the prefix, the latter half of \ (T \) to make a prefix and on the line.

while (m--) 
{
   l = read(); r = read(); ans = 0;
   if ((r - l + 1) & 1)ans = t[r];
   else ans = 0;

   ans ^= (p[r - 1] ^ p[l - 2]);

   if (!(r & 1))ans ^= ji[r - 1] ^ ji[max(l - 2, 0)];
   else ans ^= ou[r - 1] ^ ou[max(l - 2, 0)];
   printf("%d\n", ans);
}

So that each can do \ (O (1) \) asked.

Finally, I offeruglyCode

#include <iostream>
#include <cstdio>
using namespace std;
int n, m, l, r, ans;
const int N = 200010;
int a[N], s[N], t[N], p[N], ji[N], ou[N];
inline int read() 
{
    int res = 0; char ch = getchar(); bool XX = false;
    for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
    for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
    return XX ? -res : res;
}
int main() 
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)a[i] = read();
    for (int i = 1; i <= n; ++i)s[i] = a[i] ^ s[i - 1];
    for (int i = 1; i <= n; ++i)t[i] = s[i] ^ t[i - 1];
    for (int i = 1; i <= n; ++i)p[i] = t[i] ^ p[i - 1];

    for (int i = 1; i <= n; ++i)
        if (i & 1)ji[i] = ji[i - 1] ^ s[i];
        else ji[i] = ji[i - 1];
    
    for (int i = 1; i <= n; ++i)
        if (!(i & 1))ou[i] = ou[i - 1] ^ s[i];
        else ou[i] = ou[i - 1];
    
    while (m--) 
    {
        l = read(); r = read(); ans = 0;
        if ((r - l + 1) & 1)ans = t[r];
        else ans = 0;

        ans ^= (p[r - 1] ^ p[l - 2]);

        if (!(r & 1))ans ^= ji[r - 1] ^ ji[max(l - 2, 0)];
        else ans ^= ou[r - 1] ^ ou[max(l - 2, 0)];
        printf("%d\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/wljss/p/11701162.html