#include <bits/stdc++.h>
using namespace std;
#include "candies.h"
//#define int long long
typedef long long ll;
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct dat{
ll sm, mx, mn, mxpre, mnpre, mxsuf, mnsuf;
};
dat comb(dat a, dat b){
dat ans;
ans.sm = a.sm + b.sm;
ans.mx = max({a.mx, b.mx, a.mxsuf + b.mxpre});
ans.mn = min({a.mn, b.mn, a.mnsuf + b.mnpre});
ans.mxpre = max(a.mxpre, a.sm + b.mxpre);
ans.mnpre = min(a.mnpre, a.sm + b.mnpre);
ans.mxsuf = max(b.mxsuf, b.sm + a.mxsuf);
ans.mnsuf = min(b.mnsuf, b.sm + a.mnsuf);
return ans;
}
struct node{
int s, e, m;
dat val;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e)l = new node(s, m), r = new node(m + 1, e);
val.sm = val.mx = val.mn = val.mxpre = val.mnpre = val.mxsuf = val.mnsuf = 0;
}
void upd(int p, int v){
if(s == e){
val.sm = val.mx = val.mn = val.mxpre = val.mnpre = val.mxsuf = val.mnsuf = v;
}
else{
if(p <= m)l->upd(p, v);
else r->upd(p, v);
val = comb(l->val, r->val);
}
}
ll fd(ll cap){
if(s == e)return (val.sm > cap ? cap : (val.sm < 0 ? 0 : val.sm));
if(r->val.mx > cap || r->val.mn < -cap)return r->fd(cap);
else if(l->val.mxsuf > cap || l->val.mnsuf < -cap)return r->val.sm;
else if(l->val.mxsuf + r->val.mxpre > cap)return r->val.sm - r->val.mxpre + cap;
else if(l->val.mnsuf + r->val.mnpre < -cap)return r->val.sm - r->val.mnpre;
else return l->fd(cap) + r->val.sm;
}
}*root;
vector <pi> Q[200005];
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(), q = (int)l.size();
for(int i = 0; i < q; i++){
Q[l[i]].push_back({i + 1, v[i]});
Q[r[i] + 1].push_back({i + 1, 0});
}
root = new node(0, q);
vector <int> ans(n);
for(int i = 0; i < n; i++){
for(auto [a, b] : Q[i]){
root->upd(a, b);
}
root->upd(0, -c[i]);
ans[i] = root->fd(c[i]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
581 ms |
61776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |