#include "candies.h"
#include <vector>
#include <algorithm>
using namespace std;
// using ll = long long;
using ll = int;
const int MAXN = 200100;
vector<pair<int, int>> V;
struct datas {
ll mx, mi, set, p;
datas operator+(const datas &i) const {
datas re;
re.mx = max(mx, i.mx);
re.mi = min(mi, i.mi);
re.set = 0;
re.p = 0;
return re;
}
// data(ll a) : mx(a), mi(a), set(0), p(0);
}tree[MAXN << 2];
void update(int c, int l, int r) {
if(l == r)
return;
int m = (l + r) >> 1;
if(tree[c].set) {
tree[c << 1].p = 0;
tree[c << 1 | 1].p = 0;
if(tree[c].set == -1) {
tree[c << 1].set = tree[c].set;
tree[c << 1 | 1].set = tree[c].set;
tree[c << 1].mx = 0;
tree[c << 1 | 1].mx = 0;
tree[c << 1].mi = 0;
tree[c << 1 | 1].mi = 0;
} else {
tree[c << 1].set = tree[c].set;
tree[c << 1 | 1].set = tree[c].set;
tree[c << 1].mx = V[m].first;
tree[c << 1 | 1].mx = V[r].first;
tree[c << 1].mi = V[l].first;
tree[c << 1 | 1].mi = V[m + 1].first;
}
}
if(tree[c].p) {
tree[c << 1].p += tree[c].p;
tree[c << 1 | 1].p += tree[c].p;
tree[c << 1].mx += tree[c].p;
tree[c << 1 | 1].mx += tree[c].p;
tree[c << 1].mi += tree[c].p;
tree[c << 1 | 1].mi += tree[c].p;
}
tree[c].p = tree[c].set = 0;
}
void pluss(int l, int r, int tl, int tr, int c, ll x) {
update(c, tl, tr);
if(l <= tl && tr <= r) {
tree[c].mi += x;
tree[c].mx += x;
tree[c].p += x;
return;
}
int tm = (tl + tr) >> 1;
if(l <= tm)
pluss(l, r, tl, tm, c << 1, x);
if(tm + 1 <= r)
pluss(l, r, tm + 1, tr, c << 1 | 1, x);
tree[c] = tree[c << 1] + tree[c << 1 | 1];
}
void set(int l, int r, int tl, int tr, int c, ll x) {
update(c, tl, tr);
if(l <= tl && tr <= r) {
if(x == -1) {
tree[c].set = x;
tree[c].mi = 0;
tree[c].mx = 0;
tree[c].p = 0;
} else {
tree[c].set = x;
tree[c].mi = V[tl].first;
tree[c].mx = V[tr].first;
tree[c].p = 0;
}
return;
}
int tm = (tl + tr) >> 1;
if(l <= tm)
set(l, r, tl, tm, c << 1, x);
if(tm + 1 <= r)
set(l, r, tm + 1, tr, c << 1 | 1, x);
tree[c] = tree[c << 1] + tree[c << 1 | 1];
}
int search(int x, int l, int r, int c) {
update(c, l, r);
if(l == r)
return tree[c].mi;
int m = (l + r) >> 1;
if(x <= m)
return search(x, l, m, c << 1);
return search(x, m + 1, r, c << 1 | 1);
}
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(), q = l.size();
for(int i = 0; i < n; i++)
V.push_back(make_pair(c[i], i));
sort(V.begin(), V.end());
for(int i = 0; i < q; i++) {
if(v[i] > 0) {
int l = 0, r = n, m = -1;
while(l != r) {
m = (l + r) >> 1;
if(v[i] + search(m, 0, n - 1, 1) > V[m].first)
l = m + 1;
else
r = m;
}
if(l)
set(0, l - 1, 0, n - 1, 1, 1);
if(l != n)
pluss(l, n - 1, 0, n - 1, 1, v[i]);
} else {
int l = 0, r = n, m = -1;
while(l != r) {
m = (l + r) >> 1;
if(search(m, 0, n - 1, 1) + v[i] < 0)
l = m + 1;
else
r = m;
}
if(l)
set(0, l - 1, 0, n - 1, 1, -1);
if(l != n)
pluss(l, n - 1, 0, n - 1, 1, v[i]);
}
}
vector<int> ans(n);
for(int i = 0; i < n; i++) {
ans[V[i].second] = search(i, 0, n - 1, 1);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
662 ms |
18596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
381 ms |
6640 KB |
Output is correct |
4 |
Correct |
115 ms |
13700 KB |
Output is correct |
5 |
Correct |
974 ms |
19216 KB |
Output is correct |
6 |
Correct |
975 ms |
20184 KB |
Output is correct |
7 |
Correct |
942 ms |
20116 KB |
Output is correct |
8 |
Correct |
1014 ms |
19508 KB |
Output is correct |
9 |
Correct |
613 ms |
20044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |