【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken
题意:You are given an array A of N non-negative integers and an integer M.
Find the number of pair(i,j) such that 1≤i≤j≤N and min(Ai,Ai+1,...,Aj)⋅(Ai⊕Ai+1⊕...⊕Aj)≤M.
先用把数字从小到大依次插入,用个set预处理出每个最小值控制的子区间的范围,对于大小相同的数,靠左的数优先。
然后对于每个子区间,最小值将它划分成左右两段,枚举较短的那段,在较长的那段中用可持久化Trie查询满足条件的对数。前缀异或和别忘了处理,很经典了。
就是设M/min(Ai,Ai+1,...,Aj)为W,每次向左右儿子查询,如果W的这位是零,那就不累计答案,直接朝零(这里指异或上左端点的前缀和以后的零)走;如果W的这位是一,那就统计上零(这里指异或上左端点的前缀和以后的零)的子树的答案,然后朝一走。别忘了到叶子以后还要在累计一次。(叶子必然满足条件,可以累计。)
- #include<cstdio>
- #include<set>
- #include<algorithm>
- using namespace std;
- typedef set<int>::iterator ITER;
- typedef long long ll;
- int n;
- ll m,a[100005];
- int b[100005],lb[100005],ub[100005];
- int w,xors[100005];
- bool cmp(const int &x,const int &y){
- return (a[x]!=a[y] ? a[x]<a[y] : x<y);
- }
- ll ans;
- #define N 100005
- #define MAXBIT 31
- int root[N],ch[N*(MAXBIT+1)][2],sz[N*(MAXBIT+1)],tot;
- void add(int now,int W)
- {
- int old=root[now-1];
- root[now]=++tot;
- now=root[now];
- for(int i=MAXBIT;i>=1;--i)
- {
- int Bit=((W>>(i-1))&1);
- sz[now]=sz[old]+1;
- ch[now][Bit^1]=ch[old][Bit^1];
- ch[now][Bit]=++tot;
- now=ch[now][Bit];
- old=ch[old][Bit];
- }
- sz[now]=sz[old]+1;
- }
- void query(int bef,int L,int R,int W,int fl)
- {
- L=root[L];R=root[R+1];
- for(int i=MAXBIT;i>=1;--i){
- int Bit=(W>>(i-1)&1);
- if(Bit==1){
- if((xors[bef-fl]>>(i-1)&1)==1){
- ans+=(ll)(sz[ch[R][1]]-sz[ch[L][1]]);
- R=ch[R][0];
- L=ch[L][0];
- }
- else{
- ans+=(ll)(sz[ch[R][0]]-sz[ch[L][0]]);
- R=ch[R][1];
- L=ch[L][1];
- }
- }
- else{
- if((xors[bef-fl]>>(i-1)&1)==1){
- R=ch[R][1];
- L=ch[L][1];
- }
- else{
- R=ch[R][0];
- L=ch[L][0];
- }
- }
- }
- ans+=(ll)(sz[R]-sz[L]);
- }
- int main(){
- //freopen("f.in","r",stdin);
- scanf("%d%lld",&n,&m);
- for(int i=1;i<=n;++i){
- scanf("%lld",&a[i]);
- xors[i]=(xors[i-1]^a[i]);
- }
- for(int i=1;i<=n;++i){
- b[i]=i;
- }
- sort(b+1,b+n+1,cmp);
- set<int> S;
- for(int i=1;i<=n;++i){
- ITER it=S.lower_bound(b[i]);
- if(it!=S.end()){
- ub[b[i]]=((*it)-1);
- }
- else{
- ub[b[i]]=n;
- }
- if(it==S.begin()){
- lb[b[i]]=1;
- }
- else{
- --it;
- lb[b[i]]=((*it)+1);
- }
- S.insert(b[i]);
- }
- add(1,0);
- for(int i=1;i<=n;++i){
- add(i+1,xors[i]);
- }
- for(int i=1;i<=n;++i){
- if(m/a[i]>=(1ll<<31)){
- ans+=(ll)(i-lb[i]+1)*(ll)(ub[i]-i+1);
- continue;
- }
- w=(int)(m/a[i]);
- if(i-lb[i]<=ub[i]-i){
- for(int j=lb[i];j<=i;++j){
- query(j,i,ub[i],w,1);
- }
- }
- else{
- for(int j=i;j<=ub[i];++j){
- query(j,lb[i]-1,i-1,w,0);
- }
- }
- }
- printf("%lld\n",ans);
- return 0;
- }
【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken的更多相关文章
- The 15th UESTC Programming Contest Preliminary K - Kidd1ng Me? cdoj1565
地址:http://acm.uestc.edu.cn/#/problem/show/1565 题目: Kidd1ng Me? Time Limit: 3000/1000MS (Java/Others) ...
- 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem
题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
- 【推导】The 16th UESTC Programming Contest Preliminary L - Foxtrot
题意:有n瓶药剂,其中只有一瓶药剂有毒.让你用最少的小白鼠试出哪瓶有毒.你只有一次给任意只小白鼠各喂食任意种类药剂的机会. m只老鼠就能对应2^m种“生死状态”的组合,给每种状态分配一个种类的药剂,然 ...
- The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567
地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...
- The 15th UESTC Programming Contest Preliminary C - C0ins cdoj1554
地址:http://acm.uestc.edu.cn/#/problem/show/1554 题目: C0ins Time Limit: 3000/1000MS (Java/Others) M ...
- The 15th UESTC Programming Contest Preliminary B - B0n0 Path cdoj1559
地址:http://acm.uestc.edu.cn/#/problem/show/1559 题目: B0n0 Path Time Limit: 1500/500MS (Java/Others) ...
- The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557
地址:http://acm.uestc.edu.cn/#/problem/show/1557 题目: Minimum C0st Time Limit: 3000/1000MS (Java/Others ...
- The 16th UESTC Programming Contest Final 游记
心情不好来写博客. 为了满足ykk想要气球的愿望,NicoDafaGood.Achen和我成功去神大耍了一圈. 因为队名一开始是LargeDumpling应援会,然后队名被和谐,变成了学校的名字,顿时 ...
- The 15th UESTC Programming Contest Preliminary G - GC?(X,Y) cdoj1564
地址:http://acm.uestc.edu.cn/#/problem/show/1564 题目: G - GC?(X,Y) Time Limit: 3000/1000MS (Java/Others ...
随机推荐
- [Openwrt 扩展上篇]USB挂载&U盘启动&Samba共享
最近偷懒,没学习,反想起自己的路由刷了Openwrt,正好闲置了一个硬盘想拿来做个网络硬盘,于是开始了折腾....这里将不谈论如何刷Openwrt,如何ssh,如何添加PPOE,如何添加相对应服务的包 ...
- Html 使用技巧 -- 设置display属性可以使div隐藏后释放占用的页面空间
div的visibility可以控制div的显示和隐藏,但是隐藏后页面显示空白: style="visibility: none;" document.getElemen ...
- 关于UNIX的exec函数
在UNIX系统中,系统为进程相关提供了一系列的控制原语,包括:进程fork,进程exit,进程exec,进程wait等服务. 该篇文章主要与进程exec服务有关,并记录了几个需要注意留意的点. 照例给 ...
- [转]CNN 中千奇百怪的卷积方式大汇总
https://www.leiphone.com/news/201709/AzBc9Sg44fs57hyY.html 推荐另一篇很好的总结:变形卷积核.可分离卷积?卷积神经网络中十大拍案叫绝的操作. ...
- oracle建表,设置主键,修改属性等
--建表 create table book( book_id number(10), book_name varchar2(20), book_price number(10,2), book_au ...
- 转载-SVN常用命令
SVN(Subversion)是一个自由.开源的项目源代码版本控制工具.目前,绝大多数开源软件和企业代码管理,都使用SVN作为代码版本管理软件. Subversion将文件存放在中心版本库里,这个版本 ...
- Linux下编译安装qemu和libvirt【转】
转自:http://www.cnblogs.com/findumars/p/5679742.html 目录 [hide] 1 安装qemu 1.1 qemu介绍 1.2 下载源文件 1.3 编译安装 ...
- Django模型和ORM
一.ORM ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是 ...
- poj1976
dp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...
- Python 模块进阶
import导入模块 1. import 搜索路径 import sys sys.path 例子: In [1]: import sys In [2]: sys.path Out[2]: ['', ' ...