Codeforces 833E Caramel Clouds
E. Caramel Clouds
It is well-known that the best decoration for a flower bed in Sweetland are vanilla muffins. Seedlings of this plant need sun to grow up. Slastyona has m seedlings, and the j-th seedling needs at least kj minutes of sunlight to grow up.
Most of the time it's sunny in Sweetland, but sometimes some caramel clouds come, the i-th of which will appear at time moment (minute) li and disappear at time moment ri. Of course, the clouds make shadows, and the seedlings can't grow when there is at least one cloud veiling the sun.
Slastyona wants to grow up her muffins as fast as possible. She has exactly C candies, which is the main currency in Sweetland.
One can dispel any cloud by paying ci candies. However, in order to comply with Sweetland's Department of Meteorology regulations, one can't dispel more than two clouds.
Slastyona hasn't decided yet which of the m seedlings will be planted at the princess' garden, so she needs your help. For each seedling determine the earliest moment it can grow up if Slastyona won't break the law and won't spend more candies than she has. Note that each of the seedlings is considered independently.
The seedlings start to grow at time moment 0.
The first line contains two integers n and C (0 ≤ n ≤ 3·105, 0 ≤ C ≤ 109) – the number of caramel clouds and the number of candies Slastyona has.
The next n lines contain three integers each: li, ri, ci (0 ≤ li < ri ≤ 109, 0 ≤ ci ≤ 109), describing one caramel cloud.
The next line contains single integer m (1 ≤ m ≤ 3·105) – the number of seedlings. Each of the seedlings is described with one integer kj (1 ≤ kj ≤ 109) – the required number of sunny minutes.
For each seedling print one integer – the minimum minute Slastyona can grow it up.
3 5
1 7 1
1 6 2
1 7 1
3
7
2
5
12
7
10
3 15
1 4 17
2 8 6
4 8 9
2
5
1
8
1
2 10
3 7 9
10 90 10
2
10
100
10
104
Consider the first example. For each k it is optimal to dispel clouds 1 and 3. Then the remaining cloud will give shadow on time segment [1..6]. So, intervals [0..1] and [6..inf) are sunny.

In the second example for k = 1 it is not necessary to dispel anything, and for k = 5 the best strategy is to dispel clouds 2 and 3. This adds an additional sunny segment [4..8], which together with [0..1] allows to grow up the muffin at the eight minute.

If the third example the two seedlings are completely different. For the first one it is necessary to dispel cloud 1 and obtain a sunny segment [0..10]. However, the same strategy gives answer 180 for the second seedling. Instead, we can dispel cloud 2, to make segments [0..3] and [7..inf) sunny, and this allows up to shorten the time to 104.
题目链接:http://codeforces.com/problemset/problem/833/E
叉姐的题解:

叉姐的代码:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <utility>
#include <vector> const int N = ; struct Sum
{
int add(int id, int value)
{
if (a[].second == id) {
a[].first = std::max(a[].first, value);
} else if (a[].first < value) {
a[] = {value, id};
}
if (a[].first < a[].first) {
std::swap(a[], a[]);
}
} int ask(int id)
{
if (a[].second != id) {
return a[].first;
}
return a[].first;
} std::pair<int, int> a[] = {{, -}, {, -}};
}; int cost[N + ], toupd[N]; int main()
{
#ifdef LOCAL_JUDGE
freopen("E.in", "r", stdin);
#endif
int n, budget;
while (scanf("%d%d", &n, &budget) == ) {
cost[n] = ;
std::vector<std::pair<int, int>> events;
events.emplace_back(, n);
events.emplace_back(, n);
for (int i = , l, r; i < n; ++ i) {
scanf("%d%d%d", &l, &r, cost + i);
events.emplace_back(l, i);
events.emplace_back(r, i);
}
std::sort(events.begin(), events.end());
std::vector<int> values(cost, cost + n);
std::sort(values.begin(), values.end());
values.erase(std::unique(values.begin(), values.end()), values.end());
std::set<int> covers;
if (events[].second < n) {
covers.insert(events[].second);
}
int curmx = ;
std::vector<std::pair<int, int>> parts;
memset(toupd, , sizeof(toupd));
std::vector<Sum> bit(values.size());
std::map<std::pair<int, int>, int> length;
for (int t = ; t < (int)events.size(); ++ t) {
int mxlen = events[t].first - events[t - ].first;
if (mxlen > && (int)covers.size() <= ) {
int p = n, q = n;
if ((int)covers.size() > ) {
p = *covers.begin();
}
if ((int)covers.size() > ) {
q = *covers.rbegin();
}
int start = -;
if (p == n) { //
start = curmx;
} else {
if (q == n) { //
if (cost[p] <= budget) {
start = toupd[p];
for (int k = (int)(std::upper_bound(values.begin(), values.end(), budget - cost[p]) - values.begin()) - ; k >= ; k -= ~k & k + ) {
start = std::max(start, bit[k].ask(p));
}
auto value = length[{p, q}] + mxlen;
for (int k = std::lower_bound(values.begin(), values.end(), cost[p]) - values.begin(); k < (int)values.size(); k += ~k & k + ) {
bit[k].add(p, value);
}
}
} else if (cost[p] + cost[q] <= budget) {
start = length[{p, n}] + length[{q, n}];
toupd[p] = std::max(toupd[p], length[{q, n}] + length[{p, q}] + mxlen);
toupd[q] = std::max(toupd[q], length[{p, n}] + length[{p, q}] + mxlen);
}
if (~start) {
start += length[{p, q}] + length[{n, n}];
}
}
if (~start && start + mxlen > curmx) {
curmx = start + mxlen;
parts.emplace_back(curmx, events[t].first);
}
length[{p, q}] += mxlen;
}
auto&& i = events[t].second;
if (i < n) {
if (covers.count(i)) {
covers.erase(i);
} else {
covers.insert(i);
}
}
}
int q, t;
scanf("%d", &q);
while (q --) {
scanf("%d", &t);
auto it = std::lower_bound(parts.begin(), parts.end(), std::make_pair(t, ));
printf("%d\n", it->second - (it->first - t));
}
}
}
Codeforces 833E Caramel Clouds的更多相关文章
- 【CF833E】Caramel Clouds(线段树)
[CF833E]Caramel Clouds(线段树) 题面 CF 洛谷 题解 首先把区间一段一段分出来,那么只有四种情况. 要么没有被任何一朵云被覆盖,那么直接就会产生这一段的贡献. 要么被一朵云覆 ...
- 【CF833E】Caramel Clouds
[CF833E]Caramel Clouds 题面 洛谷 题目大意: 天上有\(n\)朵云,每朵云\(i\)会在时间\([li,ri]\)出现,你有\(C\)个糖果,你可以花费\(c_i\)个糖果让云 ...
- CF833E Caramel Clouds
题面 天上有$n$朵云,每朵云$i$会在时间$[l_i,r_i]$出现,你有$\text C$个糖果,你可以花费$c_i$个糖果让云$i$消失,同时需要保证你最多让两朵云消失.现在有$m$个独立的询问 ...
- Codeforces Round #426 (Div. 1) (ABCDE)
1. 833A The Meaningless Game 大意: 初始分数为$1$, 每轮选一个$k$, 赢的人乘$k^2$, 输的人乘$k$, 给定最终分数, 求判断是否成立. 判断一下$a\cdo ...
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- Codeforces Round #426 (Div. 2)【A.枚举,B.思维,C,二分+数学】
A. The Useless Toy time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- codeforces 436A. Feed with Candy 解题报告
题目链接:http://codeforces.com/contest/436/problem/A 题目意思:给出 n 颗只有两种类型:fruit 和 caramel的candies,这些candies ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- 【java】文件操作java.io.File
package 文件操作; import java.io.File; import java.io.IOException; public class TestFile { public static ...
- 【二十六】php之文件编程
1.获取文件的相关信息 fopen.fstat.fclose(打开文件.获取文件相关信息.关闭文件) filesize.filectime.filemtime.fileatime(文件大小.上次cha ...
- http中的get和post(二)
博客园精华区有篇文章< GET 和 POST 有什么区别?及为什么网上的多数答案都是错的 >,文中和回复多是对以下两个问题进行了深究: 长度限制 Url 是否隐藏数据 在我看来这两者都不是 ...
- 常用的 JS 排序算法整理
关于排序算法的问题可以在网上搜到一大堆,但是纯 JS 版比较零散,之前面试的时候特意整理了一遍,附带排序效率比较. //1.冒泡排序 var bubbleSort = function(arr) { ...
- adb指令介绍
一.adb命令格式为:adb [-d|-e|-s <serialNumber>] <command> 1.adb devices :列出当前电脑所连接的所有安卓设备 2.adb ...
- 修改Linux内核参数提高Nginx服务器并发性能
当linux下Nginx达到并发数很高,TCP TIME_WAIT套接字数量经常达到两.三万,这样服务器很容易被拖死.事实上,我们可以简单的通过修改Linux内核参数,可以减少Nginx服务器 的TI ...
- Docker安装入门 -- 中间件镜像
Docker安装入门 -- 中间件镜像 php-fpm 1.docker build -t csphere/php-fpm:5.4 . //在当前目录下创建镜像 2.docker run -d - ...
- Python-字典dict对象方法总结
- 从JVM字节码执行看重载和重写
Java 重写(Override)与重载(Overload) 重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的 ...
- css实现平行四边形、菱形图片效果
一.平行四边形 1. 使用两个元素实现 html <a class="button"> <div>click me</div> </a&g ...