#include "candies.h"
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 2e5 + 10;
const ll inf = 1e18;
int n, q, c[maxn];
vector<pii> Q[maxn];
pll seg[maxn << 2];
ll lazy[maxn << 2];
pll operator +(pll a, pll b){
return MP(min(a.F, b.F), max(a.S, b.S));
}
void shift(int v, int l, int r);
void add(int v, int l, int r, int ql, int qr, ll val){
if (qr <= l || r <= ql) return;
if (ql <= l && r <= qr){
seg[v].F += val;
seg[v].S += val;
lazy[v] += val;
return;
}
shift(v, l, r);
int mid = (l + r) / 2;
add(v << 1, l, mid, ql, qr, val);
add(v << 1 | 1, mid, r, ql, qr, val);
seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
int find(int v, int l, int r, int val, pll &suf){
// debug(v, l, r, val, seg[v].S, suf.S, seg[v].F, suf.F, max(seg[v].S, suf.S), min(seg[v].F, suf.F));
if (max(seg[v].S, suf.S) - min(seg[v].F, suf.F) < val){
suf = suf + seg[v];
return -3;
}
if (l + 1 == r){
suf = suf + seg[v];
return l;
}
shift(v, l, r);
int mid = (l + r) / 2;
int res = find(v << 1 | 1, mid, r, val, suf);
if (res == -3) res = find(v << 1, l, mid, val, suf);
return res;
}
pii find2(int v, int l, int r, int ql, int qr, pll val){
if (qr <= l || r <= ql) return {-3, 0};
if (ql <= l && r <= qr && seg[v].F > val.F && seg[v].S < val.S) return {-3, 0};
if (l + 1 == r){
pii ret = {l, 0};
if (seg[v].S == val.S) ret.S = 1;
return ret;
}
shift(v, l, r);
int mid = (l + r) / 2;
pii res = find2(v << 1, l, mid, ql, qr, val);
if (res.F == -3) res = find2(v << 1 | 1, mid, r, ql, qr, val);
return res;
}
int get(int v, int l, int r, int idx){
if (l + 1 == r) return seg[v].F;
shift(v, l, r);
int mid = (l + r) / 2;
if (idx < mid) return get(v << 1, l, mid, idx);
return get(v << 1 | 1, mid, r, idx);
}
void shift(int v, int l, int r){
if (!lazy[v]) return;
int mid = (l + r) / 2;
add(v << 1, l, mid, l, mid, lazy[v]);
add(v << 1 | 1, mid, r, mid, r, lazy[v]);
lazy[v] = 0;
}
vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V){
n = C.size();
q = L.size();
for (int i = 0; i < n; i++){
c[i] = C[i];
}
for (int i = 0; i < q; i++){
Q[L[i]].push_back({i, V[i]});
Q[R[i]+1].push_back({i, -V[i]});
}
add(1, -2, q, -2, -1, inf);
vector<int> ans(n);
for (int i = 0; i < n; i++){
for (auto [x, y]: Q[i]){
//debug(x, y);
add(1, -2, q, x, q, y);
}
pll tmp = {inf, -inf};
int idx = find(1, -2, q, c[i], tmp);
//debug(i, idx, tmp.F, tmp.S);
pii idx2 = find2(1, -2, q, idx+1, q, tmp);
//debug(idx2.F, idx2.S);
ans[i] = get(1, -2, q, q) - get(1, -2, q, idx2.F);
//debug(ans[i]);
if (idx2.S == 1) ans[i] += c[i];
//debug(ans[i]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
5 ms |
5204 KB |
Output is correct |
5 |
Correct |
5 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
470 ms |
32188 KB |
Output is correct |
2 |
Correct |
448 ms |
32076 KB |
Output is correct |
3 |
Correct |
462 ms |
32184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
224 ms |
26568 KB |
Output is correct |
3 |
Correct |
103 ms |
9644 KB |
Output is correct |
4 |
Correct |
447 ms |
32148 KB |
Output is correct |
5 |
Correct |
525 ms |
32180 KB |
Output is correct |
6 |
Correct |
424 ms |
32184 KB |
Output is correct |
7 |
Correct |
416 ms |
32184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
91 ms |
25304 KB |
Output is correct |
4 |
Correct |
91 ms |
8456 KB |
Output is correct |
5 |
Correct |
222 ms |
28340 KB |
Output is correct |
6 |
Correct |
222 ms |
28284 KB |
Output is correct |
7 |
Correct |
239 ms |
28300 KB |
Output is correct |
8 |
Correct |
231 ms |
28304 KB |
Output is correct |
9 |
Correct |
264 ms |
28300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
5 ms |
5204 KB |
Output is correct |
5 |
Correct |
5 ms |
5204 KB |
Output is correct |
6 |
Correct |
470 ms |
32188 KB |
Output is correct |
7 |
Correct |
448 ms |
32076 KB |
Output is correct |
8 |
Correct |
462 ms |
32184 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
224 ms |
26568 KB |
Output is correct |
11 |
Correct |
103 ms |
9644 KB |
Output is correct |
12 |
Correct |
447 ms |
32148 KB |
Output is correct |
13 |
Correct |
525 ms |
32180 KB |
Output is correct |
14 |
Correct |
424 ms |
32184 KB |
Output is correct |
15 |
Correct |
416 ms |
32184 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
91 ms |
25304 KB |
Output is correct |
19 |
Correct |
91 ms |
8456 KB |
Output is correct |
20 |
Correct |
222 ms |
28340 KB |
Output is correct |
21 |
Correct |
222 ms |
28284 KB |
Output is correct |
22 |
Correct |
239 ms |
28300 KB |
Output is correct |
23 |
Correct |
231 ms |
28304 KB |
Output is correct |
24 |
Correct |
264 ms |
28300 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
108 ms |
9616 KB |
Output is correct |
27 |
Correct |
218 ms |
29108 KB |
Output is correct |
28 |
Correct |
519 ms |
36748 KB |
Output is correct |
29 |
Correct |
516 ms |
37132 KB |
Output is correct |
30 |
Correct |
500 ms |
37304 KB |
Output is correct |
31 |
Correct |
433 ms |
37520 KB |
Output is correct |
32 |
Correct |
429 ms |
37704 KB |
Output is correct |