#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[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 == query(1, 0, q + 1, ans, q + 1).first) {
l = ans + 1, r = q + 1;
int anss = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (query(1, 0, q + 1, ans, q + 1).second >= k + c[i]) {
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 - 1);
}
else {
l = ans + 1, r = q + 1;
int anss = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (query(1, 0, q + 1, ans, q + 1).first + c[i] <= k) {
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;
}
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:112:23: warning: variable 'u' set but not used [-Wunused-but-set-variable]
112 | pair <ll, ll> u = query(1, 0, q + 1, ans, q + 1);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
148 ms |
59228 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |