#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 = 1 << 18;
const int MAXQ = MAXN;
const int INF = 1e18;
// single point add, min/max sum query on suffixs
#define L(id) ((id) * 2)
#define R(id) ((id) * 2 + 1)
struct SegTree {
struct Node {
int mns, mxs, sum;
// min suffix, max suffix, sum
} a[MAXN << 1];
inline 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 pos, int val) {
int id = pos + MAXN;
a[id].mns += val;
a[id].mxs += val;
a[id].sum += val;
for(id /= 2; id; id /= 2) pull(id);
}
// {pos, {min, max}}
int solve(int c) {
int id = 1, l = 0, r = MAXN - 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, 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(e.pos, e.val);
}
int c = C[i];
ans[i] = seg.solve(c);
}
return vector<i32>(all(ans));
}
/*
3
10 15 13
2
0 2 20
0 1 -11
0 4 13
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12596 KB |
Output is correct |
2 |
Correct |
5 ms |
12500 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
6 ms |
12816 KB |
Output is correct |
5 |
Correct |
6 ms |
12884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
36124 KB |
Output is correct |
2 |
Correct |
204 ms |
35388 KB |
Output is correct |
3 |
Correct |
189 ms |
35152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12628 KB |
Output is correct |
2 |
Correct |
120 ms |
32708 KB |
Output is correct |
3 |
Correct |
53 ms |
18872 KB |
Output is correct |
4 |
Correct |
187 ms |
37192 KB |
Output is correct |
5 |
Correct |
182 ms |
37596 KB |
Output is correct |
6 |
Correct |
191 ms |
37968 KB |
Output is correct |
7 |
Correct |
200 ms |
37276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12600 KB |
Output is correct |
2 |
Correct |
5 ms |
12628 KB |
Output is correct |
3 |
Correct |
110 ms |
32324 KB |
Output is correct |
4 |
Correct |
51 ms |
17000 KB |
Output is correct |
5 |
Correct |
145 ms |
31576 KB |
Output is correct |
6 |
Correct |
178 ms |
31508 KB |
Output is correct |
7 |
Correct |
125 ms |
31520 KB |
Output is correct |
8 |
Correct |
130 ms |
31556 KB |
Output is correct |
9 |
Correct |
161 ms |
31496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12596 KB |
Output is correct |
2 |
Correct |
5 ms |
12500 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
6 ms |
12816 KB |
Output is correct |
5 |
Correct |
6 ms |
12884 KB |
Output is correct |
6 |
Correct |
197 ms |
36124 KB |
Output is correct |
7 |
Correct |
204 ms |
35388 KB |
Output is correct |
8 |
Correct |
189 ms |
35152 KB |
Output is correct |
9 |
Correct |
5 ms |
12628 KB |
Output is correct |
10 |
Correct |
120 ms |
32708 KB |
Output is correct |
11 |
Correct |
53 ms |
18872 KB |
Output is correct |
12 |
Correct |
187 ms |
37192 KB |
Output is correct |
13 |
Correct |
182 ms |
37596 KB |
Output is correct |
14 |
Correct |
191 ms |
37968 KB |
Output is correct |
15 |
Correct |
200 ms |
37276 KB |
Output is correct |
16 |
Correct |
5 ms |
12600 KB |
Output is correct |
17 |
Correct |
5 ms |
12628 KB |
Output is correct |
18 |
Correct |
110 ms |
32324 KB |
Output is correct |
19 |
Correct |
51 ms |
17000 KB |
Output is correct |
20 |
Correct |
145 ms |
31576 KB |
Output is correct |
21 |
Correct |
178 ms |
31508 KB |
Output is correct |
22 |
Correct |
125 ms |
31520 KB |
Output is correct |
23 |
Correct |
130 ms |
31556 KB |
Output is correct |
24 |
Correct |
161 ms |
31496 KB |
Output is correct |
25 |
Correct |
4 ms |
12500 KB |
Output is correct |
26 |
Correct |
52 ms |
17068 KB |
Output is correct |
27 |
Correct |
141 ms |
32324 KB |
Output is correct |
28 |
Correct |
189 ms |
35872 KB |
Output is correct |
29 |
Correct |
192 ms |
36188 KB |
Output is correct |
30 |
Correct |
235 ms |
36376 KB |
Output is correct |
31 |
Correct |
184 ms |
36548 KB |
Output is correct |
32 |
Correct |
183 ms |
36760 KB |
Output is correct |