#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e5 + 5;
long long mn[4 * mxN], mx[4 * mxN], lazy[4 * mxN];
void operate(int x, long long v) {
mn[x] += v;
mx[x] += v;
lazy[x] += v;
}
void push(int x, int lx, int rx) {
if (lx == rx) return;
operate(x << 1, lazy[x]);
operate(x << 1 | 1, lazy[x]);
lazy[x] = 0;
}
void update(int x, int lx, int rx, int l, int r, int v) {
if (lazy[x]) push(x, lx, rx);
if (l > rx || lx > r) return;
if (l <= lx && r >= rx) {
operate(x, v);
return;
}
int m = (lx + rx) >> 1;
update(x << 1, lx, m, l, r, v);
update(x << 1 | 1, m + 1, rx, l, r, v);
mn[x] = min(mn[x << 1], mn[x << 1 | 1]);
mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
}
long long sum;
int query(int x, int lx, int rx, long long cmn, long long cmx, int c) {
if (lazy[x]) push(x, lx, rx);
if (lx == rx) {
if (mn[x] <= cmx) return sum - (cmx - c);
return sum - cmn;
}
int m = (lx + rx) >> 1;
if (max(cmx, mx[x << 1 | 1]) - min(cmn, mn[x << 1 | 1]) >= c) return query(x << 1 | 1, m + 1, rx, cmn, cmx, c);
return query(x << 1, lx, m, min(cmn, mn[x << 1 | 1]), max(cmx, mx[x << 1 | 1]), c);
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l, std::vector<int> r, std::vector<int> v) {
int n = c.size();
vector<pair<int, int>> updates[n];
for (int i = 0; i < l.size(); i++) {
updates[l[i]].push_back({i + 2, v[i]});
if (r[i] < n - 1) updates[r[i] + 1].push_back({i + 2, -v[i]});
}
int Q = l.size() + 2;
vector<int> ans(n);
for (int i = 0; i < n; i++) {
for (auto [j, val] : updates[i]) {
sum += val;
update(1, 1, n, j, Q, val);
}
ans[i] = query(1, 1, Q, 1e18, -1e18, c[i]);
}
return ans;
}
Compilation message
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:44:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < l.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
337 ms |
34104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |