#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
struct segment_tree{
long long vmax[800008], vmin[800008], lazy[800008], m;
int n;
void init(int N){
n = N;
m = 0LL;
for(int i = 0; i < 800008; i++){
vmax[i] = 0LL;
vmin[i] = 0LL;
lazy[i] = 0LL;
}
}
void lazy_update(int N){
lazy[2*N] += lazy[N];
lazy[2*N+1] += lazy[N];
vmax[2*N] += lazy[N];
vmin[2*N] += lazy[N];
vmax[2*N+1] += lazy[N];
vmin[2*N+1] += lazy[N];
lazy[N] = 0;
}
long long get_max(int N, int l, int r, int s, int t){
if(l > t || s > r)return -9000000000000000000LL;
if(l <= s && t <= r)return vmax[N];
lazy_update(N);
return max(get_max(2*N, l, r, s, (s+t)/2), get_max(2*N+1, l, r, (s+t)/2+1, t));
}
long long get_min(int N, int l, int r, int s, int t){
if(l > t || s > r)return 9000000000000000000LL;
if(l <= s && t <= r)return vmin[N];
lazy_update(N);
return min(get_min(2*N, l, r, s, (s+t)/2), get_min(2*N+1, l, r, (s+t)/2+1, t));
}
void update(int N, long long M, int l, int r, int s, int t){
if(s == 1 && t == n)m += M;
if(l > t || s > r)return;
if(l <= s && t <= r){
vmax[N] += M;
vmin[N] += M;
lazy[N] += M;
return;
}
lazy_update(N);
update(2*N, M, l, r, s, (s+t)/2);
update(2*N+1, M, l, r, (s+t)/2+1, t);
vmax[N] = max(vmax[2*N], vmax[2*N+1]);
vmin[N] = min(vmin[2*N], vmin[2*N+1]);
}
pair<long long, long long> find_index(int N, long long M, int l, int r, long long s, long long t){
if(l == r)return make_pair(vmax[N], max(s, vmax[N])+min(t, vmin[N])-vmin[N]);
lazy_update(N);
if(max(s, vmax[2*N+1])-min(t, vmin[2*N+1])<M)return find_index(2*N, M, l, (l+r)/2, max(s, vmax[2*N+1]), min(t, vmin[2*N+1]));
return find_index(2*N+1, M, (l+r)/2+1, r, s, t);
}
} A;
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size(), q = v.size();
A.init(q+1);
vector<vector<int>> x(n), y(n);
for(int i = 0; i < q; i++){
x[l[i]].push_back(i);
if(r[i]<n-1)y[r[i]+1].push_back(i);
}
vector<int> s(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < x[i].size(); j++)A.update(1, v[x[i][j]], x[i][j]+2, q+1, 1, q+1);
for(int j = 0; j < y[i].size(); j++)A.update(1, -v[y[i][j]], y[i][j]+2, q+1, 1, q+1);
//for(int j = 1; j < 6; j++)cout << A.vmax[j] << " " << A.vmin[j] << " " << A.lazy[j] << "\n";
pair<int, int> t = A.find_index(1, c[i], 1, q+1, -9000000000000000000LL, 9000000000000000000LL);
if(abs(t.first-t.second) >= c[i])s[i] = t.first > t.second ? A.m - t.second : A.m - t.second + c[i];
else s[i] = max(0LL, A.m - A.get_min(1, 1, q+1, 1, q+1));
}
return s;
}
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:72:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int j = 0; j < x[i].size(); j++)A.update(1, v[x[i][j]], x[i][j]+2, q+1, 1, q+1);
| ~~^~~~~~~~~~~~~
candies.cpp:73:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int j = 0; j < y[i].size(); j++)A.update(1, -v[y[i][j]], y[i][j]+2, q+1, 1, q+1);
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
6 ms |
19336 KB |
Output is correct |
4 |
Correct |
5 ms |
19292 KB |
Output is correct |
5 |
Correct |
5 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
372 ms |
42836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19032 KB |
Output is correct |
2 |
Incorrect |
209 ms |
26864 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
124 ms |
25196 KB |
Output is correct |
4 |
Correct |
51 ms |
30812 KB |
Output is correct |
5 |
Correct |
142 ms |
36588 KB |
Output is correct |
6 |
Correct |
156 ms |
36588 KB |
Output is correct |
7 |
Correct |
155 ms |
36588 KB |
Output is correct |
8 |
Correct |
140 ms |
36452 KB |
Output is correct |
9 |
Correct |
141 ms |
36472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
6 ms |
19336 KB |
Output is correct |
4 |
Correct |
5 ms |
19292 KB |
Output is correct |
5 |
Correct |
5 ms |
19292 KB |
Output is correct |
6 |
Incorrect |
372 ms |
42836 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |