zoukankan      html  css  js  c++  java
  • CoderForces-617B

    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, ..., ajis equal to k.

    Input

    The first line of the input contains integers nm and k (1 ≤ n, m ≤ 100 0000 ≤ 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.

    Output

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

    Example
    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: (12), (14), (15), (23), (36), (56), (66). 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.



    题意:这道题意思为:给你一行数,让你查找在这行数中有多少子区间满足其内的数亦或为K;

    题解:可以让sum[i]表示从1亦或到i的值;sum[i]^=sum[i-1];

    sum[r]=k^sum[l-1];(l,r分别表示左右端点)

    AC代码为:

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 4;
    struct node
    {
    	friend bool operator< (node x, node y)
    	{
    		if (x.pos == y.pos)return x.r<y.r;
    		return x.l<y.l;
    	}
    	int l, r, id;
    	int pos;
    };
    node qu[N];
    int n, m, k;
    int b[N], num[2 * N];
    long long ans[N];
    void solve()
    {
    	memset(num, 0, sizeof(num));
    	int le = 1, ri = 0;
    	long long temp = 0;
    	for (int i = 1; i <= m; i++)
    	{
    		while (ri<qu[i].r)
    		{
    			ri++;
    			temp += num[b[ri] ^ k];
    			num[b[ri]]++;
    		}
    		while (ri>qu[i].r)
    		{
    			num[b[ri]]--;
    			temp -= num[b[ri] ^ k];
    			ri--;
    		}
    		while (le>qu[i].l - 1)
    		{
    			le--;
    			temp += num[b[le] ^ k];
    			num[b[le]]++;
    		}
    		while (le<qu[i].l - 1)
    		{
    			num[b[le]]--;
    			temp -= num[b[le] ^ k];
    			le++;
    		}
    		ans[qu[i].id] = temp;
    	}
    }
    int main()
    {
    	b[0] = 0;
    	scanf("%d%d%d", &n, &m, &k);
    	for (int i = 1; i <= n; i++)
    	{
    		scanf("%d", &b[i]);
    		b[i] ^= b[i - 1];
    	}
    	int s = sqrt(n);
    	for (int i = 1; i <= m; i++)
    	{
    		scanf("%d%d", &qu[i].l, &qu[i].r);
    		qu[i].id = i;
    		qu[i].pos = qu[i].l / s;
    	}
    	sort(qu + 1, qu + m + 1);
    	solve();
    	for (int i = 1; i <= m; i++)
    	{
    		cout << ans[i] << "
    ";
    	}
    	return 0;
    }



  • 相关阅读:
    golang包引用解析
    解决vs code中golang插件依赖安装失败问题
    设计模式之策略模式
    设计模式之模板方法
    设计模式——面向对象设计原则
    设计模式——个人浅谈
    Sublime Text3配置Lua运行环境
    Ntrip协议使用流程及服务器实现架构
    python3解析库lxml
    python3爬虫之Urllib库(二)
  • 原文地址:https://www.cnblogs.com/csushl/p/9386595.html
Copyright © 2011-2022 走看看