#include "candies.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
typedef long long ll;
const int N = 2e5 + 10;
const int INF = 1e9 + 10;
using namespace std;
struct Node {
ll lazy;
ll mx;
ll mn;
};
Node tree[4 * N];
void pushh(int v, int tl, int tr) {
if (tree[v].lazy != 0) {
if (tl != tr) {
tree[2 * v].mx += tree[v].lazy;
tree[2 * v].mn += tree[v].lazy;
tree[2 * v + 1].mx += tree[v].lazy;
tree[2 * v + 1].mn += tree[v].lazy;
tree[2 * v].lazy += tree[v].lazy;
tree[2 * v + 1].lazy += tree[v].lazy;
}
tree[v].lazy = 0;
}
}
void build_tree(int v, int tl, int tr) {
tree[v].lazy = 0;
tree[v].mx = 0;
tree[v].mn = 0;
if (tl != tr) {
int tm = (tl + tr) / 2;
build_tree(2 * v, tl, tm);
build_tree(2 * v + 1, tm + 1, tr);
}
}
void update(int v, int tl, int tr, int l, int r, ll x) {
if (tl > r || tr < l) return;
if (tl >= l && tr <= r) {
tree[v].mx += x;
tree[v].mn += x;
tree[v].lazy += x;
return;
}
pushh(v, tl, tr);
int tm = (tl + tr) / 2;
update(2 * v, tl, tm, l, r, x);
update(2 * v + 1, tm + 1, tr, l, r, x);
tree[v].mx = max(tree[2 * v].mx, tree[2 * v + 1].mx);
tree[v].mn = min(tree[2 * v].mn, tree[2 * v + 1].mn);
}
pair <ll, ll> query(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l) return {1e18, -1e18};
if (tl >= l && tr <= r) return { tree[v].mn, tree[v].mx };
pushh(v, tl, tr);
int tm = (tl + tr) / 2;
pair <ll, ll> f1 = query(2 * v, tl, tm, l, r);
pair <ll, ll> f2 = query(2 * v + 1, tm + 1, tr, l, r);
return {min(f1.first, f2.first), max(f1.second, f2.second)};
}
ll find_value(int v, int tl, int tr, int pos) {
if (tl == tr) return tree[v].mx;
pushh(v, tl, tr);
int tm = (tl + tr) / 2;
if (pos <= tm) return find_value(2 * v, tl, tm, pos);
else return find_value(2 * v + 1, tm + 1, tr, pos);
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = (int)c.size();
int q = (int)l.size();
vector <vector<pair<ll, int>>> add(n + 1);
vector <vector<pair<ll, int>>> erase(n + 1);
vector <ll> answer(n);
for (int i = 0; i < q; i++) {
add[l[i]].push_back({ v[i], i });
erase[r[i] + 1].push_back({ -v[i], i });
}
build_tree(1, 0, q + 1);
update(1, 0, q + 1, 0, q + 1, INF);
update(1, 0, q + 1, 1, q + 1, -INF);
for (int i = 0; i < n; i++) {
for (auto it : add[i]) {
update(1, 0, q + 1, it.second + 2, q + 1, it.first);
}
for (auto it : erase[i]) {
update(1, 0, q + 1, it.second + 2, q + 1, it.first);
}
int l = 0, r = q + 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
pair <ll, ll> u = query(1, 0, q + 1, mid, q + 1);
if (u.first + c[i] <= u.second) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
pair <ll, ll> u = query(1, 0, q + 1, ans, q + 1);
ll k = find_value(1, 0, q + 1, ans);
if (k == u.first) {
l = ans + 1, r = q + 1;
int anss = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (query(1, 0, q + 1, mid, q + 1).second == u.second) {
anss = mid;
l = mid + 1;
}
else r = mid - 1;
}
answer[i] = c[i] + find_value(1, 0, q + 1, q + 1) - find_value(1, 0, q + 1, anss);
}
else {
l = ans + 1, r = q + 1;
int anss = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (query(1, 0, q + 1, mid, q + 1).first == u.first) {
anss = mid;
l = mid + 1;
}
else r = mid - 1;
}
answer[i] = find_value(1, 0, q + 1, q + 1) - find_value(1, 0, q + 1, anss);
}
}
vector <int> qwe;
for (int i = 0; i < n; i++) {
qwe.push_back(answer[i]);
}
return qwe;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1137 ms |
41468 KB |
Output is correct |
2 |
Correct |
1240 ms |
45536 KB |
Output is correct |
3 |
Correct |
1266 ms |
45380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
219 ms |
28124 KB |
Output is correct |
3 |
Correct |
418 ms |
16436 KB |
Output is correct |
4 |
Correct |
1479 ms |
47384 KB |
Output is correct |
5 |
Correct |
1346 ms |
47792 KB |
Output is correct |
6 |
Correct |
1000 ms |
48172 KB |
Output is correct |
7 |
Correct |
984 ms |
47524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
99 ms |
23880 KB |
Output is correct |
4 |
Correct |
348 ms |
15300 KB |
Output is correct |
5 |
Correct |
959 ms |
41392 KB |
Output is correct |
6 |
Correct |
862 ms |
42072 KB |
Output is correct |
7 |
Correct |
685 ms |
42656 KB |
Output is correct |
8 |
Correct |
1018 ms |
41304 KB |
Output is correct |
9 |
Correct |
1121 ms |
42780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
1137 ms |
41468 KB |
Output is correct |
7 |
Correct |
1240 ms |
45536 KB |
Output is correct |
8 |
Correct |
1266 ms |
45380 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
219 ms |
28124 KB |
Output is correct |
11 |
Correct |
418 ms |
16436 KB |
Output is correct |
12 |
Correct |
1479 ms |
47384 KB |
Output is correct |
13 |
Correct |
1346 ms |
47792 KB |
Output is correct |
14 |
Correct |
1000 ms |
48172 KB |
Output is correct |
15 |
Correct |
984 ms |
47524 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
99 ms |
23880 KB |
Output is correct |
19 |
Correct |
348 ms |
15300 KB |
Output is correct |
20 |
Correct |
959 ms |
41392 KB |
Output is correct |
21 |
Correct |
862 ms |
42072 KB |
Output is correct |
22 |
Correct |
685 ms |
42656 KB |
Output is correct |
23 |
Correct |
1018 ms |
41304 KB |
Output is correct |
24 |
Correct |
1121 ms |
42780 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
384 ms |
15448 KB |
Output is correct |
27 |
Correct |
197 ms |
30692 KB |
Output is correct |
28 |
Correct |
1356 ms |
46024 KB |
Output is correct |
29 |
Correct |
1242 ms |
46420 KB |
Output is correct |
30 |
Correct |
1134 ms |
46612 KB |
Output is correct |
31 |
Correct |
1086 ms |
46824 KB |
Output is correct |
32 |
Correct |
1094 ms |
46960 KB |
Output is correct |