#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef tuple<ll, ll, ll> iii;
const int MAXN = 200005;
const ll inf = 1e18;
struct node{
int s, e, m; ll mx, mn, lazy;
node *l, *r;
node (int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1; mx = mn = lazy = 0;
if (s != e){
l = new node(s, m); r = new node(m + 1, e);
}
}
void push(ll v){
mx += v; mn += v;
lazy += v;
}
void propo(){
if (s == e) return;
if (lazy != 0){
l->push(lazy); r->push(lazy);
lazy = 0;
}
}
void update(int x, int y, ll v){
if (x == s && y == e){
push(v); return;
}
propo();
if (y <= m) l->update(x, y, v);
else if (x > m) r->update(x, y, v);
else{
l->update(x, m, v); r->update(m + 1, y, v);
}
mx = max(l->mx, r->mx); mn = min(l->mn, r->mn);
}
ll query(int x){
if (s == e) return mx;
propo();
if (x <= m) return l->query(x);
else return r->query(x);
}
iii find(ll omx, ll omn, ll range){
if (s == e) return {s, omx, omn};
propo();
ll nmx = max(omx, r->mx), nmn = min(omn, r->mn);
if (nmx - nmn <= range) return l->find(nmx, nmn, range);
else return r->find(omx, omn, range);
}
};
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v){
int nums = c.size(), queries = l.size();
vector<vector<int>> rsv(nums), rev(nums);
for (int q = 0; q < queries; q++){
rsv[l[q]].push_back(q);
rev[r[q]].push_back(q);
}
node *root = new node(-2, queries - 1);
root->update(-2, -2, inf);
vector<int> ans(nums);
for (int i = 0; i < nums; i++){
for (int q : rsv[i]) root->update(q, queries - 1, v[q]);
ll fid, mx, mn; tie(fid, mx, mn) = root->find(-inf, inf, c[i]);
ll fv = root->query(fid), ev = root->query(queries - 1);
if (fv > mx) ans[i] = ev - mn;
else ans[i] = c[i] - (mx - ev);
for (int q : rev[i]) root->update(q, queries - 1, -v[q]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
405 ms |
48968 KB |
Output is correct |
2 |
Correct |
383 ms |
53064 KB |
Output is correct |
3 |
Correct |
374 ms |
52812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
219 ms |
32488 KB |
Output is correct |
3 |
Correct |
51 ms |
14660 KB |
Output is correct |
4 |
Correct |
401 ms |
52552 KB |
Output is correct |
5 |
Correct |
405 ms |
48968 KB |
Output is correct |
6 |
Correct |
401 ms |
52600 KB |
Output is correct |
7 |
Correct |
413 ms |
55144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
120 ms |
31880 KB |
Output is correct |
4 |
Correct |
54 ms |
12480 KB |
Output is correct |
5 |
Correct |
191 ms |
43356 KB |
Output is correct |
6 |
Correct |
166 ms |
43356 KB |
Output is correct |
7 |
Correct |
165 ms |
43352 KB |
Output is correct |
8 |
Correct |
200 ms |
43444 KB |
Output is correct |
9 |
Correct |
172 ms |
43304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
405 ms |
48968 KB |
Output is correct |
7 |
Correct |
383 ms |
53064 KB |
Output is correct |
8 |
Correct |
374 ms |
52812 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
219 ms |
32488 KB |
Output is correct |
11 |
Correct |
51 ms |
14660 KB |
Output is correct |
12 |
Correct |
401 ms |
52552 KB |
Output is correct |
13 |
Correct |
405 ms |
48968 KB |
Output is correct |
14 |
Correct |
401 ms |
52600 KB |
Output is correct |
15 |
Correct |
413 ms |
55144 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
120 ms |
31880 KB |
Output is correct |
19 |
Correct |
54 ms |
12480 KB |
Output is correct |
20 |
Correct |
191 ms |
43356 KB |
Output is correct |
21 |
Correct |
166 ms |
43356 KB |
Output is correct |
22 |
Correct |
165 ms |
43352 KB |
Output is correct |
23 |
Correct |
200 ms |
43444 KB |
Output is correct |
24 |
Correct |
172 ms |
43304 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
50 ms |
13648 KB |
Output is correct |
27 |
Correct |
228 ms |
35144 KB |
Output is correct |
28 |
Correct |
427 ms |
53468 KB |
Output is correct |
29 |
Correct |
362 ms |
53832 KB |
Output is correct |
30 |
Correct |
380 ms |
54060 KB |
Output is correct |
31 |
Correct |
383 ms |
54344 KB |
Output is correct |
32 |
Correct |
386 ms |
54600 KB |
Output is correct |