#include<bits/stdc++.h>
#include "candies.h"
using namespace std;
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e18;
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
vector<int> s(n);
int q = sz(v);
int sub1 = (n<=2000&&q<=2000);
int sub2 = 1;
int sub3 = 1;
int sub4 = 1;
for (auto it : v){
if (it < 0) sub2 = 0;
}
for (int i=1;i<n;i++){
sub3 &= c[i]==c[i-1];
}
for (int i=0;i<q;i++){
sub4 &= l[i]==0&&r[i]==n-1;
}
if (sub1){
for (int i=0;i<q;i++){
for (int k=l[i];k<=r[i];k++){
s[k] += v[i];
s[k] = max(0,s[k]);
s[k] = min(s[k],c[k]);
}
}
}
else if (sub2){
vector<ll> prf(n+1,0);
for (int i=0;i<q;i++){
prf[l[i]]+=v[i];
prf[r[i]+1]-=v[i];
}
for (int i=1;i<=n;i++){
prf[i] += prf[i-1];
}
for (int i=0;i<n;i++){
prf[i] = min(prf[i],(ll)c[i]);
s[i] = prf[i];
}
}
else if (sub3){
}
else if (sub4){
vector<ll> a(q);
for (int i=0;i<q;i++){
a[i] = v[i];
if (i) a[i] += a[i-1];
}
vector<pair<ll,ll>> mn(q+1), mx(q+1);
mn[q] = {inf,-n}, mx[q] = {-inf,n};
for (int i=n-1;i>=0;i--){
mn[i] = min({a[i],-i},mn[i+1]);
mx[i] = max({a[i],i},mx[i+1]);
}
mx[q] = {0,n};
mn[q] = {0,n};
for (int i=0;i<n;i++){
int lo=-1,hi=q;// lo + 1
while (hi-lo > 1){
int mi = (lo+hi)/2;
if (mx[mi].F-mn[mi].F >= c[i]) hi = mi;
else lo = mi;
}
if (hi==n){
s[i] = a.back();
}
else {
if (mx[hi].S > mn[hi].S){ // the max after
int p = mx[hi].S+1;
s[i] = c[i] - (mx[p].F-mn[p].F);
}
else {
int p = mn[hi].S+1;
s[i] = mx[p].F-mn[p].F;
}
}
}
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
13652 KB |
Output is correct |
2 |
Correct |
79 ms |
12884 KB |
Output is correct |
3 |
Correct |
78 ms |
12756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Incorrect |
45 ms |
8008 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
216 KB |
Output is correct |
3 |
Incorrect |
48 ms |
15448 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
94 ms |
13652 KB |
Output is correct |
7 |
Correct |
79 ms |
12884 KB |
Output is correct |
8 |
Correct |
78 ms |
12756 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Incorrect |
45 ms |
8008 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |