#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
long long seg[800005], mx[800005], mn[800005];
vector<long long> vec[200005];
vector<int> ans;
void update(long long i, long long l, long long r, long long x, long long y)
{
if (l==r)
{
seg[i]=mx[i]=mn[i]=y;
return;
}
long long mid=(l+r)/2;
if (x<=mid)
update(i*2, l, mid, x, y);
else
update(i*2+1, mid+1, r, x, y);
seg[i]=seg[i*2]+seg[i*2+1];
mx[i]=max(mx[i*2], mx[i*2+1]);
mn[i]=min(mn[i*2], mn[i*2+1]);
}
long long query(long long i, long long l, long long r, long long x)
{
if (l==r)
return max(0LL, min(x, seg[i]));
long long mid=(l+r)/2, res;
if (mx[i*2+1]-mn[i*2+1]>=x)
return query(i*2+1, mid+1, r, x);
res=query(i*2, l, mid, x);
if (res+mx[i*2+1]>=x)
return x-(mx[i*2+1]-seg[i*2+1]);
if (res+mn[i*2+1]<=0)
return seg[i*2+1]-mn[i*2+1];
return res+seg[i*2+1];
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v)
{
long long n=c.size(), q=v.size();
for (long long i=0; i<q; i++)
{
vec[l[i]].push_back(i);
vec[r[i]+1].push_back(i);
}
for (long long i=0; i<n; i++)
{
for (long long j:vec[i])
{
if (l[j]==i)
update(1, 0, q-1, j, v[j]);
else
update(1, 0, q-1, j, 0);
}
ans.push_back(query(1, 0, q-1, c[i]));
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
345 ms |
32264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |