#include "candies.h"
#include<bits/stdc++.h>
#include <vector>
using namespace std;
const int N = 200005;
struct saint{
long long mx, mn;
};
long long lazy[4*N];
saint aint[4*N];
void push(int nod, int l, int r){
aint[nod].mx += lazy[nod];
aint[nod].mn += lazy[nod];
if(l != r){
lazy[2*nod] += lazy[nod];
lazy[2*nod + 1] += lazy[nod];
}
lazy[nod] = 0;
}
saint join(saint a, saint b){
saint res;
res.mx = max(a.mx, b.mx);
res.mn = min(a.mn, b.mn);
return res;
}
void aint_update(int nod, int l, int r, int ul, int ur, long long val){
push(nod, l, r);
if(r < ul || ur < l)
return;
if(ul <= l && r <= r){
lazy[nod] += val;
push(nod, l, r);
return;
}
int mid = (l + r)/2;
aint_update(2*nod, l, mid, ul, ur, val);
aint_update(2*nod + 1, mid + 1, r, ul, ur, val);
aint[nod] = join(aint[2*nod], aint[2*nod + 1]);
}
long long aint_query(int nod, int l, int r, int pz){
push(nod, l, r);
if(pz < l || pz > r)
return LLONG_MIN;
if(l == r)
return aint[nod].mx;
int mid = (l + r)/2;
return max(aint_query(2*nod, l, mid, pz), aint_query(2*nod + 1, mid + 1, r, pz));
}
int aint_cautb(int nod, int l, int r, long long c, long long &mxy, long long &mni){
push(nod, l, r);
long long cmx = mxy, cmn = mni;
cmx = max(cmx, aint[nod].mx);
cmn = min(cmn, aint[nod].mn);
if(cmx - cmn <= c){
mxy = cmx;
mni = cmn;
return l;
}
if(l == r){
return r + 1;
}
int mid = (l + r)/2;
int poz = aint_cautb(2*nod + 1, mid + 1, r, c, mxy, mni);
if(poz > mid + 1)
return poz;
return aint_cautb(2*nod, l, mid, c, mxy, mni);
}
struct update{
int id;
long long val;
};
vector<update> upd[N];
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = c.size();
std::vector<int> s(n);
int rmax = r.size();
for(int i = 0; i < r.size();i ++){
upd[l[i]].push_back({i + 1, v[i]});
upd[r[i] + 1].push_back({i + 1, -v[i]});
}
for(int i = 0; i < n; i++){
for(auto x:upd[i]){
aint_update(1, 0, rmax, x.id, rmax, x.val);
}
long long mxc = LLONG_MIN, mnc = LLONG_MAX;
int pzm = aint_cautb(1, 0, rmax, c[i], mxc, mnc);
long long last_val = aint_query(1, 0, rmax, rmax);
if(pzm == 0){
s[i] = last_val - mnc;
}
else{
long long val = aint_query(1, 0, rmax, pzm - 1);
if(val > mxc){
s[i] = last_val - mnc;
}
else if(val < mnc){
s[i] = last_val - (mxc - c[i]);
}
else{
assert(0);
}
}
}
return s;
}
Compilation message
candies.cpp: In function 'void aint_update(int, int, int, int, int, long long int)':
candies.cpp:30:19: warning: self-comparison always evaluates to true [-Wtautological-compare]
30 | if(ul <= l && r <= r){
| ~ ^~ ~
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i < r.size();i ++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
4 ms |
5276 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
490 ms |
34824 KB |
Output is correct |
2 |
Correct |
475 ms |
34768 KB |
Output is correct |
3 |
Correct |
477 ms |
34952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
218 ms |
33912 KB |
Output is correct |
3 |
Correct |
123 ms |
10944 KB |
Output is correct |
4 |
Correct |
483 ms |
40764 KB |
Output is correct |
5 |
Correct |
471 ms |
41168 KB |
Output is correct |
6 |
Correct |
480 ms |
41560 KB |
Output is correct |
7 |
Correct |
470 ms |
40896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
113 ms |
28360 KB |
Output is correct |
4 |
Correct |
97 ms |
8904 KB |
Output is correct |
5 |
Correct |
254 ms |
32540 KB |
Output is correct |
6 |
Correct |
281 ms |
32476 KB |
Output is correct |
7 |
Correct |
288 ms |
32444 KB |
Output is correct |
8 |
Correct |
303 ms |
32540 KB |
Output is correct |
9 |
Correct |
265 ms |
32532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
4 ms |
5276 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5324 KB |
Output is correct |
6 |
Correct |
490 ms |
34824 KB |
Output is correct |
7 |
Correct |
475 ms |
34768 KB |
Output is correct |
8 |
Correct |
477 ms |
34952 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
218 ms |
33912 KB |
Output is correct |
11 |
Correct |
123 ms |
10944 KB |
Output is correct |
12 |
Correct |
483 ms |
40764 KB |
Output is correct |
13 |
Correct |
471 ms |
41168 KB |
Output is correct |
14 |
Correct |
480 ms |
41560 KB |
Output is correct |
15 |
Correct |
470 ms |
40896 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
113 ms |
28360 KB |
Output is correct |
19 |
Correct |
97 ms |
8904 KB |
Output is correct |
20 |
Correct |
254 ms |
32540 KB |
Output is correct |
21 |
Correct |
281 ms |
32476 KB |
Output is correct |
22 |
Correct |
288 ms |
32444 KB |
Output is correct |
23 |
Correct |
303 ms |
32540 KB |
Output is correct |
24 |
Correct |
265 ms |
32532 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
92 ms |
8780 KB |
Output is correct |
27 |
Correct |
246 ms |
33464 KB |
Output is correct |
28 |
Correct |
446 ms |
39412 KB |
Output is correct |
29 |
Correct |
479 ms |
39780 KB |
Output is correct |
30 |
Correct |
472 ms |
40068 KB |
Output is correct |
31 |
Correct |
458 ms |
40124 KB |
Output is correct |
32 |
Correct |
449 ms |
40364 KB |
Output is correct |