#include<bits/stdc++.h>
#include "candies.h"
using namespace std;
const long long INF = 1e18;
class Tree{
private:
int lrSize = 2;
vector<long long> maxVal;
vector<long long> minVal;
vector<long long> lazy;
void update(int pos, bool up){
minVal[pos] = min(minVal[2*pos], minVal[2*pos+1]);
maxVal[pos] = max(maxVal[2*pos], maxVal[2*pos+1]);
if(pos > 1 && up)update(pos/2, true);
}
void push(int pos){
minVal[2*pos] += lazy[pos];
maxVal[2*pos] += lazy[pos];
lazy[2*pos] += lazy[pos];
minVal[2*pos + 1] += lazy[pos];
maxVal[2*pos + 1] += lazy[pos];
lazy[2*pos + 1] += lazy[pos];
lazy[pos] = 0;
}
void rangeAdd(int a, int b, int l, int r, int pos, long long val){
if(b < l || r < a)return;
if(a <= l && r <= b){
minVal[pos] += val; maxVal[pos] += val;
lazy[pos] += val;
}else{
int m = (l + r) / 2;
push(pos);
rangeAdd(a, b, l, m, 2*pos, val);
rangeAdd(a, b, m+1, r, 2*pos + 1, val);
update(pos, false);
}
}
public:
Tree(int n){
while(lrSize < n)lrSize *= 2;
maxVal.resize(2*lrSize, 0);
minVal.resize(2*lrSize, 0);
lazy.resize(2*lrSize, 0);
}
void add(int a, long long val){
rangeAdd(a, lrSize-1, 0, lrSize-1, 1, val);
}
long long getAns(long long c){
long long maxV = -INF;
long long minV = INF;
int pos = 1;
while(pos < lrSize){
push(pos);
if(max(maxVal[2*pos + 1], maxV) - min(minVal[2*pos + 1], minV) >= c){
pos = 2*pos + 1;
}else{
maxV = max(maxV, maxVal[2*pos + 1]);
minV = min(minV, minVal[2*pos + 1]);
pos = 2 * pos;
}
}
long long val1 = minVal[pos];
pos = 1;
while(pos < lrSize){
push(pos);
pos = 2*pos + 1;
}
long long val2 = minVal[pos];
//cout<<"Debug info "<<val1<<" "<<val2<<"\n";
if(val1 < minV){
return c - (maxV - val2);
}else{
return (val2 - minV);
}
}
};
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
int q = l.size();
vector<int> s(n);
vector<vector<int>> add(n);
vector<vector<int>> remove(n);
for(int i = 0; i < q; i++){
add[l[i]].push_back(i);
remove[r[i]].push_back(i);
}
Tree tree(q + 2);
tree.add(0, INF);
tree.add(1, -INF);
for(int i = 0; i < n; i++){
for(auto x : add[i]){
tree.add(x + 2, v[x]);
}
s[i] = tree.getAns(c[i]);
for(auto x : remove[i]){
tree.add(x+2, -v[x]);
}
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
594 ms |
41132 KB |
Output is correct |
2 |
Correct |
593 ms |
40364 KB |
Output is correct |
3 |
Correct |
630 ms |
40276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
249 ms |
23240 KB |
Output is correct |
3 |
Correct |
85 ms |
14412 KB |
Output is correct |
4 |
Correct |
588 ms |
42232 KB |
Output is correct |
5 |
Correct |
586 ms |
42676 KB |
Output is correct |
6 |
Correct |
598 ms |
42948 KB |
Output is correct |
7 |
Correct |
571 ms |
42288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
177 ms |
21816 KB |
Output is correct |
4 |
Correct |
81 ms |
13412 KB |
Output is correct |
5 |
Correct |
441 ms |
34488 KB |
Output is correct |
6 |
Correct |
443 ms |
34972 KB |
Output is correct |
7 |
Correct |
427 ms |
35668 KB |
Output is correct |
8 |
Correct |
444 ms |
34328 KB |
Output is correct |
9 |
Correct |
476 ms |
35740 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 |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
594 ms |
41132 KB |
Output is correct |
7 |
Correct |
593 ms |
40364 KB |
Output is correct |
8 |
Correct |
630 ms |
40276 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
249 ms |
23240 KB |
Output is correct |
11 |
Correct |
85 ms |
14412 KB |
Output is correct |
12 |
Correct |
588 ms |
42232 KB |
Output is correct |
13 |
Correct |
586 ms |
42676 KB |
Output is correct |
14 |
Correct |
598 ms |
42948 KB |
Output is correct |
15 |
Correct |
571 ms |
42288 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
177 ms |
21816 KB |
Output is correct |
19 |
Correct |
81 ms |
13412 KB |
Output is correct |
20 |
Correct |
441 ms |
34488 KB |
Output is correct |
21 |
Correct |
443 ms |
34972 KB |
Output is correct |
22 |
Correct |
427 ms |
35668 KB |
Output is correct |
23 |
Correct |
444 ms |
34328 KB |
Output is correct |
24 |
Correct |
476 ms |
35740 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
73 ms |
13516 KB |
Output is correct |
27 |
Correct |
242 ms |
22920 KB |
Output is correct |
28 |
Correct |
598 ms |
40796 KB |
Output is correct |
29 |
Correct |
580 ms |
41132 KB |
Output is correct |
30 |
Correct |
589 ms |
41384 KB |
Output is correct |
31 |
Correct |
593 ms |
41648 KB |
Output is correct |
32 |
Correct |
572 ms |
41800 KB |
Output is correct |