#include "candies.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define pb push_back
#define ln '\n'
using i64 = long long;
const int N = 2e5 + 5;
int C;
struct SegTree{
struct Info{
i64 mn, mx, s;
Info(i64 k) : mn(min(0LL, k)), mx(max(0LL, k)), s(k) {}
Info() : mn(0), mx(0), s(0) {}
Info(const Info &a, const Info &b){
s = a.s + b.s;
mn = min(a.mn, a.s + b.mn);
mx = max(a.mx, a.s + b.mx);
}
i64 eval(){
if ( mx - mn <= C || mn < -C ){
return s - mn;
}
return C + s - mx;
}
};
vector <Info> T;
int n;
SegTree(int n) : n(n) {
T.resize(n * 4 + 5);
}
void upd(int v, int l, int r, int p, i64 x){
if ( l == r ){
T[v] = Info(T[v].s + x);
return;
}
int m = (l + r) / 2;
if ( p <= m ) upd(v * 2, l, m, p, x);
else upd(v * 2 + 1, m + 1, r, p, x);
T[v] = Info(T[v * 2], T[v * 2 + 1]);
}
void upd(int p, i64 x){ upd(1, 0, n, p, x); }
i64 qry(int v, int l, int r, Info p){
if ( l == r ){
return Info(T[v], p).eval();
}
int m = (l + r) / 2;
Info nxt = Info(T[v * 2 + 1], p);
if ( nxt.mx - nxt.mn <= C ){
return qry(v * 2, l, m, nxt);
}
return qry(v * 2 + 1, m + 1, r, p);
}
i64 qry(int c){ C = c;
return qry(1, 0, n, Info());
}
};
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int q = l.size(), n = c.size();
vector <vector<ar<int,2>>> inc(n + 1);
for ( int i = 0; i < q; i++ ){
inc[l[i]].pb({i, v[i]});
inc[r[i] + 1].pb({i, -v[i]});
}
SegTree seg(q);
vector <int> ans(n);
for ( int i = 0; i < n; i++ ){
for ( auto &[j, v]: inc[i] ){
seg.upd(j + 1, v);
}
ans[i] = seg.qry(c[i]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
37868 KB |
Output is correct |
2 |
Correct |
149 ms |
37972 KB |
Output is correct |
3 |
Correct |
159 ms |
37968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
76 ms |
28400 KB |
Output is correct |
3 |
Correct |
39 ms |
7768 KB |
Output is correct |
4 |
Correct |
150 ms |
37924 KB |
Output is correct |
5 |
Correct |
155 ms |
44120 KB |
Output is correct |
6 |
Correct |
156 ms |
44632 KB |
Output is correct |
7 |
Correct |
144 ms |
43832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
45 ms |
27216 KB |
Output is correct |
4 |
Correct |
32 ms |
7772 KB |
Output is correct |
5 |
Correct |
86 ms |
33972 KB |
Output is correct |
6 |
Correct |
85 ms |
38224 KB |
Output is correct |
7 |
Correct |
85 ms |
38836 KB |
Output is correct |
8 |
Correct |
82 ms |
37456 KB |
Output is correct |
9 |
Correct |
83 ms |
39860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
158 ms |
37868 KB |
Output is correct |
7 |
Correct |
149 ms |
37972 KB |
Output is correct |
8 |
Correct |
159 ms |
37968 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
76 ms |
28400 KB |
Output is correct |
11 |
Correct |
39 ms |
7768 KB |
Output is correct |
12 |
Correct |
150 ms |
37924 KB |
Output is correct |
13 |
Correct |
155 ms |
44120 KB |
Output is correct |
14 |
Correct |
156 ms |
44632 KB |
Output is correct |
15 |
Correct |
144 ms |
43832 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
45 ms |
27216 KB |
Output is correct |
19 |
Correct |
32 ms |
7772 KB |
Output is correct |
20 |
Correct |
86 ms |
33972 KB |
Output is correct |
21 |
Correct |
85 ms |
38224 KB |
Output is correct |
22 |
Correct |
85 ms |
38836 KB |
Output is correct |
23 |
Correct |
82 ms |
37456 KB |
Output is correct |
24 |
Correct |
83 ms |
39860 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
33 ms |
9068 KB |
Output is correct |
27 |
Correct |
77 ms |
31068 KB |
Output is correct |
28 |
Correct |
158 ms |
42788 KB |
Output is correct |
29 |
Correct |
181 ms |
42836 KB |
Output is correct |
30 |
Correct |
150 ms |
43092 KB |
Output is correct |
31 |
Correct |
189 ms |
43088 KB |
Output is correct |
32 |
Correct |
147 ms |
43344 KB |
Output is correct |