Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】
2021-07-10 00:06
标签:force 有一个 ble for arrays fine file rop 数据 任意门:http://codeforces.com/problemset/problem/617/E Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l?≤?i?≤?j?≤?r and the xor of the numbers ai,?ai?+?1,?...,?aj is equal to k. The first line of the input contains integers n, m and k (1?≤?n,?m?≤?100?000, 0?≤?k?≤?1?000?000) — the length of the array, the number of queries and Bob‘s favorite number respectively. The second line contains n integers ai (0?≤?ai?≤?1?000?000) — Bob‘s array. Then m lines follow. The i-th line contains integers li and ri (1?≤?li?≤?ri?≤?n) — the parameters of the i-th query. Print m lines, answer the queries in the order they appear in the input. In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query. In the second sample xor equals 1 for all subarrays of an odd length. 有一串长度为 N 的数列,M次查询 ( l,r )内有多少对 ( i, j )使得 ai ^ ai+1 ^ ... ^ aj = K; 这里的区间查询是离线的,可以用传说中的莫队算法(优雅而华丽的暴力算法)。 异或的题目有一个很巧妙的地方就是利用 a^b^a = b 这个性质。 这道题目我们也要预处理一下 sum(i) 前 i 个数的异或和, 那么 ai ^ ai+1 ^ ... ^ aj = sum( i - 1) ^ sum( j ) 了。 接下来我们就可以按照查询的区间用莫队算法遍历一遍,同时记录当前区间某个前缀和的出现次数; 根据 sum( i - 1) ^ sum( j ) = K ,K ^ sum( i - 1 ) = sum( j ) || K ^ sum( j ) = sum( i - 1) ,由符合条件的前缀和次数来推出符合条件的( i,j )的个数啦。 听了大神的课,这道题有两个坑: 1、答案的数据的数据范围是爆 int 的; 2、虽然是1e6 的 K,但异或的结果要大于 1e6 ; AC code: Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】 标签:force 有一个 ble for arrays fine file rop 数据 原文地址:https://www.cnblogs.com/ymzjj/p/9563453.htmlE. XOR and Favorite Number
Input
Output
Examples
6 2 3
1 2 1 1 0 3
1 6
3 57
05 3 1
1 1 1 1 1
1 5
2 4
1 39
4
4题目大意:
解题思路:
Tip:
1 #include
上一篇:springMvc项目配置步骤
下一篇:数组的某一维的索引为负数
文章标题:Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】
文章链接:http://soscw.com/index.php/essay/102987.html