#include "candies.h"
#ifdef NYAOWO
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;
inline void chmin(int &x, const int &v) {
x = min(x, v);
}
inline void chmax(int &x, const int &v) {
x = max(x, v);
}
const int MAXN = 200'000;
const int MAXQ = MAXN;
const int INF = 1e18;
// single point add, min/max sum query on suffixs
#define L(id) ((id) * 2 + 1)
#define R(id) ((id) * 2 + 2)
struct SegTree {
struct Node {
int mns, mxs, sum;
// min suffix, max suffix, sum
} a[MAXN * 4 + 10];
void pull(int id) {
a[id].mns = min(a[L(id)].mns + a[R(id)].sum, a[R(id)].mns);
a[id].mxs = max(a[L(id)].mxs + a[R(id)].sum, a[R(id)].mxs);
a[id].sum = a[L(id)].sum + a[R(id)].sum;
}
// initialize
void build() {
memset(a, 0, sizeof(a));
}
// add value for a single point
void single_add(int id, int l, int r, int pos, int val) {
if(l == r) {
a[id].mns += val;
a[id].mxs += val;
a[id].sum += val;
return;
}
int m = l + (r - l) / 2;
if(pos <= m) single_add(L(id), l, m, pos, val);
if(pos > m) single_add(R(id), m + 1, r, pos, val);
pull(id);
}
// {pos, {min, max}}
int solve(int q, int c) {
int id = 0, l = 0, r = q + 1;
int mx = 0, mn = 0, sum = 0;
while(l != r) {
int nmx = max(mx, a[R(id)].mxs + sum);
int nmn = min(mn, a[R(id)].mns + sum);
int m = l + (r - l) / 2;
if(nmx - nmn > c) {
id = R(id);
l = m + 1;
} else {
mx = nmx; mn = nmn;
sum += a[R(id)].sum;
id = L(id);
r = m;
}
}
if(a[id].sum < 0) return c - mx;
else return -mn;
}
} seg;
vector<i32> distribute_candies(vector<i32> C, vector<i32> LE, vector<i32> RI, vector<i32> VAL) {
int n = sz(C);
int q = sz(VAL);
seg.build();
seg.single_add(0, 0, q + 1, 0, INF);
struct Event { int time, pos, val; };
vector<Event> ev;
For(i, 0, q - 1) {
ev.push_back({LE[i], i + 1, -VAL[i]});
ev.push_back({RI[i] + 1, i + 1, VAL[i]});
}
sort(all(ev), [&](const Event &e1, const Event &e2) {
return e1.time > e2.time;
});
vector<int> ans(n);
For(i, 0, n - 1) {
while(sz(ev) && ev.back().time == i) {
auto e = ev.back(); ev.pop_back();
seg.single_add(0, 0, q + 1, e.pos, e.val);
}
int c = C[i];
ans[i] = seg.solve(q, c);
}
return vector<i32>(all(ans));
}
/*
3
10 15 13
2
0 2 20
0 1 -11
0 4 13
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19208 KB |
Output is correct |
4 |
Correct |
9 ms |
19284 KB |
Output is correct |
5 |
Correct |
9 ms |
19284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
37728 KB |
Output is correct |
2 |
Correct |
292 ms |
37740 KB |
Output is correct |
3 |
Correct |
244 ms |
37812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19028 KB |
Output is correct |
2 |
Correct |
203 ms |
36152 KB |
Output is correct |
3 |
Correct |
66 ms |
23228 KB |
Output is correct |
4 |
Correct |
251 ms |
37816 KB |
Output is correct |
5 |
Correct |
291 ms |
37736 KB |
Output is correct |
6 |
Correct |
264 ms |
37740 KB |
Output is correct |
7 |
Correct |
280 ms |
37732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19000 KB |
Output is correct |
3 |
Correct |
93 ms |
36184 KB |
Output is correct |
4 |
Correct |
52 ms |
23240 KB |
Output is correct |
5 |
Correct |
138 ms |
37724 KB |
Output is correct |
6 |
Correct |
155 ms |
37816 KB |
Output is correct |
7 |
Correct |
143 ms |
37732 KB |
Output is correct |
8 |
Correct |
138 ms |
37724 KB |
Output is correct |
9 |
Correct |
154 ms |
37728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19208 KB |
Output is correct |
4 |
Correct |
9 ms |
19284 KB |
Output is correct |
5 |
Correct |
9 ms |
19284 KB |
Output is correct |
6 |
Correct |
243 ms |
37728 KB |
Output is correct |
7 |
Correct |
292 ms |
37740 KB |
Output is correct |
8 |
Correct |
244 ms |
37812 KB |
Output is correct |
9 |
Correct |
7 ms |
19028 KB |
Output is correct |
10 |
Correct |
203 ms |
36152 KB |
Output is correct |
11 |
Correct |
66 ms |
23228 KB |
Output is correct |
12 |
Correct |
251 ms |
37816 KB |
Output is correct |
13 |
Correct |
291 ms |
37736 KB |
Output is correct |
14 |
Correct |
264 ms |
37740 KB |
Output is correct |
15 |
Correct |
280 ms |
37732 KB |
Output is correct |
16 |
Correct |
7 ms |
19028 KB |
Output is correct |
17 |
Correct |
9 ms |
19000 KB |
Output is correct |
18 |
Correct |
93 ms |
36184 KB |
Output is correct |
19 |
Correct |
52 ms |
23240 KB |
Output is correct |
20 |
Correct |
138 ms |
37724 KB |
Output is correct |
21 |
Correct |
155 ms |
37816 KB |
Output is correct |
22 |
Correct |
143 ms |
37732 KB |
Output is correct |
23 |
Correct |
138 ms |
37724 KB |
Output is correct |
24 |
Correct |
154 ms |
37728 KB |
Output is correct |
25 |
Correct |
7 ms |
19028 KB |
Output is correct |
26 |
Correct |
54 ms |
23152 KB |
Output is correct |
27 |
Correct |
179 ms |
36152 KB |
Output is correct |
28 |
Correct |
273 ms |
37724 KB |
Output is correct |
29 |
Correct |
237 ms |
37724 KB |
Output is correct |
30 |
Correct |
258 ms |
37728 KB |
Output is correct |
31 |
Correct |
234 ms |
37724 KB |
Output is correct |
32 |
Correct |
230 ms |
37724 KB |
Output is correct |