Codeforces 833E Caramel Clouds

简介: E. Caramel Clouds time limit per test:3 seconds memory limit per test:256 megabytes input:standard input output:standard out...

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

叉姐的题解:

叉姐的代码:

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <map>
  5 #include <set>
  6 #include <utility>
  7 #include <vector>
  8 
  9 const int N = 300000;
 10 
 11 struct Sum
 12 {
 13     int add(int id, int value)
 14     {
 15         if (a[0].second == id) {
 16             a[0].first = std::max(a[0].first, value);
 17         } else if (a[1].first < value) {
 18             a[1] = {value, id};
 19         }
 20         if (a[0].first < a[1].first) {
 21             std::swap(a[0], a[1]);
 22         }
 23     }
 24 
 25     int ask(int id)
 26     {
 27         if (a[0].second != id) {
 28             return a[0].first;
 29         }
 30         return a[1].first;
 31     }
 32 
 33     std::pair<int, int> a[2] = {{0, -1}, {0, -1}};
 34 };
 35 
 36 int cost[N + 1], toupd[N];
 37 
 38 int main()
 39 {
 40 #ifdef LOCAL_JUDGE
 41     freopen("E.in", "r", stdin);
 42 #endif
 43     int n, budget;
 44     while (scanf("%d%d", &n, &budget) == 2) {
 45         cost[n] = 0;
 46         std::vector<std::pair<int, int>> events;
 47         events.emplace_back(0, n);
 48         events.emplace_back(2000000000, n);
 49         for (int i = 0, l, r; i < n; ++ i) {
 50             scanf("%d%d%d", &l, &r, cost + i);
 51             events.emplace_back(l, i);
 52             events.emplace_back(r, i);
 53         }
 54         std::sort(events.begin(), events.end());
 55         std::vector<int> values(cost, cost + n);
 56         std::sort(values.begin(), values.end());
 57         values.erase(std::unique(values.begin(), values.end()), values.end());
 58         std::set<int> covers;
 59         if (events[0].second < n) {
 60             covers.insert(events[0].second);
 61         }
 62         int curmx = 0;
 63         std::vector<std::pair<int, int>> parts;
 64         memset(toupd, 0, sizeof(toupd));
 65         std::vector<Sum> bit(values.size());
 66         std::map<std::pair<int, int>, int> length;
 67         for (int t = 1; t < (int)events.size(); ++ t) {
 68             int mxlen = events[t].first - events[t - 1].first;
 69             if (mxlen > 0 && (int)covers.size() <= 2) {
 70                 int p = n, q = n;
 71                 if ((int)covers.size() > 0) {
 72                     p = *covers.begin();
 73                 }
 74                 if ((int)covers.size() > 1) {
 75                     q = *covers.rbegin();
 76                 }
 77                 int start = -1;
 78                 if (p == n) { // 0
 79                     start = curmx;
 80                 } else {
 81                     if (q == n) { // 1
 82                         if (cost[p] <= budget) {
 83                             start = toupd[p];
 84                             for (int k = (int)(std::upper_bound(values.begin(), values.end(), budget - cost[p]) - values.begin()) - 1; k >= 0; k -= ~k & k + 1) {
 85                                 start = std::max(start, bit[k].ask(p));
 86                             }
 87                             auto value = length[{p, q}] + mxlen;
 88                             for (int k = std::lower_bound(values.begin(), values.end(), cost[p]) - values.begin(); k < (int)values.size(); k += ~k & k + 1) {
 89                                 bit[k].add(p, value);
 90                             }
 91                         }
 92                     } else if (cost[p] + cost[q] <= budget) {
 93                         start = length[{p, n}] + length[{q, n}];
 94                         toupd[p] = std::max(toupd[p], length[{q, n}] + length[{p, q}] + mxlen);
 95                         toupd[q] = std::max(toupd[q], length[{p, n}] + length[{p, q}] + mxlen);
 96                     }
 97                     if (~start) {
 98                         start += length[{p, q}] + length[{n, n}];
 99                     }
100                 }
101                 if (~start && start + mxlen > curmx) {
102                     curmx = start + mxlen;
103                     parts.emplace_back(curmx, events[t].first);
104                 }
105                 length[{p, q}] += mxlen;
106             }
107             auto&& i = events[t].second;
108             if (i < n) {
109                 if (covers.count(i)) {
110                     covers.erase(i);
111                 } else {
112                     covers.insert(i);
113                 }
114             }
115         }
116         int q, t;
117         scanf("%d", &q);
118         while (q --) {
119             scanf("%d", &t);
120             auto it = std::lower_bound(parts.begin(), parts.end(), std::make_pair(t, 0));
121             printf("%d\n", it->second - (it->first - t));
122         }
123     }
124 }

 

目录
相关文章
|
12月前
|
JavaScript 前端开发 索引
JS 删除数组元素( 5种方法 )
JS 删除数组元素( 5种方法 )
594 1
|
存储 JavaScript API
在Vue中,如何实现状态的共享?
在Vue中,如何实现状态的共享?
343 41
Linux 命令 `chown`:改变文件或目录的所有者
`chown` 是 Linux 中用于改变文件或目录所有者的命令。基本语法是 `chown [选项] 新所有者 文件或目录...`。常用选项包括 `-R` 递归更改、`-c` 显示详细信息和 `-v` 显示详细处理。示例:将 `example.txt` 所有者改为 `user2` 使用 `chown user2 example.txt`;更改目录 `mydir` 及其内容所有者为 `user2` 使用 `chown -R user2 mydir`。注意,通常只有 root 或当前所有者能更改所有者,且需谨慎操作以避免影响权限。
|
Shell 应用服务中间件 nginx
Docker命令集大全(Docker命令,一篇搞定)
【1月更文挑战第12天】 一、Docker容器命令: 二、Docker镜像命令 三、重启Docker命令 四、Docker数据卷命令 五、挂载数据卷
610 3
|
网络协议 Java 关系型数据库
PolarDB-X 私有协议2.0
本文主要介绍私有协议2.0,也即XRPC的背景、总体设计、相关技术实现细节和性能测试结果。
|
存储 安全 Java
每日一道面试题之Collection 和 Collections 有什么区别?
每日一道面试题之Collection 和 Collections 有什么区别?
200 0
|
存储 编解码 安全
阿里云服务器五代、六代、七代、八代实例规格与经济型e和通用算力型u1实例介绍
阿里云服务器实例规格经过多次升级之后,目前已经推出了最新的第八代云服务器,当下主售的云服务器实例也以及从以往的六代七代过渡到七代和八代为主,同时还有通用算力型及经济型这些刚推出不久的新品云服务器实例,对于有的新手用户来说,并不是很清楚阿里云五代、六代、七代、八代云服务器的实例规格具体有哪些,以及它们之间有何区别,最新一代相比于上一代在性能上有何提升。本文对阿里云五代、六代云服务器实例规格做个简单介绍,同时对七代和八代云服务器做个重点介绍,以供大家参考。
阿里云服务器五代、六代、七代、八代实例规格与经济型e和通用算力型u1实例介绍
|
关系型数据库 分布式数据库 数据库
PolarDB for PostgreSQL报错问题之psql连接数据库报错如何解决
PolarDB for PostgreSQL是基于PostgreSQL开发的一款云原生关系型数据库服务,它提供了高性能、高可用性和弹性扩展的特性;本合集将围绕PolarDB(pg)的部署、管理和优化提供指导,以及常见问题的排查和解决办法。
|
存储 前端开发 JavaScript
人人都是前端架构师:我来带你阅读 React18 源码!
人人都是前端架构师:我来带你阅读 React18 源码!
654 0