#include <bits/stdc++.h>
//#include "grader.cpp"
#include "candies.h"
#define F first
#define S second
#define pb push_back
#define sz(x) (int)x.size()
#define el "\n"
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
struct node {
ll mn, mx, D;
node() {mn = 2e18; mx = -2e18; D = 0;}
node(ll x) {mn = mx = x; D = 0;}
void mrg(node &l, node &r) {
mn = min(l.mn, r.mn);
mx = max(l.mx, r.mx);
}
};
struct seg_tree {
vector <node> t;
int n;
seg_tree() {}
seg_tree(int _n) {
n = _n;
t.resize(8 * n, node(0));
}
void push(int v) {
if (t[v].D) {
t[v].mn += t[v].D;
t[v].mx += t[v].D;
t[v * 2].D += t[v].D;
t[v * 2 + 1].D += t[v].D;
t[v].D = 0;
}
}
void upd(int v, int l, int r, int tl, int tr, ll val) {
push(v);
if (l > r || tl > tr || tl > r || l > tr) {
return;
}
if (l >= tl && r <= tr) {
t[v].D += val;
push(v);
return;
}
int md = (l + r) >> 1;
upd(v * 2, l, md, tl, tr, val);
upd(v * 2 + 1, md + 1, r, tl, tr, val);
t[v].mrg(t[v * 2], t[v * 2 + 1]);
}
node get(int v, int l, int r, int tl, int tr) {
push(v);
if (l > r || tl > tr || tl > r || l > tr) {
return node();
}
if (l >= tl && r <= tr) {
return t[v];
}
int md = (l + r) >> 1;
node L = get(v * 2, l, md, tl, tr);
node R = get(v * 2 + 1, md + 1, r, tl, tr);
node cur = node();
cur.mrg(L, R);
return cur;
}
void upd(int l, int r, ll val) {
upd(1, 0, n - 1, l, r, val);
}
node get(int l, int r) {
return get(1, 0, n - 1, l, r);
}
};
vector <int> distribute_candies(vector <int> c, vector <int> l, vector <int> r, vector <int> v) {
int n = sz(c), q = sz(l);
vector <vector <int> > add(n + 1), del(n + 1);
for (int i = 0; i < q; i++) {
add[l[i]].pb(i + 1);
del[r[i] + 1].pb(i + 1);
}
seg_tree t(q + 1);
vector <int> ans;
for (int i = 0; i < n; i++) {
for (auto &id : add[i]) {
t.upd(id, q, v[id - 1]);
}
for (auto &id : del[i]) {
t.upd(id, q, -v[id - 1]);
}
int l = 0;
int r = q;
while (l < r) {
int md = (l + r + 1) >> 1;
node cur = t.get(md, q);
if (cur.mx - cur.mn > c[i]) {
l = md;
} else {
r = md - 1;
}
}
int suf = r;
node sf = t.get(suf, q);
if (sf.mx - sf.mn < c[i]) {
ans.pb(t.get(q, q).mx);
continue;
}
l = 0; r = q;
while (l < r) {
int md = (l + r + 1) >> 1;
node cur = t.get(md, q);
if (cur.mn <= sf.mn) {
l = md;
} else {
r = md - 1;
}
}
int pos_mn = r;
l = 0; r = q;
while (l < r) {
int md = (l + r + 1) >> 1;
node cur = t.get(md, q);
if (cur.mx >= sf.mx) {
l = md;
} else {
r = md - 1;
}
}
int pos_mx = r;
if (pos_mx >= pos_mn) {
ll val = t.get(q, q).mx;
ans.pb(val - sf.mx + c[i]);
} else {
ans.pb(t.get(q, q).mx - sf.mn);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2258 ms |
62068 KB |
Output is correct |
2 |
Correct |
2213 ms |
62036 KB |
Output is correct |
3 |
Correct |
2117 ms |
61868 KB |
Output is correct |
# |
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 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
129 ms |
44476 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |