E. Caramel Clouds

time limit per test:3 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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.

Input

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.

Output

For each seedling print one integer – the minimum minute Slastyona can grow it up.

Examples
Input
3 5
1 7 1
1 6 2
1 7 1
3
7
2
5
Output
12
7
10
Input
3 15
1 4 17
2 8 6
4 8 9
2
5
1
Output
8
1
Input
2 10
3 7 9
10 90 10
2
10
100
Output
10
104
Note

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的更多相关文章

  1. 【CF833E】Caramel Clouds(线段树)

    [CF833E]Caramel Clouds(线段树) 题面 CF 洛谷 题解 首先把区间一段一段分出来,那么只有四种情况. 要么没有被任何一朵云被覆盖,那么直接就会产生这一段的贡献. 要么被一朵云覆 ...

  2. 【CF833E】Caramel Clouds

    [CF833E]Caramel Clouds 题面 洛谷 题目大意: 天上有\(n\)朵云,每朵云\(i\)会在时间\([li,ri]\)出现,你有\(C\)个糖果,你可以花费\(c_i\)个糖果让云 ...

  3. CF833E Caramel Clouds

    题面 天上有$n$朵云,每朵云$i$会在时间$[l_i,r_i]$出现,你有$\text C$个糖果,你可以花费$c_i$个糖果让云$i$消失,同时需要保证你最多让两朵云消失.现在有$m$个独立的询问 ...

  4. Codeforces Round #426 (Div. 1) (ABCDE)

    1. 833A The Meaningless Game 大意: 初始分数为$1$, 每轮选一个$k$, 赢的人乘$k^2$, 输的人乘$k$, 给定最终分数, 求判断是否成立. 判断一下$a\cdo ...

  5. Codeforces Gym 100500F Problem F. Door Lock 二分

    Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...

  6. 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 ...

  7. codeforces 436A. Feed with Candy 解题报告

    题目链接:http://codeforces.com/contest/436/problem/A 题目意思:给出 n 颗只有两种类型:fruit 和 caramel的candies,这些candies ...

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. Base64转换二进制文件对象 Blob/Base64转换 File 对象

    function convertBase64UrlToBlob(urlData) { var arr = dataurl.split(','),//去掉url的头,并转换为byte type = ar ...

  2. iOS wkwebview懒加载中遇到的问题

    这是我遇到的问题,也许是个例,就算狗血了点吧 需求: 当前界面(mainVC)响应点击事件,传值给webviewController(webVC)其中包含网址,此时如果在webVC中对wkwebvie ...

  3. SQL Server 数据库引擎怎样记录完整备份后修改过的数据

    SQL Server 使用两个内部数据结构跟踪被大容量复制操作修改的区,以及自上次完整备份后修改的区.这些数据结构极大地加快了差异备份的速度.当数据库使用大容量日志恢复模式时,这些数据结构也可以加快将 ...

  4. ArcGIS 网络分析[2.2] 服务区分析

    什么是服务区? 我们先提一个很常见的社会现象:一个医院,如果要发起抢救,那么10分钟内能去多远? 时间就是生命,当结合道路网的阻力进行最短路径分析时,得到的可达的覆盖区域,这个区域就是服务区. 服务区 ...

  5. Python学习(一):编写购物车

    1.购物车流程图: 2.代码实现: #!/usr/bin/env python #coding=utf-8 ChoiceOne =''' 1.查看余额 2.购物 3.退出 ''' ChoiceTwo ...

  6. python学习中的一些“坑”

    一.交互列表元素时,需要注意的坑. 例如: array=[4,5,9,8,10,8,4,0,3,4]  最大的值与第一个元素交换,最小的值与最后一个元素交换 # -*- coding: UTF-8 - ...

  7. 响应式布局—设备像素密度测试 (-webkit-min-device-pixel-ratio)

      最近遇到这种头疼的问题,百思不得其解,不耻下问,悬梁刺股这些事情都做过之后,终于看到希望,于是攒见好就收,感觉整理分享给大家,希望有所帮助. 对手机分辨率和网页像素的初步认识是,是2倍的差别. 但 ...

  8. Git详解之七:自定义Git

    自定义 Git 到目前为止,我阐述了 Git 基本的运作机制和使用方式,介绍了 Git 提供的许多工具来帮助你简单且有效地使用它. 在本章,我将会介绍 Git 的一些重要的配置方法和钩子机制以满足自定 ...

  9. C# DataGridVie利用model特性动态加载列

    今天闲来无事看到ORm的特性映射sql语句.我就想到datagridview也可以用这个来动态添加列.这样就不用每次都去界面上点开界面填列了. 代码简漏希望有人看到了能指点一二. 先定义好Datagr ...

  10. java多线程(二)-Runnable和Thread

    Java在顺序性语言的基础上提供了多线程的支持.Java的线程机制是抢占式的.这表示调度机制会周期的中断线程,将上下文切换到另一个线程,从而为每个线程都提供时间片.(与抢占式多线程对应的是 协作式多线 ...