The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR

简介: The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR

Given a sequence of nn numbers a1 ,a2,⋯,an and three functions.

Define a function f(l,r)f(l,r) which returns \oplus a[x]⊕a[x] (l \le x \le rl≤x≤r). The \oplus⊕ represents exclusive OR.

Define a function g(l,r)g(l,r) which returns \oplus f(x,y)(l \le x \le y \le r)⊕f(x,y)(l≤x≤y≤r).

Define a function w(l,r)w(l,r) which returns \oplus g(x,y)(l \le x \le y \le r)⊕g(x,y)(l≤x≤y≤r).

You are also given a number of xor-queries. A xor-query is a pair (i, j) (1≤i≤j≤n). For each xor-query (i, j)(i,j), you have to answer the result of function w(l,r).

Input

Line 1: t(1≤t≤20).


For each test case:


Line 1: n(1≤n≤100000).


Line 2: n numbers a1, a2 ,⋯,an(1≤ai≤10^9).


Line 33: q(1≤q≤100000), the number of xor-queries.


In the next q lines, each line contains 22 numbers i,j representing a xor-query(1≤i≤j≤n).


It is guaranteed that sum of nn and q≤10^6.


Output

For each xor-query (i, j)(i,j), print the result of function w(i,j)w(i,j) in a single line.

暴力打表找规律,维护前缀异或
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int b[4][maxn], c[4][maxn];
int dp1[4][maxn], dp2[4][maxn];
int main() {
  int T, n, m, l, r;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= n; j++) {
        if(j%4==i)b[i][j] = 1;
        if(j%4==i) {
          c[i][j]=1;c[i][j + 1] = 1;
        }
      }
      for (int j = 1; j <= n; j++) {
        dp1[i][j] = dp1[i][j - 1] ^ (b[i][j] * a[j]);
        dp2[i][j] = dp2[i][j - 1] ^ (c[i][j] * a[j]);
      }
    }
    scanf("%d", &m);
    while (m--) {
      scanf("%d%d", &l, &r);
      int k = (r - l + 1) % 4;
      if (k == 0) {
        printf("0\n");
      } else if (k == 1) {
        for (int i = 0; i < 4; i++) {
          if(b[i][l]) {
            printf("%d\n", (dp1[i][l - 1] ^ dp1[i][r]));
            break;
          }
        }
      } else if (k == 3) {
        for (int i = 0; i < 4; i++) {
          if (b[i][l]==0 && b[i][l+1] == 1) {
            printf("%d\n", (dp1[i][l - 1] ^ dp1[i][r]));
            break;
          }
        }
      } else if (k == 2) {
        for (int i = 0; i < 4; i++) {
          if (c[i][l] == 1 && c[i][l + 1] == 1) {
            printf("%d\n", (dp2[i][l - 1] ^ dp2[i][r]));
            break;
          }
        }
      }
    }
  }
  return 0;
}
/*
11001100
01100110
00110011
10011001
1000  1000 1000
0100  0100 0100
0010  0010 0010
0001  0001 0001
*/
相关文章
|
6月前
|
Java
hdu 1164 Eddy's research I
hdu 1164 Eddy's research I
35 0
|
6月前
|
人工智能 Java
hdu 1165 Eddy's research II
hdu 1165 Eddy's research II
36 0
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
74 0
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
88 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
94 0
|
机器学习/深度学习
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
93 0
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
70 0
|
人工智能
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
114 0
|
机器学习/深度学习 人工智能 BI
The 15th Chinese Northeast Collegiate Programming Contest
The 15th Chinese Northeast Collegiate Programming Contest
142 0