#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 400005;
const ll INF = 1000000009000000009;
ll minu[N * 4];
ll laz[N * 4];
void clr(int tl, int tr, int pos)
{
minu[pos] = laz[pos] = 0;
if (tl == tr)
return;
int m = (tl + tr) / 2;
clr(tl, m, pos * 2);
clr(m + 1, tr, pos * 2 + 1);
}
void shi(int pos)
{
if (laz[pos] == 0)
return;
minu[pos * 2] += laz[pos];
laz[pos * 2] += laz[pos];
minu[pos * 2 + 1] += laz[pos];
laz[pos * 2 + 1] += laz[pos];
laz[pos] = 0;
}
void ubd(int tl, int tr, int l, int r, ll y, int pos)
{
if (l > r)
return;
if (tl == l && tr == r)
{
minu[pos] += y;
laz[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);
minu[pos] = min(minu[pos * 2], minu[pos * 2 + 1]);
}
ll qry(int tl, int tr, int l, int r, int pos)
{
if (l > r)
return INF;
if (tl == l && tr == r)
return minu[pos];
shi(pos);
int m = (tl + tr) / 2;
return min(qry(tl, m, l, min(m, r), pos * 2),
qry(m + 1, tr, max(m + 1, l), r, pos * 2 + 1));
}
vector<int> v[N];
ll dp[N];
ll solvv(vector<int> a, vector<pair<pair<int, int>, int> > b)
{
int n = sz(a) - 1;
for (int i = 0; i <= n + 1; ++i)
{
v[i].clear();
dp[i] = 0;
}
clr(0, n, 1);
for (int i = 1; i < sz(b); ++i)
{
v[b[i].fi.fi].push_back(i);
v[b[i].fi.se + 1].push_back(-i);
}
ll s = 0;
for (int i = 1; i <= n + 1; ++i)
{
for (int j = 0; j < v[i].size(); ++j)
{
if (v[i][j] > 0)
{
ubd(0, n, b[v[i][j]].fi.fi, b[v[i][j]].fi.se, -b[v[i][j]].se, 1);
s += b[v[i][j]].se;
}
else
{
ubd(0, n, b[-v[i][j]].fi.fi, b[-v[i][j]].fi.se, b[-v[i][j]].se, 1);
s -= b[-v[i][j]].se;
}
}
if (i == n + 1)
break;
dp[i] = s + qry(0, n, 0, i - 1, 1);
ubd(0, n, 0, i - 1, a[i], 1);
ubd(0, n, i, i, dp[i], 1);
}
return minu[1];
}
void solv()
{
int n, m;
int* a, *b;
int* l, *r;
cin >> n >> m;
a = new int[n + m + 5];
b = new int[n + m + 5];
l = new int[n + m + 5];
r = new int[n + m + 5];
for (int i = 1; i <= n + m - 1; ++i)
cin >> a[i];
for (int i = 1; i <= n + m - 1; ++i)
cin >> b[i];
for (int i = 1; i <= n + m - 1; ++i)
{
int x, y;
if (i <= m)
{
x = 1;
y = (m - i + 1);
}
else
{
x = (i - m + 1);
y = 1;
}
int q = min(n - x + 1, m - y + 1);
int u = min(x - 1, m - y);
int xx = x - u;
int yy = y + u;
int L, R;
if (xx == 1)
{
L = yy;
}
else
{
L = m + xx - 1;
}
R = L + q * 2 - 1 - 1;
//cout << i << ' ' << L << ' ' << R << "\n";
l[i] = L;
r[i] = R;
}
ll ans = 0;
vector<int> v;
v.push_back(-1);
for (int i = 1; i <= n + m - 1; ++i)
{
if (i % 2 == l[1] % 2)
{
v.push_back(b[i]);
}
}
vector<pair<pair<int, int>, int> > vv;
vv.push_back(m_p(m_p(-1, -1), -1));
for (int i = 1; i <= n + m - 1; i += 2)
{
vv.push_back(m_p(m_p(l[i] / 2 + l[i] % 2, r[i] / 2 + r[i] % 2), a[i]));
}
ans += solvv(v, vv);
v.clear();
v.push_back(-1);
for (int i = 1; i <= n + m - 1; ++i)
{
if (i % 2 == l[2] % 2)
{
v.push_back(b[i]);
}
}
vv.clear();
vv.push_back(m_p(m_p(-1, -1), -1));
for (int i = 2; i <= n + m - 1; i += 2)
{
vv.push_back(m_p(m_p(l[i] / 2 + l[i] % 2, r[i] / 2 + r[i] % 2), a[i]));
}
ans += solvv(v, vv);
cout << ans << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
Compilation message
colouring.cpp: In function 'll solvv(std::vector<int>, std::vector<std::pair<std::pair<int, int>, int> >)':
colouring.cpp:89:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int j = 0; j < v[i].size(); ++j)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9704 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9736 KB |
Output is correct |
6 |
Correct |
7 ms |
9732 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9660 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9704 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9736 KB |
Output is correct |
6 |
Correct |
7 ms |
9732 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9660 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9652 KB |
Output is correct |
13 |
Correct |
7 ms |
9732 KB |
Output is correct |
14 |
Correct |
7 ms |
9724 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9704 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9736 KB |
Output is correct |
6 |
Correct |
7 ms |
9732 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9660 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9652 KB |
Output is correct |
13 |
Correct |
7 ms |
9732 KB |
Output is correct |
14 |
Correct |
7 ms |
9724 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
6 ms |
9740 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
8 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9704 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9736 KB |
Output is correct |
6 |
Correct |
7 ms |
9732 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9660 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9652 KB |
Output is correct |
13 |
Correct |
7 ms |
9732 KB |
Output is correct |
14 |
Correct |
7 ms |
9724 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
6 ms |
9740 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
8 ms |
9676 KB |
Output is correct |
30 |
Correct |
12 ms |
10092 KB |
Output is correct |
31 |
Correct |
12 ms |
10060 KB |
Output is correct |
32 |
Correct |
12 ms |
10060 KB |
Output is correct |
33 |
Correct |
11 ms |
9932 KB |
Output is correct |
34 |
Correct |
11 ms |
9940 KB |
Output is correct |
35 |
Correct |
9 ms |
9832 KB |
Output is correct |
36 |
Correct |
10 ms |
9904 KB |
Output is correct |
37 |
Correct |
9 ms |
9804 KB |
Output is correct |
38 |
Correct |
9 ms |
9872 KB |
Output is correct |
39 |
Correct |
9 ms |
9836 KB |
Output is correct |
40 |
Correct |
10 ms |
9928 KB |
Output is correct |
41 |
Correct |
9 ms |
9932 KB |
Output is correct |
42 |
Correct |
9 ms |
9872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
319 ms |
27928 KB |
Output is correct |
2 |
Correct |
329 ms |
27900 KB |
Output is correct |
3 |
Correct |
328 ms |
27948 KB |
Output is correct |
4 |
Correct |
334 ms |
28148 KB |
Output is correct |
5 |
Correct |
351 ms |
28348 KB |
Output is correct |
6 |
Correct |
327 ms |
27352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
926 ms |
46912 KB |
Output is correct |
2 |
Correct |
893 ms |
46060 KB |
Output is correct |
3 |
Correct |
900 ms |
46060 KB |
Output is correct |
4 |
Correct |
941 ms |
46348 KB |
Output is correct |
5 |
Correct |
908 ms |
46344 KB |
Output is correct |
6 |
Correct |
898 ms |
43408 KB |
Output is correct |
7 |
Correct |
907 ms |
45952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9704 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9736 KB |
Output is correct |
6 |
Correct |
7 ms |
9732 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9660 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9652 KB |
Output is correct |
13 |
Correct |
7 ms |
9732 KB |
Output is correct |
14 |
Correct |
7 ms |
9724 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
6 ms |
9740 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
8 ms |
9676 KB |
Output is correct |
30 |
Correct |
12 ms |
10092 KB |
Output is correct |
31 |
Correct |
12 ms |
10060 KB |
Output is correct |
32 |
Correct |
12 ms |
10060 KB |
Output is correct |
33 |
Correct |
11 ms |
9932 KB |
Output is correct |
34 |
Correct |
11 ms |
9940 KB |
Output is correct |
35 |
Correct |
9 ms |
9832 KB |
Output is correct |
36 |
Correct |
10 ms |
9904 KB |
Output is correct |
37 |
Correct |
9 ms |
9804 KB |
Output is correct |
38 |
Correct |
9 ms |
9872 KB |
Output is correct |
39 |
Correct |
9 ms |
9836 KB |
Output is correct |
40 |
Correct |
10 ms |
9928 KB |
Output is correct |
41 |
Correct |
9 ms |
9932 KB |
Output is correct |
42 |
Correct |
9 ms |
9872 KB |
Output is correct |
43 |
Correct |
319 ms |
27928 KB |
Output is correct |
44 |
Correct |
329 ms |
27900 KB |
Output is correct |
45 |
Correct |
328 ms |
27948 KB |
Output is correct |
46 |
Correct |
334 ms |
28148 KB |
Output is correct |
47 |
Correct |
351 ms |
28348 KB |
Output is correct |
48 |
Correct |
327 ms |
27352 KB |
Output is correct |
49 |
Correct |
926 ms |
46912 KB |
Output is correct |
50 |
Correct |
893 ms |
46060 KB |
Output is correct |
51 |
Correct |
900 ms |
46060 KB |
Output is correct |
52 |
Correct |
941 ms |
46348 KB |
Output is correct |
53 |
Correct |
908 ms |
46344 KB |
Output is correct |
54 |
Correct |
898 ms |
43408 KB |
Output is correct |
55 |
Correct |
907 ms |
45952 KB |
Output is correct |
56 |
Correct |
326 ms |
27380 KB |
Output is correct |
57 |
Correct |
342 ms |
27548 KB |
Output is correct |
58 |
Correct |
331 ms |
27916 KB |
Output is correct |
59 |
Correct |
332 ms |
26584 KB |
Output is correct |
60 |
Correct |
348 ms |
27960 KB |
Output is correct |
61 |
Correct |
349 ms |
27832 KB |
Output is correct |
62 |
Correct |
365 ms |
26628 KB |
Output is correct |
63 |
Correct |
401 ms |
27920 KB |
Output is correct |
64 |
Correct |
397 ms |
28004 KB |
Output is correct |
65 |
Correct |
452 ms |
28652 KB |
Output is correct |
66 |
Correct |
504 ms |
30140 KB |
Output is correct |
67 |
Correct |
581 ms |
31932 KB |
Output is correct |
68 |
Correct |
882 ms |
43528 KB |
Output is correct |
69 |
Correct |
898 ms |
45112 KB |
Output is correct |
70 |
Correct |
707 ms |
39104 KB |
Output is correct |