假设当前已经组合好了 $[1, x]$ ,设 $ans = x + 1$ ;显然初始时 $x = 0, ans = 1$
我们另 $y = \sum_{i = l} ^ {r} (w_i <= ans) * w_i$
如果 $ans <= y$,说明除了组合出 $[1, x]$ 中的数,
一定存在一个数满足 $<=x+1$
这个时候 $[1, y]$ 都可以组合出来,另 $ans = y + 1$ ;
如果 $ans>y$ ,说明除了组合出 $[1, x]$的数,
其余所有的数均 $> x + 1$ ,这样当前的 $ans$ 就是答案了
统计一个区间内 $<=k$ 的数的和可以用主席树来是实现

#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
const int N = 1e5 + ; int W[N * ], Root[N], Lson[N * ], Rson[N * ];
int n, A[N], B[N]; #define gc getchar()
inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} int Seg_js;
void Fill(int x, int y) {W[x] = W[y]; Lson[x] = Lson[y]; Rson[x] = Rson[y];} void Insert(int &jd, int l, int r, int x, int num) {
Fill(++ Seg_js, jd);
jd = Seg_js;
W[jd] += num;
if(l == r) return ;
int mid = (l + r) >> ;
if(x <= mid) Insert(Lson[jd], l, mid, x, num);
else Insert(Rson[jd], mid + , r, x, num);
} int Answer; void Sec_A(int jd1, int jd2, int l, int r, int x) {
if(l == r) {Answer += (W[jd2] - W[jd1]); return;}
int mid = (l + r) >> ;
if(x <= mid) Sec_A(Lson[jd1], Lson[jd2], l, mid, x);
else {
Answer += (W[Lson[jd2]] - W[Lson[jd1]]);
Sec_A(Rson[jd1], Rson[jd2], mid + , r, x);
}
} int Len; inline int Find(int x) {
int l = , r = Len, ret;
while(l <= r) {
int mid = (l + r) >> ;
if(B[mid] <= x) ret = mid, l = mid + ;
else r = mid - ;
}
return ret;
} int main() {
n = read();
for(int i = ; i <= n; i ++) A[i] = read(), B[i] = A[i];
sort(B + , B + n + );
Len = unique(B + , B + n + ) - B - ;
for(int i = ; i <= n; i ++) {
int t = lower_bound(B + , B + Len + , A[i]) - B;
Root[i] = Root[i - ];
Insert(Root[i], , Len, t, A[i]);
}
int Q = read();
for(; Q; Q --) {
int l = read(), r = read();
int Ans = ;
while() {
int tmp = Find(Ans);
Answer = ;
Sec_A(Root[l - ], Root[r], , Len, tmp);
if(Answer >= Ans) Ans = Answer + ;
else break;
}
printf("%d\n", Ans);
}
return ;
}

luogu 4587的更多相关文章

  1. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  2. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  3. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  4. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  5. HDU 4587 TWO NODES 割点

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4587 题意: 删除两个点,使连通块的数目最大化 题解: 枚举删除第一个点,然后对删除了第一个点的图跑 ...

  6. Tarjan系列算法总结(hdu 1827,4612,4587,4005)

    tarjan一直是我看了头大的问题,省选之前还是得好好系统的学习一下.我按照不同的算法在hdu上选题练习了一下,至少还是有了初步的认识.tarjan嘛,就是维护一个dfsnum[]和一个low[],在 ...

  7. 【HDOJ】4587 TWO NODES

    Tarjan解无向图的割点和桥,参考白书. /* 4587 */ #include <iostream> #include <vector> #include <algo ...

  8. Luogu 考前模拟Round. 1

    A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...

  9. luogu P2580 于是他错误的点名开始了

    luogu  P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...

随机推荐

  1. MGR安装记录

    装好所有MySQL5.7, 打开GTID 修改my.cnf文件: ## group replication transaction_write_set_extraction = XXHASH64 ## ...

  2. ArcGIS SOE开发异常之 ClassFactory cannot supply requested class

    最近SOE开发一个功能,辛辛苦苦写完, 异常: ClassFactory cannot supply requested class 辛苦解决: 百度一下,描述这个问题的帖子很多,不过内容基本一致.大 ...

  3. Consul 注册中心介绍

    在 Spring Cloud 体系中,几乎每个角色都会有两个以上的产品提供选择,比如在注册中心有:Eureka.Consul.zookeeper.etcd 等:网关的产品有 Zuul.Spring C ...

  4. PEB TEB结构体使用

    PEB TEB结构体使用一个可执行文件被OS加载到内存,并处理完成相关的模块加载,导入表处理……及其他必须处理后,形成一个进程.在OS内核中保存有一份关于这个进程的身份证信息,就是平常看到的HANDL ...

  5. C# 泛型集合排序

    Student sModel = new Student(); List<Student > list = new List<Student >(); list= list.O ...

  6. AdventureWorks 安装和配置[转自 微软msdn]

    AdventureWorks 安装和配置 2018/06/19 适用对象:SQL ServerAzure SQL 数据库Azure SQL 数据仓库并行数据仓库 AdventureWorks 下载链接 ...

  7. python简单页面爬虫入门 BeautifulSoup实现

    本文可快速搭建爬虫环境,并实现简单页面解析 1.安装 python 下载地址:https://www.python.org/downloads/ 选择对应版本,常用版本有2.7.3.4 安装后,将安装 ...

  8. node.js 微信开发2-消息回复、token获取、自定义菜单

    项目结构 >config/wechat.json 微信公众号的配置文件 >controllers/oauth.js 微信网页授权接口(下一篇再细讲讲) >controllers/we ...

  9. java web 监控cpu、内存等。hyperic-sigar

    用到一个插件hyperic-sigar 1:下载hyperic-sigar后解压,把sigar-amd64-winnt.dll(64位机器,32位用sigar-x86-winnt.dll)放到你本机的 ...

  10. code first从入门到伪精通

    新入职一家公司,虽然之前也用ef,但是方式不一样,之前用的db,现在用代码先行的code,基于现有公司基本项目框架都是用的code,所以一步登顶,从最实战的角度去操作code,心颤的很,废话不多说,开 ...