BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树
4408: [Fjoi 2016]神秘数
题目连接:
http://www.lydsy.com/JudgeOnline/problem.php?id=4408
Description
一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数。例如S={1,1,1,4,13},
1 = 1
2 = 1+1
3 = 1+1+1
4 = 4
5 = 4+1
6 = 4+1+1
7 = 4+1+1+1
8无法表示为集合S的子集的和,故集合S的神秘数为8。
现给定n个正整数a[1]..a[n],m个询问,每次询问给定一个区间l,r,求由a[l],a[l+1],…,a[r]所构成的可重复数字集合的神秘数。
Input
第一行一个整数n,表示数字个数。
第二行n个整数,从1编号。
第三行一个整数m,表示询问个数。
以下m行,每行一对整数l,r,表示一个询问。
Output
对于每个询问,输出一行对应的答案。
Sample Input
5
1 2 4 9 10
5
1 1
1 2
1 3
1 4
1 5
Sample Output
2
4
8
8
8
Hint
题意
题解:
权限题
用持久化线段树去维护就好了
假设我当前的答案是ans,那么如果在这个区间小于等于ans的数的和sum小于了ans,那么显然是不能构成ans的,那就直接输出就好了
否则就更新ans=sum+1,然后这样不停的迭代下去就好了。
这个解释的话,用dp去想就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node
{
int l,r,sum;
}T[maxn*100];
int n,cnt,a[maxn],root[maxn],m,l,r,ans,tot;
void update(int l,int r,int &x,int y,int val)
{
T[++cnt]=T[y],T[cnt].sum+=val;x=cnt;
if(l==r)return;
int mid=(l+r)/2;
if(val<=mid)update(l,mid,T[x].l,T[x].l,val);
else update(mid+1,r,T[x].r,T[x].r,val);
}
int query(int l,int r,int x,int y,int pos)
{
if(l==r)return T[y].sum-T[x].sum;
int mid=(l+r)/2;
if(pos<=mid)return query(l,mid,T[x].l,T[y].l,pos);
else return query(mid+1,r,T[x].r,T[y].r,pos)+T[T[y].l].sum-T[T[x].l].sum;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)a[i]=read(),tot+=a[i];
for(int i=1;i<=n;i++)update(1,tot,root[i],root[i-1],a[i]);
for(m=read();m;m--)
{
l=read(),r=read(),ans=1;
while(1)
{
int tmp=query(1,tot,root[l-1],root[r],ans);
if(tmp<ans)break;
ans=tmp+1;
}
printf("%d\n",ans);
}
}
BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树的更多相关文章
- Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 128[Submit][Status ...
- BZOJ 4408: [Fjoi 2016]神秘数
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 464 Solved: 281[Submit][Status ...
- bzoj 4408: [Fjoi 2016]神秘数 数学 可持久化线段树 主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=4299 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1 ...
- ●BZOJ 4408 [Fjoi 2016]神秘数
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 题解: 主席树 首先,对于一些数来说, 如果可以我们可以使得其中的某些数能够拼出 1- ...
- BZOJ 4408: [Fjoi 2016]神秘数 [主席树]
传送门 题意: 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},8无法表示为集合S的子集的和,故集合S的神秘数为8.现给定n个正整数a[1]. ...
- BZOJ 4408: [Fjoi 2016]神秘数 主席树 + 神题
Code: #include<bits/stdc++.h> #define lson ls[x] #define mid ((l+r)>>1) #define rson rs[ ...
- 4408: [Fjoi 2016]神秘数
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 452 Solved: 273 [Submit][Stat ...
- 【BZOJ-4408】神秘数 可持久化线段树
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 475 Solved: 287[Submit][Status ...
- (bzoj4408)[FJOI2016]神秘数(可持久化线段树)
(bzoj4408)[FJOI2016]神秘数(可持久化线段树) bzoj luogu 对于一个区间的数,排序之后从左到右每一个数扫 如果扫到某个数a时已经证明了前面的数能表示[1,x],那么分情况: ...
随机推荐
- Hadoop(三):MapReduce程序(python)
使用python语言进行MapReduce程序开发主要分为两个步骤,一是编写程序,二是用Hadoop Streaming命令提交任务. 还是以词频统计为例 一.程序开发1.Mapper for lin ...
- 汇编看C函数调用
http://blog.csdn.net/wishfly/article/details/5022008 简单的函数调用,通过简单的函数调用反汇编可以清楚了解如下 1.栈到底是什么,如何操纵栈的? ...
- ubuntu获得root用户权限,使用xshell连接!
一.获取root用户权限 打开linux终端命令,输入 sudo passwd root Enter new UNIX password: (在这输入你的密码) Retype new UNIX pas ...
- 20155309 2016-2017-2《Java程序设计》课程总结
预备作业1http://www.cnblogs.com/nhx19970709/p/6155580.html 第一次写博客,也是第一次用Markdown,具体流程都还不是很熟悉 预备作业2http:/ ...
- 第一篇CodeIgniter框架的下载及安装
初次学习Php,网上搜了很多php框架,最后选择了CodeIgniter. 安装环境:php5+mysql6.5+iis7 我的电脑是用来办公写文档用的,win7系统,不想换系统,所以就安装了win7 ...
- Hive SQL综合案例
一 Hive SQL练习之影评案例 案例说明 现有如此三份数据:1.users.dat 数据格式为: 2::M::56::16::70072, 共有6040条数据对应字段为:UserID BigInt ...
- SQL中format()函数对应的格式
http://www.cnbeta.com/articles/tech/632057.htm
- NHibernate 错误
Unable to locate persister for the entity named 'Model.Customer'.The persister define the persistenc ...
- FPGA In/Out Delay Timing Constaint
先简单说说这段时间遇到的问题.FPGA采集前端scaler的视频数据.像素时钟(随路时钟),视频数据,行场同步,DE.这些信号进入FPGA后.通过CSC(颜色空间转换).输出后的图像有噪点.通过查看时 ...
- eclipse使用小技巧
1.eclipse中SVN无版本信息显示,window-preference-general-appeerance-label decoration-svn勾上,显示有关项目中受 SVN 控制的资源的 ...