Codeforces Round #340 (Div. 2) E. XOR and Favorite Number

因为x^k=y,x^x=0,所以求出异或前缀和,\(sum_i\)^k=\(sum_j\),则区间i+1到j,异或值=k。所以我们运用莫队进行统计,唯一的问题在于,这次维护的区间l~r,表示的l+1~r的值,所以莫队维护的区间是左开右闭。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#pragma GCC optimize(2)
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
    char ch = getchar(); ll x = 0, f = 1;
    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 2e5 + 10;
map<int, int>mp;
int siz;
int n, m, k;
int a[N], sum[N], sum_ls[N], xor_sum[N];
ll vis[3 * N];
int belong[N];
struct qr {
    int l, r, id;
    bool operator<(const qr a)const {
        if (belong[l] == belong[a.l])
            return r < a.r;
        else return l < a.l;
    }
}m_qr[N];
int getid(int x)
{
    return mp[x] ? mp[x] : mp[x] = ++siz;
}
ll ans[N];
int l = 1, r = 0;
ll adsum = 0;
void add(int x,int y)
{
    adsum += vis[x];
    vis[y]++;
}
void del(int x,int y) {
    vis[y]--;
    adsum -= vis[x];
}
int main()
{
    n = read(); m = read(); k = read();
    int block = sqrt(n);
    upd(i, 1, n)a[i] = read(), belong[i] = (i - 1) / block + 1;
    upd(i, 1, n)sum[i] = sum[i - 1] ^ a[i], xor_sum[i] = sum[i] ^ k;
    upd(i, 1, n) { 
        sum_ls[i] = getid(sum[i]);
        xor_sum[i] = getid(xor_sum[i]);
    }
    sum_ls[0] = getid(0); vis[sum_ls[0]]++;
    xor_sum[0] = getid(k);
    int l1, r1;
    upd(i, 1, m)
    {
        l1 = read(), r1 = read();
        m_qr[i].l = l1; m_qr[i].r = r1; m_qr[i].id = i;
    }
    sort(m_qr + 1, m_qr + m + 1);
    upd(i, 1, m)
    {
        while (l < m_qr[i].l)del(xor_sum[l - 1], sum_ls[l - 1]), l++;
        while (l > m_qr[i].l)--l, add(xor_sum[l - 1], sum_ls[l - 1]);
        while (r < m_qr[i].r)r++,add(xor_sum[r], sum_ls[r]);
        while (r > m_qr[i].r)del(xor_sum[r], sum_ls[r]),r--;
        ans[m_qr[i].id] = adsum;
    }
    upd(i, 1, m)printf("%lld\n", ans[i]);
    return 0;
}
上一篇:jQuery事件绑定on()、bind()与delegate() 方法详解


下一篇:QR容错级别