zoukankan      html  css  js  c++  java
  • Codeforces 617E

    E. XOR and Favorite Number

    time limit per test4 seconds
    memory limit per test256 megabytes
    input standard input
    outpu tstandard output
    Bob has a favorite number k and a i of length n. Now he asks you to answer m queries. Each query is given by a pair l i and r i 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 a i, a i + 1, …, a j is equal to k.

    Input

    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 a i (0 ≤ a i ≤ 1 000 000) — Bob’s array.

    Then m lines follow. The i-th line contains integers l i and r i (1 ≤ l i ≤ r i ≤ n) — the parameters of the i-th query.

    Output

    Print m lines, answer the queries in the order they appear in the input.

    Examples
    input
    6 2 3
    1 2 1 1 0 3
    1 6
    3 5
    output
    7
    0
    input
    5 3 1
    1 1 1 1 1
    1 5
    2 4
    1 3
    output
    9
    4
    4

    Note

    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 k ,然后第二行输入n个数,再输入m行数,每行输入两个数l ,r 表示有n个数,m个询问,对于每个询问,你应该输出区间[l, r] 中ai ^ … ^ aj = k 这样的i, j 有多少对。

    解题思路:

    一道很好的莫队算法题,只有查询没有修改,可以离线用莫队去做,我们需要输出有几对,开ans 数组表示答案,belong 数组表示每个数属于哪个块(莫队需要把n 个数分块,每块根号n),结构体q存每次的询问,cnt[i] 表示前缀异或值为 i 的数出现了几次,套用莫队算法模板,改一下add和del函数。
    因为求的是ai ^… ^ aj == k,我们可以维护一个前缀异或,a[j] ^ a[i - 1] 即为 ai ^… ^ aj(这里的数组a表示前缀异或,和前面的意义不同) ,异或还有一个性质,我们要求的是a[i - 1] ^ a[j] = k 那么 a[j] ^ k = a[i - 1],对于每次答案的更新,实质上就是加上a[j] 和 k 去找对应匹配的a[i - 1]的值出现的次数。也就是说 每次 now += cnt[a[x] ^ k] ,del同理。要注意的是这里cnt[0] 要初始化为 1,拿样例来说,1 2 1 1 0 3 区间 1 6 ,前两个数的异或值为3,这时 cnt[a[2] ^ 3] 而a[2] ^ 3 的值为0,实际上他的值应该为 1。之后套用莫队的模板即可 AC。

    Code:

    #pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    const int N = 5e6 + 50;
    ll cnt[N], a[N], belong[N], ans[N], now = 0;
    int n, m, k;
    struct ask { int l, r, id; }q[N];
    bool cmp(ask a, ask b)
    {
    	return belong[a.l] ^ belong[b.l] ? belong[a.l] < belong[b.l] : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
    }
    void add(int x)
    {
    	now += cnt[a[x] ^ k];//这里要先更新now 再++,防止与a[x] 和 a[x] 进行匹配,匹配结果会出错,因为a[l - 1] ^ a[r] = a[l] ^ a[r] , a[x] a[x] 不合法
    	cnt[a[x]] ++;
    }
    void del(int x)
    {
    	cnt[a[x]] --;
    	now -= cnt[a[x] ^ k];
    }
    int main()
    {
    	scanf("%d%d%d", &n, &m, &k);
    	for (int i = 1; i <= n; i ++)//输入的同时初始化前缀异或数组
    	{
    		scanf("%d", &a[i]);
    		a[i] = a[i - 1] ^ a[i];
    	}
    	double block = sqrt(n);//数据分块
    	int num = ceil(n / block);
    	for (int i = 1; i <= num; i ++)
    		for (int j = (i - 1) * block + 1; j <= i * block; j ++)
    			belong[j] = i;
    	for (int i = 1; i <= m; i ++)
    	{
    		scanf("%d%d", &q[i].l, &q[i].r);
    		q[i].id = i;
    	}
    	sort(q + 1, q + 1 + m, cmp);
    	int l = 1, r = 0;
    	cnt[0] = 1;
    	for (int i = 1; i <= m; i ++)
    	{
    		int ql = q[i].l, qr = q[i].r;
    		while(l < ql) del(l - 1), l ++;
    		while(l > ql) l --, add(l - 1);
    		while(r < qr) r ++, add(r);
    		while(r > qr) del(r), r --;
    		ans[q[i].id] = now;
    	}
    	for (int i = 1; i <= m; i ++)
    		cout << ans[i] << endl;
    	return 0;
    }
    
  • 相关阅读:
    Matlab 画图
    OfferCome-0531
    OfferCome--0528
    剑指offer(2)
    剑指offer(1)
    MySQL的自增主键
    java Junit 测试
    sql 注入问题
    Facebook Libra
    markdown的博客
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294152.html
Copyright © 2011-2022 走看看