#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll sts = 1;
vector < ll > pf;
vector < pair < ll, ll > > stn, stx;
void
pfa (ll i, ll x)
{
while (i < pf.size ())
{
pf[i] += x;
i += (i + 1) & -(i + 1);
}
}
ll
pfs (ll i)
{
ll sum = 0;
while (i >= 0)
{
sum += pf[i];
i -= (i + 1) & -(i + 1);
}
return sum;
}
void
stp (auto &st, int i)
{
if (i < sts)
{
st[i * 2].first += st[i].second;
st[i * 2].second += st[i].second;
st[i * 2 + 1].first += st[i].second;
st[i * 2 + 1].second += st[i].second;
}
st[i].second = 0;
}
void
sta1 (auto &st, ll i, int j, ll x, ll n, ll l, ll r, bool c)
{
if (j < l || r < i) return;
if (i <= l && r <= j)
{
st[n].first += x, st[n].second += x;
return;
}
stp (st, n);
int m = (l + r + 1) / 2;
sta1 (st, i, j, x, n * 2, l, m - 1, c);
sta1 (st, i, j, x, n * 2 + 1, m, r, c);
st[n].first = (c
? max (st[n * 2].first, st[n * 2 + 1].first)
: min (st[n * 2].first, st[n * 2 + 1].first));
}
void
sta (ll i, ll x)
{
pfa (i, x);
sta1 (stn, i, sts - 1, x, 1, 0, sts - 1, false);
sta1 (stx, i, sts - 1, x, 1, 0, sts - 1, true);
}
ll
stnx1 (auto &st, ll i, ll j, ll n, ll l, ll r, bool c)
{
assert (!(j < l || r < i));
if (i <= l && r <= j) return st[n].first;
stp (st, n);
ll m = (l + r + 1) / 2;
if (j < m) return stnx1 (st, i, j, n * 2, l, m - 1, c);
if (m <= i) return stnx1 (st, i, j, n * 2 + 1, m, r, c);
ll x = stnx1 (st, i, j, n * 2, l, m - 1, c);
ll y = stnx1 (st, i, j, n * 2 + 1, m, r, c);
return (c ? max (x, y) : min (x, y));
}
ll
stnx (ll i, bool c)
{
return stnx1 ((c ? stx : stn), 0, i, 1, 0, sts - 1, c);
}
ll
tst1 (ll x, ll n, ll l, ll r, ll mn, ll mx)
{
if (n >= sts) return l;
stp (stn, n);
stp (stx, n);
ll mn2 = min (mn, stn[n * 2].first), mx2 = max (mx, stx[n * 2].first), m = (l + r + 1) / 2;
if (mx2 - mn2 >= x)
return tst1 (x, n * 2, l, m - 1, mn, mx);
else
return tst1 (x, n * 2 + 1, m, r, mn2, mx2);
}
ll
tst (ll x)
{
return tst1 (x, 1, 0, sts - 1, 0, 0);
}
std::vector<int> distribute_candies (std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v)
{
ll n = c.size();
v.push_back (0);
l.push_back (0);
r.push_back (-1);
vector < ll > a;
for (auto &x : v) a.push_back (x);
reverse (a.begin (), a.end ());
reverse (l.begin (), l.end ());
reverse (r.begin (), r.end ());
a.push_back (-1e18);
l.push_back (0);
r.push_back (n - 1);
for (auto &x : a) x *= -1;
vector < vector < pair < ll, ll > > > ea (n + 1);
for (int i = 0; i < a.size (); ++i)
ea[l[i]].push_back ({ i, a[i] }),
ea[r[i] + 1].push_back ({ i, -a[i] });
while (sts < a.size ()) sts *= 2;
stn.resize (sts * 2);
stx.resize (sts * 2);
pf.resize (a.size ());
vector < int > ans (n);
for (int i = 0; i < n; ++i)
{
for (auto &x : ea[i]) sta (x.first, x.second);
int j = tst (c[i]);
if (pfs (j) == stnx (j, false))
ans[i] = c[i] - stnx (j, true);
else
ans[i] = -stnx (j, false);
}
return ans;
}
Compilation message
candies.cpp: In function 'void pfa(ll, ll)':
candies.cpp:13:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | while (i < pf.size ())
| ~~^~~~~~~~~~~~
candies.cpp: At global scope:
candies.cpp:33:6: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
33 | stp (auto &st, int i)
| ^~~~
candies.cpp:46:7: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
46 | sta1 (auto &st, ll i, int j, ll x, ll n, ll l, ll r, bool c)
| ^~~~
candies.cpp:72:8: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
72 | stnx1 (auto &st, ll i, ll j, ll n, ll l, ll r, bool c)
| ^~~~
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:127:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for (int i = 0; i < a.size (); ++i)
| ~~^~~~~~~~~~~
candies.cpp:130:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | while (sts < a.size ()) sts *= 2;
| ~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
848 KB |
Output is correct |
5 |
Correct |
5 ms |
696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
517 ms |
46620 KB |
Output is correct |
2 |
Correct |
556 ms |
46276 KB |
Output is correct |
3 |
Correct |
549 ms |
46248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
317 ms |
36668 KB |
Output is correct |
3 |
Correct |
86 ms |
9808 KB |
Output is correct |
4 |
Correct |
561 ms |
47904 KB |
Output is correct |
5 |
Correct |
556 ms |
48728 KB |
Output is correct |
6 |
Correct |
522 ms |
49072 KB |
Output is correct |
7 |
Correct |
512 ms |
48152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
144 ms |
35352 KB |
Output is correct |
4 |
Correct |
77 ms |
8916 KB |
Output is correct |
5 |
Correct |
294 ms |
43948 KB |
Output is correct |
6 |
Correct |
256 ms |
43484 KB |
Output is correct |
7 |
Correct |
248 ms |
45596 KB |
Output is correct |
8 |
Correct |
251 ms |
42404 KB |
Output is correct |
9 |
Correct |
251 ms |
45084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
848 KB |
Output is correct |
5 |
Correct |
5 ms |
696 KB |
Output is correct |
6 |
Correct |
517 ms |
46620 KB |
Output is correct |
7 |
Correct |
556 ms |
46276 KB |
Output is correct |
8 |
Correct |
549 ms |
46248 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
317 ms |
36668 KB |
Output is correct |
11 |
Correct |
86 ms |
9808 KB |
Output is correct |
12 |
Correct |
561 ms |
47904 KB |
Output is correct |
13 |
Correct |
556 ms |
48728 KB |
Output is correct |
14 |
Correct |
522 ms |
49072 KB |
Output is correct |
15 |
Correct |
512 ms |
48152 KB |
Output is correct |
16 |
Correct |
1 ms |
500 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
144 ms |
35352 KB |
Output is correct |
19 |
Correct |
77 ms |
8916 KB |
Output is correct |
20 |
Correct |
294 ms |
43948 KB |
Output is correct |
21 |
Correct |
256 ms |
43484 KB |
Output is correct |
22 |
Correct |
248 ms |
45596 KB |
Output is correct |
23 |
Correct |
251 ms |
42404 KB |
Output is correct |
24 |
Correct |
251 ms |
45084 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
81 ms |
8784 KB |
Output is correct |
27 |
Correct |
324 ms |
36228 KB |
Output is correct |
28 |
Correct |
578 ms |
46760 KB |
Output is correct |
29 |
Correct |
531 ms |
47152 KB |
Output is correct |
30 |
Correct |
533 ms |
47380 KB |
Output is correct |
31 |
Correct |
534 ms |
47532 KB |
Output is correct |
32 |
Correct |
579 ms |
47728 KB |
Output is correct |