//#include "candies.h"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define vi vector<int>
const ll inf = 0x3f3f3f3f3f3f3f3f;
struct segTree{
int l, r;
segTree *tl, *tr;
ll ss, mn, mx;
segTree(int l, int r) : l(l), r(r){
ss = mn = mx = 0;
if(l != r){
int mid = l + (r - l) / 2;
tl = new segTree(l, mid);
tr = new segTree(mid + 1, r);
}
}
void add(int x, ll y){
if(x < l || r < x) return;
if(l == r) return (void)(ss += y, mn += y, mx += y);
tl->add(x, y), tr->add(x, y);
ss = tl->ss + tr->ss;
mn = min(tl->mn + tr->ss, tr->mn);
mx = max(tl->mx + tr->ss, tr->mx);
}
ll qry(ll x){
if(l == r) return ss;
return tr->mx <= x ? min(tl->qry(x - tr->ss) + tr->ss, tr->mn) : tr->qry(x);
}
};
const int mxn = 200001;
int n, q;
vi b[mxn];
vi distribute_candies(vi a, vi l, vi r, vi v){
n = a.size(), q = l.size();
for(int i = 0; i < q; i++){
b[l[i]].push_back(i + 1);
b[r[i] + 1].push_back(-i - 1);
}
vi f(n);
segTree tre(-2, q - 1);
tre.add(-2, inf), tre.add(-1, -inf);
for(int i = 0; i < n; i++){
for(int j : b[i]) tre.add(abs(j) - 1, j / abs(j) * v[abs(j) - 1]);
int l = -1, r = a[i];
while(r - l > 1){
int mid = (l + r) / 2;
if(mid - tre.qry(mid) < a[i]) l = mid;
else r = mid;
}
f[i] = r;
}
return f;
}
int fufu(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vi a(n), l(q), r(q), v(q);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < q; i++) cin >> l[i] >> r[i] >> v[i];
vi f = distribute_candies(a, l, r, v);
cout << f[0];
for(int i = 1; i < n; i++) cout << " " << f[i];
cout << endl;
return 0;
}
Compilation message
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:34:17: warning: 'tre.segTree::tr' may be used uninitialized in this function [-Wmaybe-uninitialized]
34 | ss = tl->ss + tr->ss;
| ^~
candies.cpp:57:10: note: 'tre.segTree::tr' was declared here
57 | segTree tre(-2, q - 1);
| ^~~
candies.cpp:34:17: warning: 'tre.segTree::tl' may be used uninitialized in this function [-Wmaybe-uninitialized]
34 | ss = tl->ss + tr->ss;
| ^~
candies.cpp:57:10: note: 'tre.segTree::tl' was declared here
57 | segTree tre(-2, q - 1);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4988 KB |
Output is correct |
3 |
Correct |
5 ms |
5256 KB |
Output is correct |
4 |
Correct |
5 ms |
5324 KB |
Output is correct |
5 |
Correct |
6 ms |
5380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
828 ms |
42964 KB |
Output is correct |
2 |
Correct |
1018 ms |
46752 KB |
Output is correct |
3 |
Correct |
1053 ms |
46588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
346 ms |
39856 KB |
Output is correct |
3 |
Correct |
255 ms |
10976 KB |
Output is correct |
4 |
Correct |
858 ms |
48592 KB |
Output is correct |
5 |
Correct |
869 ms |
49092 KB |
Output is correct |
6 |
Correct |
745 ms |
49376 KB |
Output is correct |
7 |
Correct |
882 ms |
48836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
115 ms |
38852 KB |
Output is correct |
4 |
Correct |
154 ms |
8884 KB |
Output is correct |
5 |
Correct |
340 ms |
42184 KB |
Output is correct |
6 |
Correct |
341 ms |
42864 KB |
Output is correct |
7 |
Correct |
279 ms |
43448 KB |
Output is correct |
8 |
Correct |
338 ms |
42036 KB |
Output is correct |
9 |
Correct |
525 ms |
43508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4988 KB |
Output is correct |
3 |
Correct |
5 ms |
5256 KB |
Output is correct |
4 |
Correct |
5 ms |
5324 KB |
Output is correct |
5 |
Correct |
6 ms |
5380 KB |
Output is correct |
6 |
Correct |
828 ms |
42964 KB |
Output is correct |
7 |
Correct |
1018 ms |
46752 KB |
Output is correct |
8 |
Correct |
1053 ms |
46588 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
346 ms |
39856 KB |
Output is correct |
11 |
Correct |
255 ms |
10976 KB |
Output is correct |
12 |
Correct |
858 ms |
48592 KB |
Output is correct |
13 |
Correct |
869 ms |
49092 KB |
Output is correct |
14 |
Correct |
745 ms |
49376 KB |
Output is correct |
15 |
Correct |
882 ms |
48836 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
115 ms |
38852 KB |
Output is correct |
19 |
Correct |
154 ms |
8884 KB |
Output is correct |
20 |
Correct |
340 ms |
42184 KB |
Output is correct |
21 |
Correct |
341 ms |
42864 KB |
Output is correct |
22 |
Correct |
279 ms |
43448 KB |
Output is correct |
23 |
Correct |
338 ms |
42036 KB |
Output is correct |
24 |
Correct |
525 ms |
43508 KB |
Output is correct |
25 |
Correct |
3 ms |
4944 KB |
Output is correct |
26 |
Correct |
165 ms |
8968 KB |
Output is correct |
27 |
Correct |
341 ms |
39328 KB |
Output is correct |
28 |
Correct |
743 ms |
47292 KB |
Output is correct |
29 |
Correct |
701 ms |
47632 KB |
Output is correct |
30 |
Correct |
694 ms |
47816 KB |
Output is correct |
31 |
Correct |
670 ms |
48000 KB |
Output is correct |
32 |
Correct |
605 ms |
48140 KB |
Output is correct |