题解-CF617E XOR and Favorite Number
题面
给定 \(n,m,k\) 和 \(n\) 个数的序列 \(a_i\),\(m\) 次求区间 \([l,r]\) 中异或值为 \(k\) 的子序列个数。
数据范围:\(1\le n,m\le 10^5\),\(0\le k,a_i\le 10^6\)。
蒟蒻语
这题的莫队做法太一眼,谁都会是吧(
那么蒟蒻就来写一个过不了这题(会 MLE、TLE)但是可以在线且很有趣的做法:分块。
说不定谁去开大限制卡个离线蒟蒻解就成正解了。
蒟蒻解
设 \(f(l,r)\) 表示区间 \([l,r]\) 的异或值为 \(k\) 的子区间数。
设 \(cross(l,r)\) 表示区间左端点在 \([1,l]\) 右端点在 \([r,n]\) 的异或值为 \(k\) 的区间数。
先容斥一下(\(cross(l-1,r+1)\) 为了抵消掉 \(f(1,n)\) 中多余的部分):
\]
这东西前 \(3\) 项可以时空 \(\Theta(n)\) 求出,只是最后一项太恶心了,蒟蒻的方法是分块时空 \(\Theta(n\sqrt{n})\)(巨佬如果有更优做法一定要指教蒟蒻)。
设有 \(b_n\) 个块,每个块为 \([l_i,r_i]\)。
先预处理 \(i<j:cro_{i,j}=cross(r_i,l_j)\),做法同莫队,前缀和加 unordered_map。
再记 \(lc_{i,c}\) 表示 \([1,r_i]\) 中前缀和为 \(c\) 的个数,\(rc_{i,c}\) 表示 \([l_i,n]\) 中前缀和为 \(c\) 的个数。
求 \(cross(l,r)\) 时,设 \(lb=\max_{i=1}^{b_n} r_i\le l\),\(rb=\min_{i=1}^{b_n} l_i\ge r\)。
结果就是 \(cro_{lb,rb}\) 再加上
左端点在 \([r_{lb}+1,l]\) 右端点在 \([l_{rb},n]\) 的答案(用 \(rc\) 数组求)
和左端点在 \([1,r_{lb}]\) 右端点在 \([r,l_{rb}-1]\) 的答案(用 \(lc\) 数组求)
和左端点在 \([r_{lb}+1,l]\) 右端点在 \([r,l_{rb}-1]\) 的答案(用前缀和加 unordered_map)。
然后这题就做完了,别忘了这个做法过不了。
代码
#include <bits/stdc++.h>
using namespace std;
//Start
typedef long long ll;
typedef double db;
#define mp(a,b) make_pair(a,b)
#define x first
#define y second
#define be(a) a.begin()
#define en(a) a.end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
//Data
const int N=1e5;
int n,m,k,sm[N+2];
int a[N+1]; ll la[N+2],ra[N+2];
unordered_map<int,int> cnt;
//Block
const int B=320;
int sz,b[N+1];
ll cro[B+2][B+2];
unordered_map<int,int> lc[B+2],rc[B+2];
void build(){
sz=sqrt(n);
for(int i=1;i<=n;i++) b[i]=(i-1)/sz+1;
for(int i=1;i<=b[n];i++)
for(int j=1;j<=min(sz*i,n);j++) lc[i][sm[j-1]]++;
for(int i=1;i<=b[n];i++)
for(int j=n;j>=sz*(i-1)+1;j--) rc[i][sm[j]]++;
for(int i=1;i<=b[n];i++){
for(int t=b[n];t>i;t--){
for(int j=sz*(t-1)+1;j<=min(sz*t,n);j++)
cro[i][t]+=lc[i][sm[j]^k];
cro[i][t]+=cro[i][t+1];
}
}
}
ll cross(int l,int r){
if(l<0||r>n) return 0;
int lb=b[l+1]-1,rb=b[r-1]+1;
ll res=cro[lb][rb];
cnt.clear();
for(int i=lb*sz+1;i<=l;i++) cnt[sm[i-1]]++;
for(int i=r;i<=min((rb-1)*sz,n);i++) res+=cnt[sm[i]^k];
for(int i=lb*sz+1;i<=l;i++) res+=rc[rb][sm[i-1]^k];
for(int i=r;i<=min((rb-1)*sz,n);i++) res+=lc[lb][sm[i]^k];
return res;
}
//Main
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) sm[i]=sm[i-1]^a[i];
cnt.clear(),cnt[sm[0]]++;
for(int i=1;i<=n;i++) la[i]=la[i-1]+cnt[sm[i]^k],cnt[sm[i]]++;
cnt.clear(),cnt[sm[n]]++;
for(int i=n;i>=1;i--) ra[i]=ra[i+1]+cnt[sm[i-1]^k],cnt[sm[i-1]]++;
assert(la[n]==ra[1]);
// for(int i=1;i<=n;i++) cout<<la[i]<<' ';cout<<'\n';
// for(int i=1;i<=n;i++) cout<<ra[i]<<' ';cout<<'\n';
build();
for(int i=0;i<m;i++){
int l,r; cin>>l>>r;
cout<<ra[l]+la[r]-la[n]+cross(l-1,r+1)<<'\n';
}
return 0;
}
祝大家学习愉快!
题解-CF617E XOR and Favorite Number的更多相关文章
- CF617E XOR and Favorite Number
CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...
- [CF617E]XOR and Favorite Number/[CQOI2018]异或序列
题目大意: 给定一个长度为$n(n\leq10^5)$的数列$A$和数$k$$(A_i,k\leq10^6)$.$m$组询问,每次询问区间$[l,r]$中有多少对$i,j(l\leq i\leq j\ ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- 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 ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Codeforeces 617E XOR and Favorite Number(莫队+小技巧)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- XOR and Favorite Number (莫对算法)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- 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 ...
随机推荐
- kettle练习
Kettle实现,把数据从CSV文件复制到Excel文件. 首先,创建一个转换,找到核心对象,找到输入里面的CVS文件输入图元,拖拽到工作区域,双击CVS文件输入. 可以修改步骤的名称,点击浏览,选择 ...
- Shodan搜索引擎详解及Python命令行调用
shodan常用信息搜索命令 shodan配置命令 shodan init T1N3uP0Lyeq5w0wxxxxxxxxxxxxxxx //API设置 shodan信息收集 shodan myip ...
- Route_of_Linux
爬过六个陡坡,对Linux了如指掌 本文是极客时间App中刘超老师趣谈Linux操作系统的学习路径课程的学习笔记 抛弃旧思维习惯,熟练使用命令行 要从Windows的思维习惯,切换成Linux的命令行 ...
- Folx专业版智能速控功能详解
限速功能指的是,用户可以通过限制最大上传.下载速度来控制任务下载的带宽使用,减少因下载导致其他应用程序出现网络延迟的情况.Folx不仅为用户提供简单的任务限速功能,而且还提供更加智能的速控功能,供用户 ...
- 使用Camtasia制作我的观影报告
最近抖音兴起做<我的观影报告>风潮.<我的观影报告>是通过剪辑影片+旁白的方法,将自己观看过的影片安利给观众的方式.如果大家想要制作这类型的观影报告,建议使用Camtasia( ...
- JVM垃圾回收器、内存分配与回收策略
新生代垃圾收集器 1. Serial收集器 serial收集器即串行收集器,是一个单线程收集器. 串行收集器在进行垃圾回收时只使用一个CPU或一条收集线程去完成垃圾回收工作,并且会暂停其他的工作线程( ...
- python+selenium利用cookie记住密码
先上代码 1 from selenium import webdriver 2 from time import sleep 3 4 dr = webdriver.Chrome() 5 dr.get( ...
- Python变量引用
>>>a=3 >>>b=a >>>a=4 >>>b >>>3 >>>List1=[1,2,3 ...
- 安装Linux软件时遇到这个问题,如何解决?
提示 Could not get lock /var/lib/dpkg/lock 报错? 有些小伙伴在使用 apt 包管理器更新或安装软件时,可能会遇到过诸如以下的错误提示: E: Could not ...
- 跟随杠精的视角一起来了解Redis的主从复制
不想弹好吉他的撸铁狗,都不是好的程序猿 虽然说单机的Redis性能很好,也有完备的持久化机制,那如果你的业务体量真的很大,超过了单机能够承载的上限了怎么办?不做任何处理的话Redis挂了怎么办?带着这 ...