【BZOJ-4408】神秘数 可持久化线段树
4408: [Fjoi 2016]神秘数
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 475 Solved: 287
[Submit][Status][Discuss]
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](l<=r),求由a[l],a[l+1],…,a[r]所构成的可重复数字集合的神秘数。
Input
第一行一个整数n,表示数字个数。
第二行n个整数,从1编号。
第三行一个整数m,表示询问个数。
以下m行,每行一对整数l,r,表示一个询问。
Output
对于每个询问,输出一行对应的答案。
Sample Input
1 2 4 9 10
5
1 1
1 2
1 3
1 4
1 5
Sample Output
4
8
8
8
HINT
对于100%的数据点,n,m <= 100000,∑a[i] <= 10^9
Source
Solution
这道题挺好的思路。
首先考虑在集合中已经选出$k$个数的时候,再加入第$k+1$个数的情况。
显然有当$a_{k+1}>\sum ^{k}_{i=1} a_{k} +1$时,$ans=\sum ^{k}_{i=1} a_{k}+1$
否则显然这个这些数能组合出的范围扩大$a_{k+1}$
所以思路就是对于一个$ans$,求出$\sum ^{R}_{i=L} (a_{i}<ans) a_{i}$,如果这些数能组合到$ans$,那么这个$ans$只能扩大,所以把$ans$扩大到$\sum ^{R}_{i=L} (a_{i}<ans) a_{i} +1$继续做,否则得到神秘数。
所以支持这样做的还是利用可持久化线段树求出。
但是这样的复杂度还是比较暴力的,不过题目中说了$\sum a_{i}<10^{9}$所以复杂度最坏是 $O(MlogNlog10^{9})$
话说这题被xyx秒了....
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
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;
}
#define MAXN 100010 int N,M,a[MAXN]; namespace PrTree{
int root[MAXN],sum[MAXN*20],lson[MAXN*20],rson[MAXN*20],sz;
inline void Insert(int l,int r,int &x,int last,int pos,int val)
{
x=++sz;
lson[x]=lson[last],rson[x]=rson[last];
sum[x]=sum[last]+val;
if (l==r) return;
int mid=(l+r)>>1;
if (pos<=mid) Insert(l,mid,lson[x],lson[last],pos,val);
else Insert(mid+1,r,rson[x],rson[last],pos,val);
}
inline int Query(int l,int r,int L,int R,int x,int y)
{
if (L>R) return 0;
if (L<=l && R>=r) return sum[y]-sum[x];
int mid=(l+r)>>1,re=0;
if (L<=mid) re+=Query(l,mid,L,R,lson[x],lson[y]);
if (R>mid) re+=Query(mid+1,r,L,R,rson[x],rson[y]);
return re;
}
}using namespace PrTree; int ls[MAXN]; int main()
{
N=read();
for (int i=1; i<=N; i++) ls[i]=a[i]=read(); sort(ls+1,ls+N+1); int tot=unique(ls+1,ls+N+1)-ls-1; for (int i=1; i<=N; i++) a[i]=lower_bound(ls+1,ls+tot+1,a[i])-ls; for (int i=1; i<=N; i++) PrTree::Insert(1,tot,root[i],root[i-1],a[i],ls[a[i]]); M=read();
while (M--) {
int l=read(),r=read();
int ans=1,up,pos;
while (1) {
pos=upper_bound(ls+1,ls+tot+1,ans)-ls-1;
if (ans<=(up=PrTree::Query(1,tot,1,pos,root[l-1],root[r])))
ans=up+1;
else break;
}
printf("%d\n",ans);
}
return 0;
}
【BZOJ-4408】神秘数 可持久化线段树的更多相关文章
- (bzoj4408)[FJOI2016]神秘数(可持久化线段树)
(bzoj4408)[FJOI2016]神秘数(可持久化线段树) bzoj luogu 对于一个区间的数,排序之后从左到右每一个数扫 如果扫到某个数a时已经证明了前面的数能表示[1,x],那么分情况: ...
- 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]神秘数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 Description 一个可重复数字集 ...
- BZOJ 4408 FJOI2016 神秘数 可持久化线段树
Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1+1+14 = 45 = 4+16 ...
- 51Nod 1175 区间中第K大的数 (可持久化线段树+离散)
1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...
- bzoj 2653 二分答案+可持久化线段树
首先离散化,然后我们知道如果对于一个询问的区间[l1,r1],[l2,r2],我们二分到一个答案x,将[l1,r2]区间中的元素大于等于x的设为1,其余的设为-1,那么如果[l1,r1]的最大右区间和 ...
- bzoj 2653 middle (可持久化线段树)
middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1981 Solved: 1097[Submit][Status][Discuss] D ...
- BZOJ 3542 [Poi2014]Couriers ——可持久化线段树
[题目分析] 查找区间内出现次数大于一半的数字. 直接用主席树,线段树上维护区间大小,由于要求出现次数大于一半,每到一个节点可以分治下去. 时间复杂度(N+Q)logN [代码] #include & ...
- BZOJ 4408 神秘数
题解同各神犇的方法... #include<iostream> #include<cstdio> #include<cstring> #include<alg ...
随机推荐
- Maven的国内镜像(解决jar下载过慢)
Maven简介 maven作为一个项目管理工具确实非常好用,结果在使用时候,你会发现下载jar速度不如自己在网上下载.之前oschina的中央仓库可用,现在oschina的maven服务器关了,只能拿 ...
- SpringMVC关于ajax提交400错误(后台获取为null)
400错误有三种情况 1:请求的数据量过大,不过这种情况一般很少见. 2:请求的data参数有误,确保每一个参数都能请求到. 注释:之前小白出现400错误,后台获取参数为null是因为第三种情况,经过 ...
- select遍历list默认选中初始值
<select id="userstatus" name="userstatus"> <c:forEach items=&qu ...
- bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战
http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...
- Asp.net 子web application的Session共享
需求提出: 网站: 父Web Application: http://www.test.com/ 子Web Application 1: http://www.test.com/child1 子Web ...
- [转载]WIN7已经记住访问另一台电脑的账号和密码 凭证
http://jingyan.baidu.com/article/a3aad71aaa32eeb1fb0096c8.html
- [整] Android ListView 去除边缘阴影、选中色、拖动背景色等
以下是通过XML定义的方式实现,如果需要通过代码实现,找到对应是set方式设置即可. 去除ListView滑到顶部和底部时边缘的黑色阴影: android:fadingEdge="none& ...
- [原]JUnit 自定义扩展思路
1. 理解Annotation,http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html 2. JUNIT整体执行过程分析,htt ...
- iOS8 UICollectionView横向滑动demo
在iOS8中,scrollView和加载在它上面的点击事件会有冲突,所以做一个横向滑动的界面最好的选择就是UICollectionView. 这个效果可以用苹果公司提供的官方demo修改而来,下载地址 ...
- 第8月第21天 django lbforum项目记录
1. django-admin.py startproject lbforum01 ls cd lbforum01/ ls python manage.py startapp forum sudo p ...