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
*/
相关文章
|
7月前
|
Java
hdu 1164 Eddy's research I
hdu 1164 Eddy's research I
40 0
|
7月前
|
人工智能 Java
hdu 1165 Eddy's research II
hdu 1165 Eddy's research II
43 0
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
80 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
96 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
96 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
73 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
97 0
German Collegiate Programming Contest 2019 B . Bouldering (最短路)
German Collegiate Programming Contest 2019 B . Bouldering (最短路)
105 0
German Collegiate Programming Contest 2019 B . Bouldering (最短路)
|
机器学习/深度学习 人工智能 BI
The 15th Chinese Northeast Collegiate Programming Contest
The 15th Chinese Northeast Collegiate Programming Contest
146 0
|
移动开发 前端开发
ICPC Latin American Regional 2017-Imperial roads(LCA)
题目大意: 给出n个点,m条边的一个图,q个询问, 每次询问给出两个点u,v,问包含u-v这条边的最小生成树是多少 这道题比较板 首先求一下这个图的最小生成树对于这n个点,最小生成树一定是n-1条边,如果说再加上一条边,一定会构成一个环。 我们把生成的这个最小生成树看作是一个以1为根节点的最小生成树。 所以说在下面的q个询问中,如果说这条边用到了最小生成树中(这条边是最小生成树上的边),那么直接输出当前最小生成树的代价就好;如果说当前这条边没有出现在最小生成树当中,那么最小生成树的权值val加上这条边之后就构成了一个环,求出这两个点所在的环内的最大边权,并将这个边权减去,就是最终结果
120 0
ICPC Latin American Regional 2017-Imperial roads(LCA)

热门文章

最新文章