#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define m_p make_pair
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
typedef long long ll;
const int N = 200005;
int n, q, C;
struct ban
{
int ans, l, r;
ban(){}
ban(int ans, int l, int r)
{
this->ans = ans;
this->l = l;
this->r = r;
}
};
ban mer(const ban& l, const ban& r)
{
ban res;
if (l.ans <= r.l)
res.ans = r.ans;
else if (l.ans >= r.r)
res.ans = r.ans + r.r - r.l;
else
res.ans = r.ans + l.ans - r.l;
res.l = l.l + max(0, r.l - l.ans);
res.r = l.r - max(0, l.ans + l.r - l.l - r.r);
if (res.l > res.r)
res.l = res.r = 0;
return res;
}
ban t[N * 4];
void bil(int tl, int tr, int pos)
{
t[pos] = ban(0, 0, C);
if (tl == tr)
return;
int m = (tl + tr) / 2;
bil(tl, m, pos * 2);
bil(m + 1, tr, pos * 2 + 1);
}
void ubd(int tl, int tr, int x, int y, int pos)
{
if (tl == tr)
{
y = min(y, C);
y = max(y, -C);
if (y == 0)
t[pos] = ban(0, 0, C);
else if (y > 0)
t[pos] = ban(y, 0, C - y);
else
t[pos] = ban(0, -y, C);
return;
}
int m = (tl + tr) / 2;
if (x <= m)
ubd(tl, m, x, y, pos * 2);
else
ubd(m + 1, tr, x, y, pos * 2 + 1);
t[pos] = mer(t[pos * 2], t[pos * 2 + 1]);
}
vector<int> v[N];
vector<pair<int, int> > p;
int c[N * 4];
void shi(int pos)
{
if (c[pos] == N)
return;
c[pos * 2] = c[pos * 2 + 1] = c[pos];
c[pos] = N;
}
void ubd(int tl, int tr, int l, int r, int y, int pos)
{
if (l > r)
return;
if (tl == l && tr == r)
{
c[pos] = y;
return;
}
shi(pos);
int m = (tl + tr) / 2;
ubd(tl, m, l, min(m, r), y, pos * 2);
ubd(m + 1, tr, max(m + 1, l), r, y, pos * 2 + 1);
}
int qry(int tl, int tr, int x, int pos)
{
if (tl == tr)
return c[pos];
shi(pos);
int m = (tl + tr) / 2;
if (x <= m)
return qry(tl, m, x, pos * 2);
else
return qry(m + 1, tr, x, pos * 2 + 1);
}
vector<ll> pr;
int qryy(int i, int q, const vector<int>& v)
{
int j = qry(0, n - 1, i, 1);
int s;
if (j == -1 || v[j] < 0)
s = 0;
else
s = p[i].fi;
return s + pr[q] - pr[j + 1];
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> vv) {
n = sz(c);
q = sz(l);
vector<int> ans(n);
bool f = true;
for (int i = 0; i < n; ++i)
{
if (c[i] != c[0])
{
f = false;
break;
}
}
if (f)
{
for (int i = 0; i < q; ++i)
{
v[l[i]].push_back((i + 1));
v[r[i] + 1].push_back(-(i + 1));
}
C = c[0];
bil(0, q - 1, 1);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < sz(v[i]); ++j)
{
if (v[i][j] > 0)
{
--v[i][j];
ubd(0, q - 1, v[i][j], vv[v[i][j]], 1);
}
else
{
v[i][j] *= -1;
--v[i][j];
ubd(0, q - 1, v[i][j], 0, 1);
}
}
ans[i] = t[1].ans;
}
}
else
{
for (int i = 0; i < n; ++i)
{
p.push_back(m_p(c[i], i));
}
sort(all(p));
pr.push_back(0);
for (int i = 0; i < q; ++i)
pr.push_back(pr.back() + vv[i]);
ubd(0, n - 1, 0, n - 1, -1, 1);
for (int i = 0; i < q; ++i)
{
if (vv[i] > 0)
{
int l = 0, r = n - 1;
int u = -1;
while (l <= r)
{
int m = (l + r) / 2;
if (qryy(m, i, vv) + vv[i] >= m)
{
u = m;
l = m + 1;
}
else
r = m - 1;
}
ubd(0, n - 1, 0, u, i, 1);
}
else
{
int l = 0, r = n - 1;
int u = -1;
while (l <= r)
{
int m = (l + r) / 2;
if (qryy(m, i, vv) + vv[i] <= 0)
{
u = m;
l = m + 1;
}
else
r = m - 1;
}
ubd(0, n - 1, 0, u, i, 1);
}
}
for (int i = 0; i < n; ++i)
{
ans[i] = qryy(i, q, vv);
}
}
return ans;
}
/*
10
1 2 3 4 5 6 7 8 9 10
2
0 9 10
0 9 -5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
330 ms |
22232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
180 ms |
21156 KB |
Output is correct |
3 |
Correct |
45 ms |
10880 KB |
Output is correct |
4 |
Correct |
284 ms |
29724 KB |
Output is correct |
5 |
Correct |
271 ms |
30112 KB |
Output is correct |
6 |
Correct |
282 ms |
30480 KB |
Output is correct |
7 |
Correct |
260 ms |
29828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |