#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
const ll maxn = 2e5 + 10;
const ll mod = 998244353 ;
const ll base = 3e18;
struct tk {
ll pmn = 0, pmx = 0, sum = 0;
ll chk = 0;
};
tk mer(tk a, tk b) {
if (a.chk == 0)
return b;
if (b.chk == 0)
return a;
tk c;
c.sum = a.sum + b.sum;
c.pmn = min(a.pmn, a.sum + b.pmn);
c.pmx = max(a.pmx, a.sum + b.pmx);
c.chk = a.chk;
return c;
}
tk st[4 * maxn];
void update(ll id, ll left, ll right, ll x, ll diff) {
if (x > right || x < left)
return ;
if (left == right) {
st[id].sum += diff;
st[id].pmn = min(0ll, st[id].sum);
st[id].pmx = max(0ll, st[id].sum);
st[id].chk = (st[id].sum >= 0) + 1;
return ;
}
ll mid = (left + right) / 2;
update(id * 2, left, mid, x, diff);
update(id * 2 + 1, mid + 1, right, x, diff);
st[id] = mer(st[id * 2], st[id * 2 + 1]);
}
tk get(ll id, ll left, ll right, ll x, ll y) {
tk a;
if (x > right || y < left || x > y)
return a;
if (x <= left && y >= right)
return st[id];
ll mid = (left + right) / 2;
return mer(get(id * 2, left, mid, x, y), get(id * 2 + 1, mid + 1, right, x, y));
}
vector<int> adj[maxn];
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
ll n = c.size();
ll q = l.size();
for (int i = 0; i < q; i++) {
adj[l[i]].pb(i + 2);
if (r[i] + 1 < n)
adj[r[i] + 1].pb(-(i + 2));
}
int mx = 0;
for (int i = 0; i < n; i++)
mx = max(mx, c[i]);
update(1, 1, q + 1, 1, -mx);
vector<int> ans;
for (int i = 0; i < n; i++) {
for (auto to : adj[i]) {
if (to > 0) {
update(1, 1, q + 1, to, v[to - 2]);
} else {
update(1, 1, q + 1, -to, -v[-to - 2]);
}
}
adj[i].clear();
ll l = 1, h = q + 1;
while (l <= h) {
ll mid = (l + h) / 2;
tk p = get(1, 1, q + 1, mid, q + 1);
if (p.pmx - p.pmn >= c[i])
l = mid + 1;
else
h = mid - 1;
}
tk nw = get(1, 1, q + 1, h, q + 1);
/*if (i==n-1)
{
cout <<h<<" "<<q+2<<endl;
cout <<nw.sum<<" "<<nw.pmn<<" "<<nw.pmx<<" "<<nw.chk<<endl;
}*/
//assert(nw.pmx-nw.pmn>=c[i]);
ans.pb((nw.chk == 2 ? (int)(c[i] - (nw.pmx - nw.sum)) : (int)(nw.sum - nw.pmn)));
}
return ans;
}
/*int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("test.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
vector<int > l;
vector<int > r;
vector<int > v;
vector<int > t;
ll n;
cin>> n;
while (n--)
{
int x;
cin>> x;
t.pb(x);
}
ll q;
cin>> q;
while (q--)
{
int a, b, c;
cin>>a>> b>> c;
l.pb(a);
r.pb(b);
v.pb(c);
}
vector<int> ans=distribute_candies(t,l,r,v);
for (auto to:ans) cout <<to<<endl;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5196 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
6 ms |
5224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1014 ms |
35372 KB |
Output is correct |
2 |
Correct |
997 ms |
35336 KB |
Output is correct |
3 |
Correct |
996 ms |
35352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
199 ms |
29420 KB |
Output is correct |
3 |
Correct |
311 ms |
9856 KB |
Output is correct |
4 |
Correct |
956 ms |
35444 KB |
Output is correct |
5 |
Correct |
983 ms |
35544 KB |
Output is correct |
6 |
Correct |
906 ms |
35428 KB |
Output is correct |
7 |
Correct |
967 ms |
35336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
90 ms |
27928 KB |
Output is correct |
4 |
Correct |
282 ms |
8728 KB |
Output is correct |
5 |
Correct |
747 ms |
30820 KB |
Output is correct |
6 |
Correct |
765 ms |
30840 KB |
Output is correct |
7 |
Correct |
836 ms |
30904 KB |
Output is correct |
8 |
Correct |
760 ms |
30908 KB |
Output is correct |
9 |
Correct |
791 ms |
30876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5196 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
6 ms |
5224 KB |
Output is correct |
6 |
Correct |
1014 ms |
35372 KB |
Output is correct |
7 |
Correct |
997 ms |
35336 KB |
Output is correct |
8 |
Correct |
996 ms |
35352 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
199 ms |
29420 KB |
Output is correct |
11 |
Correct |
311 ms |
9856 KB |
Output is correct |
12 |
Correct |
956 ms |
35444 KB |
Output is correct |
13 |
Correct |
983 ms |
35544 KB |
Output is correct |
14 |
Correct |
906 ms |
35428 KB |
Output is correct |
15 |
Correct |
967 ms |
35336 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
90 ms |
27928 KB |
Output is correct |
19 |
Correct |
282 ms |
8728 KB |
Output is correct |
20 |
Correct |
747 ms |
30820 KB |
Output is correct |
21 |
Correct |
765 ms |
30840 KB |
Output is correct |
22 |
Correct |
836 ms |
30904 KB |
Output is correct |
23 |
Correct |
760 ms |
30908 KB |
Output is correct |
24 |
Correct |
791 ms |
30876 KB |
Output is correct |
25 |
Correct |
3 ms |
5000 KB |
Output is correct |
26 |
Correct |
286 ms |
8880 KB |
Output is correct |
27 |
Correct |
207 ms |
29380 KB |
Output is correct |
28 |
Correct |
937 ms |
35308 KB |
Output is correct |
29 |
Correct |
938 ms |
35260 KB |
Output is correct |
30 |
Correct |
1084 ms |
35404 KB |
Output is correct |
31 |
Correct |
947 ms |
35272 KB |
Output is correct |
32 |
Correct |
909 ms |
35208 KB |
Output is correct |