2018南昌网络赛K题

题目链接:https://nanti.jisuanke.com/t/38230

Description
Given a sequence of nn numbers a1,a2,…,anand three functions.
Define a function f(l,r)f(l,r) which returns ⊕a[x] (l≤x≤r). The ⊕ represents exclusive OR.
Define a function g(l,r)g(l,r) which returns ⊕f(x,y)(l≤x≤y≤r).
Define a function w(l,r)w(l,r) which returns ⊕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), 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<=109)
Line 3:q(1≤q≤100000), the number of xor-queries.
In the next q lines, each line contains 2 numbers i,j representing a xor-query (1≤i≤j≤n).
It is guaranteed that sum of n and q <=106

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

Sample Input
1
5
1 2 3 4 5
5
1 3
1 5
1 4
4 5
3 5

Sample Output
2
4
0
1
4

思路
观察样例觉得应该是按区间长度找规律,然后就打了表
avatar
第i行代表区间长度为i时每一位所做的贡献
然后发现是4的循环节
然后dp一下,dp[i][j]表示以第i位为查询的左端点区间长度为第j种(1<=j<=4),到第n位的答案。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e6 + 10;
ll dp[maxn][5];
ll a[maxn];
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, m;
scanf("%d", &n);
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
memset(dp, 0, sizeof(dp));
dp[n][1] = a[n];
dp[n][2] = a[n];
for (int i = n - 1; i >= 1; i--) {
dp[i][1] = dp[i + 4][1] ^ a[i];
dp[i][2] = dp[i + 4][2] ^ a[i] ^ a[i + 1];
dp[i][3] = dp[i + 4][3] ^ a[i + 1];
dp[i][4] = dp[i + 4][4];
//printf("%d:\nflag=1:%d\nflag=2:%d\nflag=3:%d\nflag=4:%d\n", i, dp[i][1], dp[i][2], dp[i][3], dp[i][4]);
}
scanf("%d", &m);
while (m--) {
int l, r;
scanf("%d%d", &l, &r);
int flag = (r - l) % 4 + 1;
if (flag == 1)
printf("%lld\n", dp[l][flag] ^ dp[r + 4][flag]);
else if (flag == 2)
printf("%lld\n", dp[l][flag] ^ dp[r + 3][flag]);
else if (flag == 3)
printf("%lld\n", dp[l][flag] ^ dp[r + 2][flag]);
else
printf("0\n");
}
}
//system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值