Codeforces 617E XOR and Favorite Number
Discription
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 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 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
6 2 31 2 1 1 0 31 63 5
70
5 3 11 1 1 1 11 52 41 3
944
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.
一个比较显然的做法是找出所有点对之后上主席树,然后我就RE了hhh,这说明点对还是有点多的,,,
先放一个我RE的代码,,,改完了再贴正解
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> #include<set> #include<queue> #include<vector> #define ll long long #define pb push_back #define maxn 100005 using namespace std; //g[i]是前缀异或和==i的位置链表 vector<int> g[maxn*10]; //point[i]以i为右端点的满足条件的对的左端点集合 vector<int> point[maxn]; int n,k,now,pre,sz; int pos,m,le,ri;struct node{int s,lc,rc; }nil[maxn*177]; int rot[maxn],cnt=0;int update(int u,int l,int r){int ret=++cnt;nil[ret]=nil[u];nil[ret].s++;if(l==r) return ret;int mid=l+r>>1;if(le<=mid) nil[ret].lc=update(nil[ret].lc,l,mid);else nil[ret].rc=update(nil[ret].rc,mid+1,r);return ret; }int query(int u,int l,int r){if(l>=le&&r<=ri) return nil[u].s;int an=0,mid=l+r>>1;if(le<=mid) an=query(nil[u].lc,l,mid);if(ri>mid) an+=query(nil[u].rc,mid+1,r);return an; }inline void prework(){nil->s=0;for(int i=1;i<=n;i++){rot[i]=rot[i-1];for(int j=point[i].size()-1;j>=0;j--){le=point[i][j];rot[i]=update(rot[i],1,n);}} }int main(){scanf("%d%d%d",&n,&m,&k);pre=0,g[0].pb(0);for(int i=1;i<=n;i++){scanf("%d",&now);pre^=now,now=k^pre;sz=g[now].size();for(int j=0;j<sz;j++){pos=g[now][j];point[i].pb(pos+1);}g[pre].pb(i);}prework();while(m--){scanf("%d%d",&le,&ri);printf("%d\n",query(rot[ri],1,n));}return 0; }
第二遍莫队。。。然而又RE了。。。。
(估计是点对太多不能直接存hhhhh)
等我再改改
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> #include<set> #include<queue> #include<vector> #define ll long long #define pb push_back #define maxn 100005 using namespace std; //g[i]是前缀异或和==i的位置链表 vector<int> g[maxn*10]; //point[i]以i为右端点的满足条件的对的左端点集合 vector<int> point[maxn]; vector<int> rig[maxn]; int n,k,now,pre,sz; int pos,m,le,ri,tot;/* struct node{int s,lc,rc; }nil[maxn*177]; int rot[maxn],cnt=0;int update(int u,int l,int r){int ret=++cnt;nil[ret]=nil[u];nil[ret].s++;if(l==r) return ret;int mid=l+r>>1;if(le<=mid) nil[ret].lc=update(nil[ret].lc,l,mid);else nil[ret].rc=update(nil[ret].rc,mid+1,r);return ret; }int query(int u,int l,int r){if(l>=le&&r<=ri) return nil[u].s;int an=0,mid=l+r>>1;if(le<=mid) an=query(nil[u].lc,l,mid);if(ri>mid) an+=query(nil[u].rc,mid+1,r);return an; }inline void prework(){nil->s=0;for(int i=1;i<=n;i++){rot[i]=rot[i-1];for(int j=point[i].size()-1;j>=0;j--){le=point[i][j];rot[i]=update(rot[i],1,n);}} } */struct node{int l,r,bl,num;bool operator <(const node& u)const{return bl==u.bl?((bl&1)?r<u.r:r>u.r):bl<u.bl;} }q[maxn]; int ans[maxn];inline void adl(int x){tot+=upper_bound(rig[x].begin(),rig[x].end(),ri)-rig[x].begin(); }inline void del(int x){tot-=upper_bound(rig[x].begin(),rig[x].end(),ri)-rig[x].begin(); }inline void adr(int x){tot+=point[x].size()-(lower_bound(point[x].begin(),point[x].end(),le)-point[x].begin()); }inline void der(int x){tot-=point[x].size()-(lower_bound(point[x].begin(),point[x].end(),le)-point[x].begin()); }int main(){scanf("%d%d%d",&n,&m,&k);pre=0,g[0].pb(0);for(int i=1;i<=n;i++){scanf("%d",&now);pre^=now,now=k^pre;sz=g[now].size();for(int j=0;j<sz;j++){pos=g[now][j];//枚举顺序保证了两个链表的元素都是升序的 point[i].pb(pos+1);rig[pos+1].pb(i);}g[pre].pb(i);}sz=sqrt(n);for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].bl=(q[i].l-1)/sz+1;q[i].num=i;}sort(q+1,q+m+1);le=1,ri=0;for(int i=1;i<=m;i++){while(le<q[i].l) del(le),le++;while(le>q[i].l) le--,adl(le);while(ri<q[i].r) ri++,adr(ri);while(ri>q[i].r) der(ri),ri--;ans[q[i].num]=tot;}for(int i=1;i<=m;i++) printf("%d\n",ans[i]);return 0; }
虽然现在A了但是首先声明一下之前让我RE的可能不是炸数组而是xor出的数>=1000000,,,,,
现在写了一个正确的莫队。。。。
其实不用计点对,现算也来得及(二分)
Accepted #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> #include<set> #include<queue> #include<vector> #define ll long long #define pb push_back #define maxn 100005 using namespace std; //g[i]是前缀异或和==i的位置链表 vector<int> g[maxn*33]; int n,k,now,pre,sz,a[maxn]; int pos,m,le,ri; ll tot=0;struct node{int l,r,bl,num;bool operator <(const node& u)const{return bl==u.bl?((bl&1)?r<u.r:r>u.r):bl<u.bl;} }q[maxn]; ll ans[maxn];inline void adl(int x){int to=k^a[x-1];if(!g[to].size()) return;tot+=(ll)(upper_bound(g[to].begin(),g[to].end(),ri)-lower_bound(g[to].begin(),g[to].end(),x)); }inline void del(int x){//因为对于本题插入和删除带来的数值影响相同,//所以不用反着操作 int to=k^a[x-1];if(!g[to].size()) return;tot-=(ll)(upper_bound(g[to].begin(),g[to].end(),ri)-lower_bound(g[to].begin(),g[to].end(),x)); }inline void adr(int x){int to=k^a[x];if(!g[to].size()) return;tot+=(ll)(lower_bound(g[to].begin(),g[to].end(),x)-lower_bound(g[to].begin(),g[to].end(),le-1)); }inline void der(int x){int to=k^a[x];if(!g[to].size()) return;tot-=(ll)(lower_bound(g[to].begin(),g[to].end(),x)-lower_bound(g[to].begin(),g[to].end(),le-1)); }int main(){scanf("%d%d%d",&n,&m,&k);g[0].pb(0);for(int i=1;i<=n;i++){scanf("%d",a+i);a[i]^=a[i-1];g[a[i]].pb(i);}sz=sqrt(n);for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].bl=(q[i].l-1)/sz+1;q[i].num=i;}sort(q+1,q+m+1);le=1,ri=0;for(int i=1;i<=m;i++){while(le<q[i].l) del(le),le++;while(le>q[i].l) le--,adl(le);while(ri<q[i].r) ri++,adr(ri);while(ri>q[i].r) der(ri),ri--;ans[q[i].num]=tot;}for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);return 0; }
转载于:https://www.cnblogs.com/JYYHH/p/8371381.html
Codeforces 617E XOR and Favorite Number相关推荐
- 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 g ...
- codeforces 617E XOR and Favorite Number 莫队
https://vjudge.net/problem/CodeForces-617E 题目大意:给nnn个数,mmm个询问,以及一个数kkk,每次询问要输出[l,r][l,r][l,r]内满足a[i] ...
- CodeForces -617E XOR and Favorite Number(莫队)
题目链接:点击这里 题目大意: 给定一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an ,再给出一个数字 kkk , mmm 组询问每组询 ...
- CodeForces - 617E XOR and Favorite Number(莫队)
题目链接:点击查看 题目大意:给出一个由n个数组成的数列,现在给出m组询问,每次询问包含一个l和一个r,要求回答在闭区间[l,r]中有多少组(i,j)满足[i,j]闭区间内的所有数的异或和等于k 题目 ...
- CodeForces 617 E. XOR and Favorite Number
题目链接: CodeForces 617 E. XOR and Favorite Number 题目描述: 给出n个数,m次查询,每次查询在区间[l, r]里面有多少对(i, j),满足ai ^ ai ...
- 【CF 617E】 XOR and Favorite Number (Mo's algorithm)
[CF 617E] XOR and Favorite Number (Mo's algorithm) E. XOR and Favorite Number time limit per test 4 ...
- CF617E. XOR and Favorite Number
1 /* 2 CF617E. XOR and Favorite Number 3 http://codeforces.com/contest/617/problem/E 4 莫队算法 5 题意:求l, ...
- CF617E XOR and Favorite Number
CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...
- Codeforces617E XOR and Favorite Number (普通莫队)
题目链接: XOR and Favorite Number 2021.11.11更新了解题思路和代码 大致题意 给定一个长度为nnn的序列aaa, 和一个固定的数值kkk. 有mmm次询问, 每次询问 ...
最新文章
- 百度CTO王海峰阐释AI融合创新,降低门槛,按下产业智能化加速键
- 关于eclipse 中文注释时中文字体太小的问题解决
- 深入理解分布式技术 - 先更新数据库,还是先更新缓存
- JenKins使用pm2部署.net core网站
- 玩转NumPy——NumPy数组的切片和索引
- STM32的FLASH ID加密
- C语言typedefine 和define的区别
- 蓝桥杯大赛基础之--数列排序
- 苹果天气不显示_热门天气APP被苹果拿下,安卓用户不能再使用
- 什么是JavaScript垃圾回收?
- 正则表达式匹配第一个单个字符
- DNF私服搭建的利弊关系
- Java之—hutool工具类二维码生成跟背景图合并输出
- Linux之LAMP架构
- 比特率(码率) = 采样率 (Sampling rate ) * 位深 (Bit depth)* 声道数目/Opus/AAC/mp3
- 我的春招实习+秋招总结【前端开发】
- IP被反垃圾邮件组织列入SBL,发送邮件被退回的解决方法
- 宁波银行成长的AB面:增长与合规不可兼得?管理漏洞不容忽视
- Unable to find instance for XXX
- 咖啡技术培训:传统意式咖啡菜单制作配方及流程
热门文章
- showModalDialog跨域访问的解决
- [团队公告]第二次技术交流主题征集
- OpenCV提示—“CV_BGR2HSV”: 未声明的标识符—解决
- java 无法找到ant_无法找到与ANT(mac)关联的文件
- header+php+xiazai_php使用header函数实现文件下载
- springboot jar服务器运行后无法请求_Spring Boot微服务中Chaos Monkey的应用
- HTML元素水平居中和垂直居中
- mysql extended_explain之三:MYSQL EXPLAIN语句的extended 选项学习体会,分析诊断工具之二...
- ncbi查找目的基因序列_基于PrimerBank和NCBI数据库的引物查找与设计
- php 数据中心,数据层 · Thinkphp 独立数据中心使用手册 · 看云