//#pragma GCC optimize ("O3")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops,O3")
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cerr << "(" << (#x) << "): " << (x) << endl;
#define all(x) x.begin(), x.end()
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
const int ss = 2000;
const int maxn5 = 2e5 + 10;
int ret[maxn5], c[maxn5], sig[maxn5];
int L[maxn5], R[maxn5], V[maxn5];
std::vector<int> distribute_candies(std::vector<int> C, std::vector<int> LL,
std::vector<int> RR, std::vector<int> VV) {
int n = C.size(), q = LL.size();
for(int i = 0; i < n; i++)
c[i] = C[i];
for(int i = 0; i < q; i++){
L[i] = LL[i];
R[i] = RR[i];
V[i] = VV[i];
sig[i] = (V[i] > 0 ? 1 : -1);
}
for(int tt = 0; tt < n; tt += ss){
for(int i = 1; i < q; i += 2){
int lim = MIN(tt + ss, n);
if(MAX(L[i], L[i - 1] > tt || MIN(R[i], R[i - 1]) < lim - 1)){
int v1 = V[i - 1], v2 = V[i];
int l = MAX(tt, L[i - 1]), r = MIN(lim, R[i - 1] + 1);
for(int j = l; j < r; j++){
ret[j] += v1;
ret[j] = (ret[j] < 0 ? 0 : (ret[j] > c[j] ? c[j] : ret[j]));
}
l = MAX(tt, L[i]);
r = MIN(lim, R[i] + 1);
for(int j = l; j < r; j++){
ret[j] += v2;
ret[j] = (ret[j] < 0 ? 0 : (ret[j] > c[j] ? c[j] : ret[j]));
}
}
else{
if(sig[i] == sig[i - 1]){
if(sig[i] == 1){
unsigned v = V[i - 1] + V[i];
for(int i = tt; i < lim; i++){
ret[i] = MIN(c[i], unsigned(ret[i]) + v);
}
}
else{
int v = V[i - 1] + V[i];
for(int i = tt; i < lim; i++){
ret[i] = MAX(0, ret[i] + v);
}
}
}
else{
int v1 = V[i - 1], v2 = V[i];
for(int j = tt; j < lim; j++){
ret[j] += v1;
ret[j] = (ret[j] < 0 ? 0 : (ret[j] > c[j] ? c[j] : ret[j]));
ret[j] += v2;
ret[j] = (ret[j] < 0 ? 0 : (ret[j] > c[j] ? c[j] : ret[j]));
}
}
}
}
}
if(q & 1){
for(int i = L[q - 1]; i <= R[q - 1]; i++){
ret[i] += V[q - 1];
ret[i] = (ret[i] < 0 ? 0 : (ret[i] > c[i] ? c[i] : ret[i]));
}
}
std::vector<int> s;
for(int i = 0; i < n; i++)
s.pb(ret[i]);
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:15:24: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
15 | #define MIN(x, y) ((x) < (y) ? (x) : (y))
| ~~~~^~~~~
candies.cpp:60:38: note: in expansion of macro 'MIN'
60 | ret[i] = MIN(c[i], unsigned(ret[i]) + v);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2660 ms |
12684 KB |
Output is correct |
2 |
Correct |
2671 ms |
12660 KB |
Output is correct |
3 |
Correct |
2667 ms |
12652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
75 ms |
8428 KB |
Output is correct |
3 |
Correct |
66 ms |
5784 KB |
Output is correct |
4 |
Correct |
2719 ms |
12792 KB |
Output is correct |
5 |
Correct |
2705 ms |
12664 KB |
Output is correct |
6 |
Correct |
2695 ms |
12744 KB |
Output is correct |
7 |
Correct |
2664 ms |
12660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
70 ms |
8416 KB |
Output is correct |
4 |
Correct |
65 ms |
4876 KB |
Output is correct |
5 |
Correct |
2851 ms |
12644 KB |
Output is correct |
6 |
Correct |
2888 ms |
12580 KB |
Output is correct |
7 |
Correct |
2987 ms |
12632 KB |
Output is correct |
8 |
Correct |
2893 ms |
12676 KB |
Output is correct |
9 |
Correct |
1353 ms |
12644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2660 ms |
12684 KB |
Output is correct |
7 |
Correct |
2671 ms |
12660 KB |
Output is correct |
8 |
Correct |
2667 ms |
12652 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
75 ms |
8428 KB |
Output is correct |
11 |
Correct |
66 ms |
5784 KB |
Output is correct |
12 |
Correct |
2719 ms |
12792 KB |
Output is correct |
13 |
Correct |
2705 ms |
12664 KB |
Output is correct |
14 |
Correct |
2695 ms |
12744 KB |
Output is correct |
15 |
Correct |
2664 ms |
12660 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
70 ms |
8416 KB |
Output is correct |
19 |
Correct |
65 ms |
4876 KB |
Output is correct |
20 |
Correct |
2851 ms |
12644 KB |
Output is correct |
21 |
Correct |
2888 ms |
12580 KB |
Output is correct |
22 |
Correct |
2987 ms |
12632 KB |
Output is correct |
23 |
Correct |
2893 ms |
12676 KB |
Output is correct |
24 |
Correct |
1353 ms |
12644 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
66 ms |
5196 KB |
Output is correct |
27 |
Correct |
82 ms |
8684 KB |
Output is correct |
28 |
Correct |
2688 ms |
12948 KB |
Output is correct |
29 |
Correct |
2725 ms |
12868 KB |
Output is correct |
30 |
Correct |
2687 ms |
12820 KB |
Output is correct |
31 |
Correct |
2668 ms |
13120 KB |
Output is correct |
32 |
Correct |
2689 ms |
13032 KB |
Output is correct |