#include "candies.h"
#include <bits/stdc++.h>
#include <vector>
#define ll long long
using namespace std;
vector <ll> tree;
ll sz=1, ans;
bool ok=0;
void init(ll n){
while (sz < n) sz*=2;
tree.assign(2*sz, 0);
return ;
}
void add(ll l, ll r, ll v, ll x, ll lx, ll rx){
if (lx >= r || rx <= l) return ;
if (l <= lx && rx <= r) {
tree[x]+=v;
return ;
}
ll m=(lx+rx)/2;
add(l, r, v, 2*x+1, lx, m);
add(l, r, v, 2*x+2, m, rx);
return ;
}
void help(ll i, ll x, ll lx, ll rx, ll res=0){
if (rx-lx == 1){
if (!ok) ans=res+tree[x];
ok=1;
return;
}
ll m=(lx+rx)/2;
ll x1=x;
if (i < m) help(i, 2*x+1, lx, m, res+tree[x1]);
else help(i, 2*x+2, m, rx, res+tree[x1]);
return ;
}
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();
init(n);
vector<int> s(n);
if (n <= 2000){
for (int i=0;i<l.size();i++){
// cout << v[i] <<"\n";
for (int j=l[i];j<=r[i];j++) s[j]=min(c[j], max(0, s[j]+v[i]));
}
return s;
}
for (int i=0;i<l.size();i++){
add(l[i], r[i]+1, v[i], 0, 0, sz);
}
for (int i=0;i<n;i++){
ok=0;
ans=0;
help(i, 0, 0, sz);
if (ans > c[i]) s[i]=c[i];
else s[i]=ans;
}
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:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i=0;i<l.size();i++){
| ~^~~~~~~~~
candies.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i=0;i<l.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
8 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
11368 KB |
Output is correct |
2 |
Correct |
187 ms |
11464 KB |
Output is correct |
3 |
Correct |
186 ms |
11464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
200 ms |
5060 KB |
Output is correct |
3 |
Incorrect |
79 ms |
8108 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1682 ms |
5060 KB |
Output is correct |
4 |
Incorrect |
56 ms |
7720 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
8 ms |
340 KB |
Output is correct |
6 |
Correct |
211 ms |
11368 KB |
Output is correct |
7 |
Correct |
187 ms |
11464 KB |
Output is correct |
8 |
Correct |
186 ms |
11464 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
200 ms |
5060 KB |
Output is correct |
11 |
Incorrect |
79 ms |
8108 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |