Sponsors

Codeforces Round #426 (Div. 1), problem: (E) Caramel Clouds Solution In C/C++

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <utility>
#include <vector>

const int N = 300000;

struct Sum
{
int add(int id, int value)
{
if (a[0].second == id) {
a[0].first = std::max(a[0].first, value);
} else if (a[1].first < value) {
a[1] = {value, id};
}
if (a[0].first < a[1].first) {
std::swap(a[0], a[1]);
}
}

int ask(int id)
{
if (a[0].second != id) {
return a[0].first;
}
return a[1].first;
}

std::pair<int, int> a[2] = {{0, -1}, {0, -1}};
};

int cost[N + 1], toupd[N];

int main()
{
#ifdef LOCAL_JUDGE
freopen(“E.in”, “r”, stdin);
#endif
int n, budget;
while (scanf(“%d%d”, &n, &budget) == 2) {
cost[n] = 0;
std::vector<std::pair<int, int>> events;
events.emplace_back(0, n);
events.emplace_back(2000000000, n);
for (int i = 0, 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[0].second < n) {
covers.insert(events[0].second);
}
int curmx = 0;
std::vector<std::pair<int, int>> parts;
memset(toupd, 0, sizeof(toupd));
std::vector<Sum> bit(values.size());
std::map<std::pair<int, int>, int> length;
for (int t = 1; t < (int)events.size(); ++ t) {
int mxlen = events[t].first – events[t – 1].first;
if (mxlen > 0 && (int)covers.size() <= 2) {
int p = n, q = n;
if ((int)covers.size() > 0) {
p = *covers.begin();
}
if ((int)covers.size() > 1) {
q = *covers.rbegin();
}
int start = -1;
if (p == n) { // 0
start = curmx;
} else {
if (q == n) { // 1
if (cost[p] <= budget) {
start = toupd[p];
for (int k = (int)(std::upper_bound(values.begin(), values.end(), budget – cost[p]) – values.begin()) – 1; k >= 0; k -= ~k & k + 1) {
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 + 1) {
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, 0));
printf(“%d\n”, it->second – (it->first – t));
}
}
}

Hollywood’s AI Bet Isn’t...

Why Hollywood's Big Bet on AI Is Turning into...

How far can a...

The Scent-Sational Truth: How Far Can a Buck Smell? For...

Google’s Gemini 3 Release...

Google Gemini 3 Racks Up 100 Million New Active...

Nvidia nears deal to...

Nvidia Nears Historic $25.4 Billion Investment in OpenAI Funding...

Palantir Touts $2 Billion...

Palantir Claims $2 Billion Revenue from Controversial Trump Administration...

Trevor Noah roasts Nicki...

Trevor Noah Takes Aim: Roasting Nicki Minaj's Trump Ties...

Hollywood’s AI Bet Isn’t Paying Off

Why Hollywood's Big Bet on AI Is Turning into a Box Office Flop The convergence of Artificial Intelligence and cinema was once touted as the...

How far can a buck smell?

The Scent-Sational Truth: How Far Can a Buck Smell? For wildlife enthusiasts and dedicated hunters, understanding the sensory world of the whitetail deer is crucial....

Google’s Gemini 3 Release Won Over More Than 100 Million New Active Users

Google Gemini 3 Racks Up 100 Million New Active Users, Challenging ChatGPT's Dominance The artificial intelligence landscape is witnessing a seismic shift, confirmed by new...

Nvidia nears deal to invest $25.4 billion in OpenAI funding round

Nvidia Nears Historic $25.4 Billion Investment in OpenAI Funding Round The landscape of artificial intelligence is poised for another seismic shift as reports indicate that...

Palantir Touts $2 Billion in Revenue from Aiding Trump Administration’s ‘Unusual’ Operations

Palantir Claims $2 Billion Revenue from Controversial Trump Administration Operations Palantir Technologies, the major data analytics firm, has recently boasted about achieving a staggering financial...

Trevor Noah roasts Nicki Minaj’s Trump ties during Grammy Awards opening monologue ceremony

Trevor Noah Takes Aim: Roasting Nicki Minaj's Trump Ties at the 2026 GrammysThe 2026 Grammy Awards kicked off in Los Angeles with a bang,...

Arunachal Cabinet meets at Anini, clears major reforms on jobs, hydropower and local governance

Arunachal Cabinet Convenes in Remote Anini, Greenlights Sweeping Reforms on Jobs and Governance In a powerful demonstration of taking administration directly to the citizens, the...

Shipping Store Simulator 2037 Announced For Steam

Get Ready to Manage Mail: Shipping Store Simulator 2037 Announced for Steam The simulation genre continues its rapid expansion, offering players the chance to master...

Columbia Museum of Art – Executive Director

Columbia Museum of Art Seeks Executive Director to Steward 75-Year Legacy The cultural landscape of South Carolina is buzzing with significant news: the renowned Columbia...