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\)之间 ...
随机推荐
- python 类的私有变量和私有方法
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/08 8:46 # @Author : lijunjiang # @Fil ...
- webserver Etcd Cluster / CoreOS etcd / macOS etcd
s https://coreos.com/etcd/ https://coreos.com/etcd/docs/latest/ macOS mojave etcd 003deMac-mini:~ ma ...
- 第十五节:Expression表达式目录树(与委托的区别、自行拼接、总结几类实例间的拷贝)
一. 基本介绍 回忆: 最早接触到表达式目录树(Expression)可能要追溯到几年前使用EF早期的时候,发现where方法里的参数是Expression<Func<T,bool> ...
- SpringBoot系列: CommandLineRunner接口的用处
========================================使用 CommandLineRunner 对Spring Bean进行额外初始化==================== ...
- MarkDown 的两种页内跳转方法!!!!!
页面内跳转就是点击某个文本,能够跳转到页面里指定的其他地方,经常用于目录中. 第一种是利用Html5 比如点击Feature, 跳转到features中 MarkDown: [Feature](#1) ...
- PL/SQL连接远程oracle数据库配置
本人电脑:win10 64位, plsql:支持32位 一.安装好PL/SQL客户端 二.下载解压oracle客户端 1.instantclient 我下载的是oracle11g 32位的insta ...
- Jhipster 一个Spring Boot + Angular/React 全栈框架
Jhipster 一个Spring Boot + Angular/React 全栈框架: https://www.jhipster.tech/
- fiddler抓包工具
转载: http://www.cr173.com/html/15341_1.html https://www.cnblogs.com/shihaiming/p/5887654.html 软件简介: 数 ...
- linux下 vi命令编辑/etc/my.cnf
把my.cnf配置文件加个max_connections包括(插入命令,删除命令,修改命令.退出保存命令) 你要有这个文件写权限,shell下输入: vi /etc/my.cnf 进入vi后,按i移动 ...
- SpringMVC+Spring+Hibernate整合开发
最近突然想认真研究下java web常用框架,虽然现在一直在用,但实现的整体流程不是很了解,就在网上搜索资料,尝试自己搭建,以下是自己的搭建及测试过程. 一.准备工作: 1/安装并配置java运行环境 ...