#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T>
int len(T &a){
return a.size();
}
const ll infl = 1e8;
struct Segtree{
struct node{
ll mx = 0, mn = 0, add = 0;
void apply(ll x){
mn += x; mx += x;
add += x;
}
};
vector<node> t;
int n;
Segtree(int _n){
n = _n;
t.resize(4 * n);
}
void push(int x){
if(t[x].add){
t[x << 1].apply(t[x].add);
t[x << 1 | 1].apply(t[x].add);
t[x].add = 0;
}
}
node merge(node a, node b){
return {max(a.mx, b.mx), min(a.mn, b.mn), 0ll};
}
void upd(int x, int lx, int rx, int l, int r, ll val){
if(l <= lx && rx <= r){
t[x].apply(val);
return;
}
if(r < lx || l > rx){
return;
}
int m = (rx + lx) >> 1;
push(x);
upd(x << 1, lx, m, l, r, val);
upd(x << 1 | 1, m + 1, rx, l, r, val);
t[x] = merge(t[x << 1], t[x << 1 | 1]);
}
node get(int x, int lx, int rx, int l, int r){
if(l <= lx && rx <= r){
return t[x];
}
if(r < lx || l > rx){
return {0,0,0};
}
push(x);
int m = (lx + rx) >> 1;
return merge(get(x << 1, lx, m, l, r),
get(x << 1 | 1, m + 1, rx, l, r));
}
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 = c.size();
int m = len(v);
Segtree t(m + 1);
vector<vector<int>> in(n), out(n);
for(int i = 0; i < m; i ++){
in[l[i]].push_back(i);
out[r[i]].push_back(i);
}
for(int i = 0; i < n; i ++){
for(auto u : in[i]){
t.upd(0, u, -v[u]);
}
int l = 0, r = m;
while(l <= r){
int mid = (l + r) >> 1;
auto res = t.get(mid, m);
if(res.mx - res.mn > c[i]){
l = mid + 1;
}else r = mid - 1;
}
r ++;
if(r == 0 || v[r - 1] < 0){
c[i] = -t.get(r, m).mn;
}else{
c[i] = c[i] - t.get(r, m).mx;
}
for(auto u : out[i]){
t.upd(0, u, v[u]);
}
}
//for(auto u : suf) cout << u << ' ';cout << endl;
return c;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1141 ms |
41940 KB |
Output is correct |
2 |
Correct |
1169 ms |
41940 KB |
Output is correct |
3 |
Correct |
1243 ms |
41936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
223 ms |
26764 KB |
Output is correct |
3 |
Correct |
318 ms |
11616 KB |
Output is correct |
4 |
Correct |
1173 ms |
41928 KB |
Output is correct |
5 |
Correct |
1151 ms |
41936 KB |
Output is correct |
6 |
Correct |
1155 ms |
41868 KB |
Output is correct |
7 |
Correct |
1082 ms |
41936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
156 ms |
25656 KB |
Output is correct |
4 |
Correct |
288 ms |
11512 KB |
Output is correct |
5 |
Correct |
870 ms |
36544 KB |
Output is correct |
6 |
Correct |
951 ms |
36544 KB |
Output is correct |
7 |
Correct |
905 ms |
36548 KB |
Output is correct |
8 |
Correct |
877 ms |
36664 KB |
Output is correct |
9 |
Correct |
932 ms |
36552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
1141 ms |
41940 KB |
Output is correct |
7 |
Correct |
1169 ms |
41940 KB |
Output is correct |
8 |
Correct |
1243 ms |
41936 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
223 ms |
26764 KB |
Output is correct |
11 |
Correct |
318 ms |
11616 KB |
Output is correct |
12 |
Correct |
1173 ms |
41928 KB |
Output is correct |
13 |
Correct |
1151 ms |
41936 KB |
Output is correct |
14 |
Correct |
1155 ms |
41868 KB |
Output is correct |
15 |
Correct |
1082 ms |
41936 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
156 ms |
25656 KB |
Output is correct |
19 |
Correct |
288 ms |
11512 KB |
Output is correct |
20 |
Correct |
870 ms |
36544 KB |
Output is correct |
21 |
Correct |
951 ms |
36544 KB |
Output is correct |
22 |
Correct |
905 ms |
36548 KB |
Output is correct |
23 |
Correct |
877 ms |
36664 KB |
Output is correct |
24 |
Correct |
932 ms |
36552 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
286 ms |
11616 KB |
Output is correct |
27 |
Correct |
229 ms |
26844 KB |
Output is correct |
28 |
Correct |
1100 ms |
41828 KB |
Output is correct |
29 |
Correct |
1170 ms |
41936 KB |
Output is correct |
30 |
Correct |
1137 ms |
41836 KB |
Output is correct |
31 |
Correct |
1152 ms |
41936 KB |
Output is correct |
32 |
Correct |
1113 ms |
41968 KB |
Output is correct |