#pragma GCC optimize("Ofast,unroll-loops")
#include "candies.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
ll m, M, sum;
bool check(int c)
{
return M - m < c;
}
} seg[850005];
node merge(node p, node q)
{
auto [lm, lM, lsum] = p;
auto [rm, rM, rsum] = q;
return node{min(lm + rsum, rm), max(lM + rsum, rM), lsum + rsum};
}
int n, q;
node emp = {0, 0, 0};
void init()
{
for (int i = 1; i <= 4 * (q + 2); i++)
seg[i] = emp;
}
void modify(int pos, node val, int i = 1, int L = 0, int R = q + 1)
{
if (L == R)
seg[i] = val;
else
{
int mid = (L + R) / 2;
if (pos <= mid)
modify(pos, val, i << 1, L, mid);
else
modify(pos, val, i << 1 | 1, mid + 1, R);
seg[i] = merge(seg[i << 1], seg[i << 1 | 1]);
}
}
pair<int, node> search(int c, node suf = emp, int i = 1, int L = 0, int R = q + 1)
{
if (L == R)
return make_pair(L, suf);
else
{
int mid = (L + R) / 2;
if (merge(seg[i << 1 | 1], suf).check(c))
return search(c, merge(seg[i << 1 | 1], suf), i << 1, L, mid);
else
return search(c, suf, i << 1 | 1, mid + 1, R);
}
}
vector<int> in[200000], out[200001];
node base[200002];
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v)
{
vector<int> ans;
n = c.size(), q = l.size();
v.insert(v.begin(), -1);
l.insert(l.begin(), 0);
r.insert(r.begin(), 0);
for (int i = 1; i <= q; i++)
{
in[l[i]].emplace_back(i);
out[r[i] + 1].emplace_back(i);
if (v[i] < 0)
base[i] = node{v[i], 0, v[i]};
else
base[i] = node{0, v[i], v[i]};
}
init();
for (int i = 0; i < n; i++)
{
for (auto j : in[i])
modify(j, base[j]);
for (auto j : out[i])
modify(j, emp);
auto [L, s] = search(c[i]);
if (v[L] < 0)
ans.emplace_back(s.M);
else
ans.emplace_back(c[i] + s.m);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
8 ms |
9948 KB |
Output is correct |
4 |
Correct |
6 ms |
9972 KB |
Output is correct |
5 |
Correct |
7 ms |
10024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
338 ms |
48388 KB |
Output is correct |
2 |
Correct |
335 ms |
48376 KB |
Output is correct |
3 |
Correct |
308 ms |
48352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
164 ms |
40748 KB |
Output is correct |
3 |
Correct |
64 ms |
13676 KB |
Output is correct |
4 |
Correct |
323 ms |
48332 KB |
Output is correct |
5 |
Correct |
301 ms |
48364 KB |
Output is correct |
6 |
Correct |
309 ms |
48392 KB |
Output is correct |
7 |
Correct |
295 ms |
48388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
81 ms |
40816 KB |
Output is correct |
4 |
Correct |
55 ms |
12676 KB |
Output is correct |
5 |
Correct |
142 ms |
43140 KB |
Output is correct |
6 |
Correct |
141 ms |
43124 KB |
Output is correct |
7 |
Correct |
136 ms |
43124 KB |
Output is correct |
8 |
Correct |
142 ms |
43168 KB |
Output is correct |
9 |
Correct |
180 ms |
43124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
8 ms |
9948 KB |
Output is correct |
4 |
Correct |
6 ms |
9972 KB |
Output is correct |
5 |
Correct |
7 ms |
10024 KB |
Output is correct |
6 |
Correct |
338 ms |
48388 KB |
Output is correct |
7 |
Correct |
335 ms |
48376 KB |
Output is correct |
8 |
Correct |
308 ms |
48352 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
164 ms |
40748 KB |
Output is correct |
11 |
Correct |
64 ms |
13676 KB |
Output is correct |
12 |
Correct |
323 ms |
48332 KB |
Output is correct |
13 |
Correct |
301 ms |
48364 KB |
Output is correct |
14 |
Correct |
309 ms |
48392 KB |
Output is correct |
15 |
Correct |
295 ms |
48388 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
81 ms |
40816 KB |
Output is correct |
19 |
Correct |
55 ms |
12676 KB |
Output is correct |
20 |
Correct |
142 ms |
43140 KB |
Output is correct |
21 |
Correct |
141 ms |
43124 KB |
Output is correct |
22 |
Correct |
136 ms |
43124 KB |
Output is correct |
23 |
Correct |
142 ms |
43168 KB |
Output is correct |
24 |
Correct |
180 ms |
43124 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
60 ms |
12844 KB |
Output is correct |
27 |
Correct |
185 ms |
40680 KB |
Output is correct |
28 |
Correct |
388 ms |
48452 KB |
Output is correct |
29 |
Correct |
334 ms |
48376 KB |
Output is correct |
30 |
Correct |
303 ms |
48384 KB |
Output is correct |
31 |
Correct |
326 ms |
48400 KB |
Output is correct |
32 |
Correct |
337 ms |
48340 KB |
Output is correct |