#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#include "candies.h"
vpii events[200001];
LLI lazy[1 << 19],ss[1 << 19],bb[1 << 19],sum = 0;
int prop(int s,int e,int i) {
if (lazy[i] != 0) {
ss[i] += lazy[i],bb[i] += lazy[i];
if (s != e) lazy[2*i+1] += lazy[i],lazy[2*i+2] += lazy[i];
lazy[i] = 0;
}
return 0;
}
int update(int s,int e,int as,int ae,int i,int num) {
prop(s,e,i);
if ((s > ae) || (e < as)) return 0;
else if ((s >= as) && (e <= ae)) {
lazy[i] += num;
prop(s,e,i);
return 0;
}
int mid = (s+e) / 2;
update(s,mid,as,ae,2*i+1,num),update(mid+1,e,as,ae,2*i+2,num);
ss[i] = min(ss[2*i+1],ss[2*i+2]),bb[i] = max(bb[2*i+1],bb[2*i+2]);
return 0;
}
int query(int s,int e,int c) {
int i = 0;
LLI sss = 1e18,bbb = -1e18;
while (s < e) {
int mid = (s+e) / 2;
prop(mid+1,e,2*i+2);
if (max(bbb,bb[2*i+2])-min(sss,ss[2*i+2]) >= c) i = 2*i+2,s = mid+1;
else sss = min(sss,ss[2*i+2]),bbb = max(bbb,bb[2*i+2]),i = 2*i+1,e = mid,prop(s,e,i);
}
sss = min(sss,ss[i]),bbb = max(bbb,bb[i]);
if (sss == ss[i]) return c-(bbb-sum);
else return sum-sss;
}
vector<int> distribute_candies(vector<int> c,vector<int> l,vector<int> r,vector<int> v) {
int i,n = c.size(),q = v.size();
vi ans(n);
for (i = 0; i < q; i++) {
events[l[i]].pb(mp(i,v[i]));
events[r[i]+1].pb(mp(i,-v[i]));
}
for (i = 0; i < n; i++) {
for (auto [j,v]: events[i]) update(0,q,j+1,q,0,v),sum += v;
if (bb[0]-ss[0] <= c[i]) ans[i] = sum-ss[0];
else ans[i] = query(0,q,c[i]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5008 KB |
Output is correct |
3 |
Correct |
6 ms |
5204 KB |
Output is correct |
4 |
Correct |
5 ms |
5204 KB |
Output is correct |
5 |
Correct |
6 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
516 ms |
31788 KB |
Output is correct |
2 |
Correct |
600 ms |
35504 KB |
Output is correct |
3 |
Correct |
523 ms |
35296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
257 ms |
26760 KB |
Output is correct |
3 |
Correct |
65 ms |
8916 KB |
Output is correct |
4 |
Correct |
455 ms |
31648 KB |
Output is correct |
5 |
Correct |
448 ms |
33988 KB |
Output is correct |
6 |
Correct |
508 ms |
34072 KB |
Output is correct |
7 |
Correct |
456 ms |
37392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5000 KB |
Output is correct |
3 |
Correct |
129 ms |
25512 KB |
Output is correct |
4 |
Correct |
57 ms |
7876 KB |
Output is correct |
5 |
Correct |
174 ms |
27920 KB |
Output is correct |
6 |
Correct |
207 ms |
31844 KB |
Output is correct |
7 |
Correct |
186 ms |
32516 KB |
Output is correct |
8 |
Correct |
179 ms |
31096 KB |
Output is correct |
9 |
Correct |
184 ms |
32560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5008 KB |
Output is correct |
3 |
Correct |
6 ms |
5204 KB |
Output is correct |
4 |
Correct |
5 ms |
5204 KB |
Output is correct |
5 |
Correct |
6 ms |
5204 KB |
Output is correct |
6 |
Correct |
516 ms |
31788 KB |
Output is correct |
7 |
Correct |
600 ms |
35504 KB |
Output is correct |
8 |
Correct |
523 ms |
35296 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
257 ms |
26760 KB |
Output is correct |
11 |
Correct |
65 ms |
8916 KB |
Output is correct |
12 |
Correct |
455 ms |
31648 KB |
Output is correct |
13 |
Correct |
448 ms |
33988 KB |
Output is correct |
14 |
Correct |
508 ms |
34072 KB |
Output is correct |
15 |
Correct |
456 ms |
37392 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5000 KB |
Output is correct |
18 |
Correct |
129 ms |
25512 KB |
Output is correct |
19 |
Correct |
57 ms |
7876 KB |
Output is correct |
20 |
Correct |
174 ms |
27920 KB |
Output is correct |
21 |
Correct |
207 ms |
31844 KB |
Output is correct |
22 |
Correct |
186 ms |
32516 KB |
Output is correct |
23 |
Correct |
179 ms |
31096 KB |
Output is correct |
24 |
Correct |
184 ms |
32560 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
62 ms |
8800 KB |
Output is correct |
27 |
Correct |
257 ms |
29124 KB |
Output is correct |
28 |
Correct |
427 ms |
35956 KB |
Output is correct |
29 |
Correct |
480 ms |
36336 KB |
Output is correct |
30 |
Correct |
476 ms |
36612 KB |
Output is correct |
31 |
Correct |
500 ms |
36720 KB |
Output is correct |
32 |
Correct |
468 ms |
36912 KB |
Output is correct |