#include <bits/stdc++.h>
#include "meetings.h"
using namespace std;
typedef long long ll;
struct node_t {
int left, right;
ll k, b, l, r;
bool cover;
node_t(bool cover = false, ll k = 0, ll b = 0):cover(cover), k(k), b(b) {
}
void apply(node_t tag) {
if (tag.cover) {
k = b = l = r = 0;
cover = true;
}
k += tag.k;
b += tag.b;
l += tag.k * left + tag.b;
r += tag.k * right + tag.b;
}
void reset() {
k = b = 0;
cover = false;
}
};
class segtree_t {
vector<node_t> tree;
int n;
void build(int x, int l, int r) {
tree[x].left = l;
tree[x].right = r;
if (l != r) {
int y = l + r >> 1, z = x + (y - l + 1 << 1);
build(x + 1, l, y);
build(z, y + 1, r);
}
}
void push(int x, int z) {
if (tree[x].cover || tree[x].k || tree[x].b) {
tree[x + 1].apply(tree[x]);
tree[z].apply(tree[x]);
tree[x].reset();
}
}
void pull(int x, int z) {
tree[x].l = tree[x + 1].l;
tree[x].r = tree[z].r;
}
ll query(int x, int l, int r, int p) {
if (l == r) {
return tree[x].l;
} else {
int y = l + r >> 1, z = x + (y - l + 1 << 1);
push(x, z);
if (p <= y) {
return query(x + 1, l, y, p);
} else {
return query(z, y + 1, r, p);
}
}
}
void add(int x, int l, int r, int ql, int qr, ll value) {
if (l == ql && r == qr) {
tree[x].apply(node_t(false, 0, value));
} else {
int y = l + r >> 1, z = x + (y - l + 1 << 1);
push(x, z);
if (qr <= y) {
add(x + 1, l, y, ql, qr, value);
} else if (ql > y) {
add(z, y + 1, r, ql, qr, value);
} else {
add(x + 1, l, y, ql, y, value);
add(z, y + 1, r, y + 1, qr, value);
}
pull(x, z);
}
}
void merge(int x, int l, int r, int ql, int qr, ll k, ll b) {
if (l == ql && r == qr) {
ll my_l = k * l + b, my_r = k * r + b;
if (my_l >= tree[x].l && my_r >= tree[x].r) {
return;
}
if (tree[x].l >= my_l && tree[x].r >= my_r) {
tree[x].apply(node_t(true, k, b));
return;
}
}
int y = l + r >> 1, z = x + (y - l + 1 << 1);
push(x, z);
if (qr <= y) {
merge(x + 1, l, y, ql, qr, k, b);
} else if (ql > y) {
merge(z, y + 1, r, ql, qr, k, b);
} else {
merge(x + 1, l, y, ql, y, k, b);
merge(z, y + 1, r, y + 1, qr, k, b);
}
pull(x, z);
}
public:
segtree_t(int n):n(n) {
tree.resize((n << 1) - 1);
build(0, 0, n - 1);
}
ll query(int p) {
return query(0, 0, n - 1, p);
}
void add(int l, int r, ll value) {
add(0, 0, n - 1, l, r, value);
}
void merge(int l, int r, ll k, ll b) {
merge(0, 0, n - 1, l, r, k, b);
}
};
vector<ll> minimum_costs(vector<int> h, vector<int> l, vector<int> r) {
int n = h.size(), log_n = 0;
while (1 << log_n <= n) {
++log_n;
}
auto my_max = [&](int x, int y) {
return h[x] < h[y] ? y : x;
};
vector<vector<int>> rmq(log_n, vector<int> (n));
vector<int> length(n);
for (int i = 0; i < n; ++i) {
rmq[0][i] = i;
}
for (int i = 1; i < log_n; ++i) {
for (int j = 0; j + (1 << i) <= n; ++j) {
rmq[i][j] = my_max(rmq[i - 1][j], rmq[i - 1][j + (1 << i - 1)]);
}
}
for (int i = 2; i < n; ++i) {
length[i] = length[i >> 1] + 1;
}
auto query_my_max = [&](int l, int r) {
int k = length[r - l];
return my_max(rmq[k][l], rmq[k][r - (1 << k) + 1]);
};
int m = l.size();
vector<ll> answer(m);
vector<vector<int>> event(n);
for (int i = 0; i < m; ++i) {
event[query_my_max(l[i], r[i])].push_back(i);
}
segtree_t prefix(n), suffix(n);
function<void(int, int)> solve = [&](int left, int right) {
int mid = query_my_max(left, right);
if (left < mid) {
solve(left, mid - 1);
}
if (mid < right) {
solve(mid + 1, right);
}
for (auto p : event[mid]) {
answer[p] = (ll)h[mid] * (r[p] - l[p] + 1);
if (l[p] < mid) {
answer[p] = min(answer[p], suffix.query(l[p]) + (ll)h[mid] * (r[p] - mid + 1));
}
if (r[p] > mid) {
answer[p] = min(answer[p], prefix.query(r[p]) + (ll)h[mid] * (mid - l[p] + 1));
}
}
ll prefix_value = h[mid] + (left < mid ? prefix.query(mid - 1) : 0);
ll suffix_value = h[mid] + (mid < right ? suffix.query(mid + 1) : 0);
prefix.add(mid, mid, prefix_value);
suffix.add(mid, mid, suffix_value);
if (mid < right) {
prefix.add(mid + 1, right, (ll)h[mid] * (mid - left + 1));
prefix.merge(mid + 1, right, h[mid], prefix_value - (ll)h[mid] * mid);
}
if (left < mid) {
suffix.add(left, mid - 1, (ll)h[mid] * (right - mid + 1));
suffix.merge(left, mid - 1, -h[mid], suffix_value + (ll)h[mid] * mid);
}
};
solve(0, n - 1);
return answer;
}
Compilation message
meetings.cpp: In constructor 'node_t::node_t(bool, ll, ll)':
meetings.cpp:11:8: warning: 'node_t::cover' will be initialized after [-Wreorder]
bool cover;
^~~~~
meetings.cpp:10:6: warning: 'll node_t::k' [-Wreorder]
ll k, b, l, r;
^
meetings.cpp:13:3: warning: when initialized here [-Wreorder]
node_t(bool cover = false, ll k = 0, ll b = 0):cover(cover), k(k), b(b) {
^~~~~~
meetings.cpp: In member function 'void segtree_t::build(int, int, int)':
meetings.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~^~~
meetings.cpp:41:42: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~~~~~^~~
meetings.cpp: In member function 'll segtree_t::query(int, int, int, int)':
meetings.cpp:64:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~^~~
meetings.cpp:64:42: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~~~~~^~~
meetings.cpp: In member function 'void segtree_t::add(int, int, int, int, int, ll)':
meetings.cpp:78:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~^~~
meetings.cpp:78:42: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~~~~~^~~
meetings.cpp: In member function 'void segtree_t::merge(int, int, int, int, int, ll, ll)':
meetings.cpp:103:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~^~~
meetings.cpp:103:40: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
int y = l + r >> 1, z = x + (y - l + 1 << 1);
~~~~~~^~~
meetings.cpp: In function 'std::vector<long long int> minimum_costs(std::vector<int>, std::vector<int>, std::vector<int>)':
meetings.cpp:152:64: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
rmq[i][j] = my_max(rmq[i - 1][j], rmq[i - 1][j + (1 << i - 1)]);
~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
1144 KB |
Output is correct |
3 |
Correct |
6 ms |
1144 KB |
Output is correct |
4 |
Correct |
6 ms |
1116 KB |
Output is correct |
5 |
Correct |
6 ms |
1144 KB |
Output is correct |
6 |
Correct |
6 ms |
1400 KB |
Output is correct |
7 |
Correct |
6 ms |
1144 KB |
Output is correct |
8 |
Correct |
6 ms |
1500 KB |
Output is correct |
9 |
Correct |
6 ms |
1400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
1144 KB |
Output is correct |
3 |
Correct |
6 ms |
1144 KB |
Output is correct |
4 |
Correct |
6 ms |
1116 KB |
Output is correct |
5 |
Correct |
6 ms |
1144 KB |
Output is correct |
6 |
Correct |
6 ms |
1400 KB |
Output is correct |
7 |
Correct |
6 ms |
1144 KB |
Output is correct |
8 |
Correct |
6 ms |
1500 KB |
Output is correct |
9 |
Correct |
6 ms |
1400 KB |
Output is correct |
10 |
Correct |
13 ms |
1912 KB |
Output is correct |
11 |
Correct |
10 ms |
1912 KB |
Output is correct |
12 |
Correct |
13 ms |
1912 KB |
Output is correct |
13 |
Correct |
12 ms |
1912 KB |
Output is correct |
14 |
Correct |
13 ms |
2396 KB |
Output is correct |
15 |
Correct |
12 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
50 ms |
4956 KB |
Output is correct |
3 |
Correct |
298 ms |
43872 KB |
Output is correct |
4 |
Correct |
271 ms |
32452 KB |
Output is correct |
5 |
Correct |
268 ms |
45812 KB |
Output is correct |
6 |
Correct |
278 ms |
46700 KB |
Output is correct |
7 |
Correct |
314 ms |
49648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
50 ms |
4956 KB |
Output is correct |
3 |
Correct |
298 ms |
43872 KB |
Output is correct |
4 |
Correct |
271 ms |
32452 KB |
Output is correct |
5 |
Correct |
268 ms |
45812 KB |
Output is correct |
6 |
Correct |
278 ms |
46700 KB |
Output is correct |
7 |
Correct |
314 ms |
49648 KB |
Output is correct |
8 |
Correct |
275 ms |
33508 KB |
Output is correct |
9 |
Correct |
228 ms |
33324 KB |
Output is correct |
10 |
Correct |
243 ms |
33448 KB |
Output is correct |
11 |
Correct |
261 ms |
32340 KB |
Output is correct |
12 |
Correct |
222 ms |
32628 KB |
Output is correct |
13 |
Correct |
248 ms |
32476 KB |
Output is correct |
14 |
Correct |
311 ms |
43920 KB |
Output is correct |
15 |
Correct |
262 ms |
32240 KB |
Output is correct |
16 |
Correct |
312 ms |
44400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
1144 KB |
Output is correct |
3 |
Correct |
6 ms |
1144 KB |
Output is correct |
4 |
Correct |
6 ms |
1116 KB |
Output is correct |
5 |
Correct |
6 ms |
1144 KB |
Output is correct |
6 |
Correct |
6 ms |
1400 KB |
Output is correct |
7 |
Correct |
6 ms |
1144 KB |
Output is correct |
8 |
Correct |
6 ms |
1500 KB |
Output is correct |
9 |
Correct |
6 ms |
1400 KB |
Output is correct |
10 |
Correct |
13 ms |
1912 KB |
Output is correct |
11 |
Correct |
10 ms |
1912 KB |
Output is correct |
12 |
Correct |
13 ms |
1912 KB |
Output is correct |
13 |
Correct |
12 ms |
1912 KB |
Output is correct |
14 |
Correct |
13 ms |
2396 KB |
Output is correct |
15 |
Correct |
12 ms |
1912 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
50 ms |
4956 KB |
Output is correct |
18 |
Correct |
298 ms |
43872 KB |
Output is correct |
19 |
Correct |
271 ms |
32452 KB |
Output is correct |
20 |
Correct |
268 ms |
45812 KB |
Output is correct |
21 |
Correct |
278 ms |
46700 KB |
Output is correct |
22 |
Correct |
314 ms |
49648 KB |
Output is correct |
23 |
Correct |
275 ms |
33508 KB |
Output is correct |
24 |
Correct |
228 ms |
33324 KB |
Output is correct |
25 |
Correct |
243 ms |
33448 KB |
Output is correct |
26 |
Correct |
261 ms |
32340 KB |
Output is correct |
27 |
Correct |
222 ms |
32628 KB |
Output is correct |
28 |
Correct |
248 ms |
32476 KB |
Output is correct |
29 |
Correct |
311 ms |
43920 KB |
Output is correct |
30 |
Correct |
262 ms |
32240 KB |
Output is correct |
31 |
Correct |
312 ms |
44400 KB |
Output is correct |
32 |
Correct |
2388 ms |
248396 KB |
Output is correct |
33 |
Correct |
1894 ms |
251124 KB |
Output is correct |
34 |
Correct |
2120 ms |
249364 KB |
Output is correct |
35 |
Correct |
2584 ms |
248832 KB |
Output is correct |
36 |
Correct |
1885 ms |
251004 KB |
Output is correct |
37 |
Correct |
2137 ms |
249708 KB |
Output is correct |
38 |
Correct |
2985 ms |
335740 KB |
Output is correct |
39 |
Correct |
2928 ms |
335784 KB |
Output is correct |
40 |
Correct |
2761 ms |
257908 KB |
Output is correct |
41 |
Correct |
3153 ms |
420672 KB |
Output is correct |
42 |
Correct |
3506 ms |
421604 KB |
Output is correct |
43 |
Correct |
3537 ms |
421616 KB |
Output is correct |
44 |
Correct |
3322 ms |
335736 KB |
Output is correct |