莫队

题目链接:CodeForces - 617E

题目描述:

 

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 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.

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: (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满足a[i]^a[i+1]^...^a[j]=k;

思路:

莫队,对于区间[a,b],区间[a,b+1]的ans等于[a,b]的ans加上区间[a,b]内OXR[b+1]^k的个数

     对于[a,b]的亦或和,即为XOR[b]^XOR[a-1]

   XOR[b]^XOR[a-1] == k   <==>   XOR[b]^k == XOR[a-1]

   因此寻找有多少个XOR[a-1]满足XOR[b]^XOR[a-1] == k ,即寻找有多少个XOR[b]^k

   使用一个cnt数组记录当前状态下不同区间亦或和的值出现的次数。

这边讲一下亦或的小知识:

(1)x^y=k   <==>  x^k=y   <==>   y^k=x。

(2)sumxor[r]^sum[l-1]=sum[l-r];

则插入一个数时,答案加上该数异或k的cnt值,该数的cnt值加一,删除一个数时,先该数的cnt值减一,再答案减去该数异或k的cnt值

代码

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#define ri(n) scanf("%d",&n)
#define oi(n) printf("%d\n",n)
#define rl(n) scanf("%lld",&n)
#define ol(n) printf("%lld\n",n)
#define rep(i,l,r) for(i=l;i<=r;i++)
#define rep1(i,l,r) for(i=l;i<r;i++)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int epg=10-8;
const int mod=1e9+7;
const int maxn=1e6+10;
ll ans[maxn];
int a[maxn];
int b[maxn];
int n,m,k;
int num[2*maxn];
struct node
{
    int l,r,id;
    bool operator<(const node &x)const
    {
        return b[l]<b[x.l]||(b[l]==b[x.l]&&r<x.r);
    }
}p[maxn];
void solve()
{
    int le=1,ri=0;
    ll temp=0;
    memset(ans,0,sizeof(ans));
    memset(num,0,sizeof(num));
    //cout<<1<<endl;
    for(int i=1;i<=m;i++)
    {
         while(le<p[i].l)
         {
             num[a[le]]--;
             temp-=num[a[le]^k];
             le++;
         }
         while(le>p[i].l-1)
         {
             le--;
             temp+=num[a[le]^k];
             num[a[le]]++;
         }
         while(ri<p[i].r)
         {
             ri++;
             temp+=num[a[ri]^k];
             num[a[ri]]++;
         }
         while(ri>p[i].r)
         {
             num[a[ri]]--;
             temp-=num[a[ri]^k];
             ri--;
         }
         ans[p[i].id]=temp;
         //cout<<"1"<<endl;
    }
    for(int i=1;i<=m;i++)
        printf("%lld\n",ans[i]);
}
int main()
{
    //int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)==3)
    {
        a[0]=0;
        int len=sqrt(n);
        for(int i=1;i<=n;i++)
            b[i]=(i-1)/len+1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            a[i]^=a[i-1];
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&p[i].l,&p[i].r);
            p[i].id=i;
        }
        sort(p+1,p+1+m);
        //for(int i=1;i<=m;i++)
            //printf("%d %d %d\n",p[i].l,p[i].r,p[i].id);
        solve();
    }
    return 0;
}

 

 

莫队算法是一种基于分块的算法,用于解决一些静态区间查询问题,时间复杂度为 $O(n\sqrt{n})$。以下是一个基于Python的莫队算法的示例代码: ```python import math # 定义块的大小 BLOCK_SIZE = 0 # 初始化块的大小 def init_block_size(n): global BLOCK_SIZE BLOCK_SIZE = int(math.sqrt(n)) # 定义查询操作 def query(left, right): pass # 在这里写查询操作的代码 # 定义添加操作 def add(x): pass # 在这里写添加操作的代码 # 定义删除操作 def remove(x): pass # 在这里写删除操作的代码 # 定义莫队算法 def mo_algorithm(n, q, queries): init_block_size(n) queries.sort(key=lambda x: (x[0] // BLOCK_SIZE, x[1])) left, right = 0, -1 for query in queries: while left > query[0]: left -= 1 add(left) while right < query[1]: right += 1 add(right) while left < query[0]: remove(left) left += 1 while right > query[1]: remove(right) right -= 1 query(query[0], query[1]) ``` 在这段代码中,我们首先定义了一个全局变量 `BLOCK_SIZE`,用于表示块的大小。接着,我们定义了三个操作函数 `query()`、`add()` 和 `remove()`,分别用于查询、添加和删除元素。在 `mo_algorithm()` 函数中,我们首先调用 `init_block_size()` 函数初始化块的大小,然后将查询操作按照块的大小和右端点排序,接着使用双指针维护当前查询区间的左右端点,每次移动指针时调用 `add()` 和 `remove()` 函数更新块的状态,最后调用 `query()` 函数进行查询操作。 请注意,这段代码只是一个示例,具体的 `query()`、`add()` 和 `remove()` 函数的实现取决于具体的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值