【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 ...
随机推荐
- mybatis的配置文件中<selectKey>标签问题
1.mybatis的配置文件中,使用sequence生成主键 未执行add方法之前,主键未生成(null):刚执行add之后,主键即生成(212) 这里的重点是,一旦执行add方法,配置文件中的sel ...
- VUE项目用hbuilder 打包为手机APP
一.测试项目是否可以正确运行 指令:npm run dev 首先我们先建立一个vue的项目,本人用的是vue-cli随便建立的,然后运行项目 不必非得是像我这样的,这一步的目的只是测试一下咱们的 ...
- [转]CNN 中千奇百怪的卷积方式大汇总
https://www.leiphone.com/news/201709/AzBc9Sg44fs57hyY.html 推荐另一篇很好的总结:变形卷积核.可分离卷积?卷积神经网络中十大拍案叫绝的操作. ...
- vue表格中显示金额格式化与保存时格式化为数字并校验!
最近项目中遇到了成本计算的,需要显示金额,保存一下,以后方便直接拿来用! 一 数字转金额格式显示 //数字转金额格式 format:function(s){ if(/[^0-9\.]/.test(s) ...
- 按需引入antd报错问题
1.使用create-react-app工具创建了一个项目 create-react-app antd-demo 2.安装babel-plugin-import npm install babel-p ...
- python基础--hashlib模块
hashlib模块用于加密操作,代替了md5和sha模块, 主要提供SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法. # -*- coding:utf-8 - ...
- Extjs6设置Store、Ajax、form的请求方式(GET、POST)
Extjs6 设置Store.Ajax.form的请求方式(GET.POST) Ajax请求和Form的submit方法设置请求方式和原来一样,使用method : 'POST'设置 // 表单提交 ...
- 洛谷P1286 两数之和
这个题.. 刚开始没看见输入若干行,所以有的点就.. 令 m = n * (n - 1) / 2 已知 s = {s (1), s(2), ..., s(m)}, s(i) <= s(i+1) ...
- HTML5 Differences from HTML4
Abstract "HTML5 Differences from HTML4" describes the differences of the HTML5 specificati ...
- 如何阻止点击scrollviewer里面的单位内容时,自动滚动
<Style TargetType="{x:Type ListBoxItem}"> <Setter Property="FocusVisualStyle ...