假设当前已经组合好了 $[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. Python+VSCode+Git【转】

    Python+VSCode+Git 学习总结 - 秦无邪 - 博客园

  2. 湖北校园网PC端拨号算法逆向

    湖北校园网PC端拨号算法逆向 前言 上一文 PPPoE中间人拦截以及校园网突破漫谈我们谈到使用 PPPoE 拦截来获取真实的账号密码. 在这个的基础上,我对我们湖北的客户端进行了逆向,得到了拨号加密算 ...

  3. Spring AOP日志实现(一)

    前置通知:获取访问的类,访问的方法,带参数和不带参数的 日志表信息描述字段: 获取访问时长:

  4. Java帖子

    IDEA新手使用教程(详解):https://cloud.tencent.com/developer/article/1448115 Java学习文档:https://www.sxt.cn/Java_ ...

  5. tree 树形加载及增删改

    //异步1<template> <div class="addequipment org"> <div class="top"&g ...

  6. Java 之 IO 异常的处理【了解】

    一.JDK7 前的处理 前面的 Demo 中,一直把异常抛出,而在实际中并不能这样处理,建议使用 try...catch...finally 代码块,处理异常部分. 格式: try{ 可能会产出异常的 ...

  7. webdispatch配置

    PRDPISP01:/sapmnt/WIP/profile # su - wipadm PRDPISP01:wipadm 23> cdpro PRDPISP01:wipadm 24> ls ...

  8. Subversion和TortoiseSVN,VisualSVN图文教程

    转自:http://www.veryhuo.com/a/view/156054.html 本文为大家介绍一下SVN使用教程,在开始之前,您有必要了解一下为什么要使用SVN?因为程序员在编写程序的过程中 ...

  9. laravel模型关联评论

    用户模型 public function show(Post $post,LogManager $log){ $post->load("comments"); //这种方式是 ...

  10. ubuntu18.04 安装idea

    首先从官网下载idea:IntelliJ IDEA    (在安装IDEA前应先安装jdk环境) 得到ideaIU-2019.2.4.tar.gz 将安装包移动到/usr/local,这样可以让所有用 ...