#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = (1 << 18);
int up[2 * N];
pair<int, int> mn[2 * N];
pair<int, int> mx[2 * N];
void init() {
for (int i = 2 * N - 1; i >= 1; i--) {
if (i >= N)
mn[i].second = mx[i].second = i - N;
else
mn[i].second = mx[i].second = mn[2 * i].second;
mn[i].first = mx[i].first = 0;
}
}
void setpush(int v, int d) {
up[v] += d;
mn[v].first += d;
mx[v].first += d;
}
void push(int v) {
if (v >= N || !up[v]) return;
setpush(2 * v, up[v]);
setpush(2 * v + 1, up[v]);
up[v] = 0;
}
void update(int ql, int qr, int d, int l = 0, int r = N - 1, int v = 1) {
if (r < ql || l > qr)
return;
if (ql <= l && r <= qr) {
setpush(v, d);
return;
}
int mid = (l + r) / 2;
push(v);
update(ql, qr, d, l, mid, 2 * v);
update(ql, qr, d, mid + 1, r, 2 * v + 1);
mn[v] = min(mn[2 * v], mn[2 * v + 1]);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
}
int get(int pos, int l = 0, int r = N - 1, int v = 1) {
if (l == r)
return mn[v].first;
int mid = (l + r) / 2;
push(v);
if (pos <= mid)
return get(pos, l, mid, 2 * v);
return get(pos, mid + 1, r, 2 * v + 1);
}
pair<int, int> get_min(int ql, int qr, int l = 0, int r = N - 1, int v = 1) {
if (r < ql || l > qr)
return {INF, 0};
if (ql <= l && r <= qr)
return mn[v];
int mid = (l + r) / 2;
push(v);
return min(get_min(ql, qr, l, mid, 2 * v),
get_min(ql, qr, mid + 1, r, 2 * v + 1));
}
pair<int, int> get_max(int ql, int qr, int l = 0, int r = N - 1, int v = 1) {
if (r < ql || l > qr)
return {-INF, 0};
if (ql <= l && r <= qr)
return mx[v];
int mid = (l + r) / 2;
push(v);
return max(get_max(ql, qr, l, mid, 2 * v),
get_max(ql, qr, mid + 1, r, 2 * v + 1));
}
int calc(int c) {
int l = 0, r = N - 1;
while (r - l > 1) {
int mid = (l + r) / 2;
if (get_max(mid, N - 1).first - get_min(mid, N - 1).first >= c)
l = mid;
else
r = mid;
}
// cout << c << ":\n";
// cout << get_min(l, N - 1).first << ' ';
// cout << get_max(l, N - 1).first << '\n';
// cout << '\n';
if (get_min(l, N - 1).second < get_max(l, N - 1).second) {
return get(N - 1) - get_max(l, N - 1).first + c;
} else {
return get(N - 1) - get_min(l, N - 1).first;
}
}
vector<pair<int, int>> upd[N];
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
init();
update(0, 0, +INF);
int n = c.size(), q = l.size();
for (int i = 0; i < q; i++) {
upd[l[i]].push_back({v[i], i + 2});
upd[r[i]+1].push_back({-v[i], i + 2});
}
vector<int> s(n);
for (int i = 0; i < n; i++) {
for (auto [v, p] : upd[i])
update(p, N - 1, v);
s[i] = calc(c[i]);
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
14940 KB |
Output is correct |
3 |
Correct |
7 ms |
14940 KB |
Output is correct |
4 |
Correct |
8 ms |
14904 KB |
Output is correct |
5 |
Correct |
19 ms |
15172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1521 ms |
34436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
14940 KB |
Output is correct |
2 |
Incorrect |
203 ms |
28648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14940 KB |
Output is correct |
2 |
Correct |
10 ms |
14940 KB |
Output is correct |
3 |
Correct |
93 ms |
26948 KB |
Output is correct |
4 |
Correct |
988 ms |
18636 KB |
Output is correct |
5 |
Correct |
1071 ms |
30164 KB |
Output is correct |
6 |
Correct |
1030 ms |
30840 KB |
Output is correct |
7 |
Incorrect |
995 ms |
31124 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
14940 KB |
Output is correct |
3 |
Correct |
7 ms |
14940 KB |
Output is correct |
4 |
Correct |
8 ms |
14904 KB |
Output is correct |
5 |
Correct |
19 ms |
15172 KB |
Output is correct |
6 |
Incorrect |
1521 ms |
34436 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |