XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和

CodeForces - 617E

给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k。(注意 i ! =  j)
维护一个前缀异或值就可以了。要注意的是 区间[l, r], 我们需要将pre[l-1]......pre[r]都加进去, pre[l-1]不能少。

#include<bits/stdc++.h>
using namespace std;
#define maxn 1234567
#define ll long long
ll s[1<<22],n,m,k,col[maxn],B,sum,ans[maxn],pre[maxn],l,r;
struct node
{
    int l,r,id;
    bool operator<(const node &b)const
    {
        return l/B==b.l/B?r<b.r:l<b.l;
    }
} a[maxn];
void del(int x)
{
    s[pre[x]]--;
    sum-=s[k^pre[x]];
}
void add(int x)
{
    sum+=s[k^pre[x]];
    s[pre[x]]++;
}
int main()
{
    r=-1;
    scanf("%lld%lld%lld",&n,&m,&k);
    for(int i=1; i<=n; i++)
    {
        scanf("%lld",&col[i]);
        pre[i]=pre[i-1]^col[i];
    }
    B=sqrt(n);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
        a[i].id=i;
        a[i].l--;
    }
    sort(a,a+m);
    for(int i=0; i<m; i++)
    {
        while(l<a[i].l)
            del(l++);
        while(r>a[i].r)
            del(r--);
        while(l>a[i].l)
            add(--l);
        while(r<a[i].r)
            add(++r);
        ans[a[i].id]=sum;
    }
    for(int i=0; i<m; i++)printf("%lld\n",ans[i]);
    return 0;
}

  

原文地址:https://www.cnblogs.com/SDUTNING/p/10261584.html

时间: 2024-07-30 12:52:36

XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和的相关文章

CodeForces - 86D 莫队算法

http://codeforces.com/problemset/problem/86/D 莫队算法就是调整查询的顺序,然后暴力求解. 每回可以通过现有区间解ans(l,r)得到区间(l+1,r),(l-1,r),(l,r+1),(l,r-1)的区间解. 调整方式http://blog.csdn.net/bossup/article/details/39236275 这题比那个还要简单,查询的是K^2*Z,很清楚就是莫队算法,然而做的时候没有学过,回来补题补到 关键是我一直没明白为什么重载小于号

P4462 [CQOI2018]异或序列 莫队 异或

题目描述 已知一个长度为n的整数数列a_1,a_2,...,a_na1?,a2?,...,an?,给定查询参数l.r,问在a_l,a_{l+1},...,a_ral?,al+1?,...,ar?区间内,有多少子串满足异或和等于k.也就是说,对于所有的x,y (I ≤ x ≤ y ≤ r),能够满足a_x \bigoplus a_{x+1} \bigoplus ... \bigoplus a_y = kax??ax+1??...?ay?=k的x,y有多少组. 输入格式 输入文件第一行,为3个整数n

莫队专题

https://blog.csdn.net/qq_41552508/article/details/100556943附上学习连接 以防万一还是搬出来吧 一.适用问题 莫队算法是一种离线算法,用分块去优化暴力,不包含修改的话,复杂度为 O(nn−−√+mn−−√) O(n\sqrt n+m\sqrt n)O(n n ? +m n ? ),n nn 为序列长度,m mm 为操作总数. 二.算法实现 莫队本质上就是用分块去优化暴力的离线算法,将总复杂度降到 O(nn−−√) O(n\sqrt n)O

Gym101138D Strange Queries 莫队、前缀和、容斥

传送门 THUWC2019D1T1撞题可还行 以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及-- 设\(que_{i,j}\)表示询问\((1,i,1,j)\)的答案,那么询问\((a,b,c,d)=que_{b,d} - que_{a-1 , d} - que_{b , c - 1} + que_{a - 1 , c - 1}\) 把一个询问拆成\(4\)个询问,然后对这\(4\)个询问莫队就可以了 不知道怎么回事THUWC上想到了莫队想到了前缀和想到了容斥就是没想到莫队+前缀

CodeForces - 617E XOR and Favorite Number (莫队+前缀和)

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,

(莫队算法)CodeForces - 617E XOR and Favorite Number

题意: 长度为n的数列,m次询问,还有一个k.每次询问询问询问从数列的L到R内有多少个连续子序列异或起来等于k. 分析: 因为事先知道这题可以用莫队写,就正好用这题练习莫队. 预处理每个前缀异或和. 然后莫队按分块排序后,不断更新,用一个数组cnt[]记录当前L到R前缀和的数量. R向右拉,新增的数量就是cnt[pre^k],pre表示当前这个R位置的前缀异或和,然后更新一下cnt. 其他的也类似. 算是一个比较好的入门题. 代码: 1 #include <cstdio> 2 #include

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks yo

codeforces 617 E. XOR and Favorite Number(莫队算法)

题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, r] 使得 al xor al+1 xor ··· ar 为 k. 题解: 本题只有区间查询没有区间修改,而且数据量不大(10w),所以可以用离线的方法解决. 使用莫队算法来解决,就需要O(1)的修改[L, R+1] .[L, R-1].[L+1, R].[L-1, R]. 详细的莫队可以百度学一

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

E. XOR and Favorite Number 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