BZOJ.4299.Codechef FRBSUM(主席树)
记mx为最大的满足1~mx都能组成的数。
考虑当前能构成1~v中的所有数,再加入一个数x,若x>v+1,则mx=v,x不会产生影响;否则x<=v+1,则新的mx=x+v。
对于区间[l,r]的询问,模拟这个过程。假设当前答案为v,查询[l,r]中值在[1,v+1]中的数的和sum,若sum==v,即不存在v+1,break;否则v加上这些v+1的和即v=sum,继续。
用主席树实现。v每次至少增加一倍(好像这么说不恰当,每次加的数至少是上一个数+1?),所以复杂度\(O(n\log n\log a)\)。
为什么跑的比较慢呢。。
//38224kb 2860ms
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 300000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=1e5+5;
int A[N],root[N];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Tree
{
#define S N*31//loga not logn && 31 not 30...
#define lson son[x][0]
#define rson son[x][1]
int tot,sum[S],son[S][2];
inline void Insert(int x,int &y,int l,int r,int p)
{
sum[y=++tot]=sum[x]+p;
if(l==r) return;
int m=l+r>>1;
if(p<=m) son[y][1]=rson, Insert(lson,son[y][0],l,m,p);
else son[y][0]=lson, Insert(rson,son[y][1],m+1,r,p);
}
int Query(int x,int y,int l,int r,int R)
{
if(r<=R) return sum[y]-sum[x];
int m=l+r>>1;
if(m<R) return sum[son[y][0]]-sum[lson]+Query(rson,son[y][1],m+1,r,R);
else return Query(lson,son[y][0],l,m,R);
}
}T;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
int n=read(); int R=1;
for(int i=1; i<=n; ++i) R+=(A[i]=read());
for(int i=1; i<=n; ++i) T.Insert(root[i-1],root[i],1,R,A[i]);
for(int m=read(),l,r,ans,sum; m--; )
{
l=read(),r=read(),ans=0;
while(ans<(sum=T.Query(root[l-1],root[r],1,R,ans+1))) ans=sum;
printf("%d\n",ans+1);
}
return 0;
}
BZOJ.4299.Codechef FRBSUM(主席树)的更多相关文章
- bzoj4408 [Fjoi 2016]神秘数 & bzoj4299 Codechef FRBSUM 主席树+二分+贪心
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4299 https://lydsy.com/JudgeOnline/problem.php?id ...
- bzoj 4299 Codechef FRBSUM
定义一个集合的神秘数为不能表示成这个集合的某个子集和的最小正整数,给一个数列,多次求区间神秘数 $n \leq 100000$ sol: 考虑这个神秘数的性质,可以发现,如果神秘数是 $x$,那么 $ ...
- BZOJ4299: Codechef FRBSUM(主席树)
题意 题目链接 数集S的ForbiddenSum定义为无法用S的某个子集(可以为空)的和表示的最小的非负整数. 例如,S={1,1,3,7},则它的子集和中包含0(S’=∅),1(S’={1}),2( ...
- [BZOJ4408&&BZOJ4299][FJOI2016 && Codechef]神秘数&&FRBSUM(主席树)
4299: Codechef FRBSUM Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 550 Solved: 351[Submit][Statu ...
- 4299: Codechef FRBSUM
4299: Codechef FRBSUM https://www.lydsy.com/JudgeOnline/problem.php?id=4299 分析: 主席树. https://blog.se ...
- BZOJ 4556(后缀数组+主席树求前驱后继+二分||后缀数组+二分+可持久化线段树)
换markdown写了.. 题意: 给你一个1e5的字符串,1e5组询问,求\([l_1,r_1]\)的所有子串与\([l_2,r_2]\)的lcp 思路: 首先可以发现答案是具有单调性的,我们考虑二 ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- bzoj 4012: [HNOI2015]开店 主席树
Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...
- bzoj 4826: [Hnoi2017]影魔 [主席树 单调栈]
4826: [Hnoi2017]影魔 题意:一个排列,点对\((i,j)\),\(p=max(i+1,j-1)\),若\(p<a_i,a_j\)贡献p1,若\(p\)在\(a_1,a_2\)之间 ...
随机推荐
- BZOJ 3192: [JLOI2013]删除物品(树状数组)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=3192 题解: 首先每次一定是来回移动直到最大的到顶上. 所以我们可以将第两个堆的堆顶接起来 ...
- CF757E Bash Plays with Functions
题解 q<=1e6,询问非常多.而n,r也很大,必须要预处理所有的答案,询问的时候,能比较快速地查询. 离线也是没有什么意义的,因为必须递推. 先翻译$f_0(n)$ $f_0(n)=\sum_ ...
- 题解-洛谷P1184 高手之在一起
https://www.luogu.org/problemnew/show/P1184 (题目出处) 见到地名,自然就想到字符串了.可以从第一天开始,将她的位置与高手方便取得地方一一比较,(char字 ...
- CMDB服务器管理系统【s5day90】:创建资产更新服务器硬盘信息
1.创建硬件资产信息 import json from django.shortcuts import render,HttpResponse from django.views.decorators ...
- Connection to Oracle failed. [66000][12505] Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor .
我安装了Oracle数据库,默认的数据库用户名是system,密码口令是安装过程中你自己设置的.可以先使用命令框,输入 sqlplus system; 然后再输入密码即可. 然后我的数据库连接工具使用 ...
- java集合分割
java集合分割成等份的小集合: private <T> List<List<T>> getSubList(List list,int len) { if(list ...
- line-height && vertical-align 学习总结
前言 line-height.font-size.vertical-align是设置行内元素布局的关键属性.这三个属性是相互依赖的关系,改变行间距离.设置垂直对齐等都需要它们的通力合作. 行高 lin ...
- DTO/DO等POJO对象的使用场景和 orika-mapper 框架的使用
对于项目而言, 我们一般会有DAO->Service->Controller分层设计, 这些层次体现了每层的作用, 而层次之间的数据传递对象设计很少被提及, 下面是一个相对完整的数据转换过 ...
- [再寄小读者之数学篇](2014-06-21 Beal-Kaot-Majda type logarithmic Sobolev inequality)
For $f\in H^s(\bbR^3)$ with $s>\cfrac{3}{2}$, we have $$\bex \sen{f}_{L^\infty}\leq C\sex{1+\sen{ ...
- CentOS 7 破解root密码
破解root密码必须在本机上进行,如果使用SecureCRT Xshell等远程工具修改是会出错的! A0 哥们儿,忘记密码了,怎么办??? A1.进入启动界面: A2. 按‘e’键进入编辑模式 A ...