#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
struct segment_tree{
long long vmax[600006], vmin[600006], lazy[600006], m;
int n;
void init(int N){
n = N;
m = 0LL;
for(int i = 0; i < 600006; 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];
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];
return max(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, int 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<int, int> find_index(int N, int 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 - t.second);
}
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:70:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | 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:71:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | 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);
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Incorrect |
4 ms |
14428 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
310 ms |
38144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Incorrect |
4 ms |
14428 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |