//#define _GLIBCXX_DEBUG
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using normal_queue = priority_queue <T, vector<T>, greater<>>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define ld long double
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define eb emplace_back
template<typename T>
bool ckmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<typename T>
bool ckmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
int bit(int x, int b) {
return (x >> b) & 1;
}
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
//soryan za musor sverhu
const ll infL = 3e18;
const int infI = 1e9 + 7;
const int N = 300001;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
/** * @brief Slope-Trick * @docs docs/slope-trick.md * @see https://maspypy.com/slope-trick-1-%E8%A7%A3%E8%AA%AC%E7%B7%A8 */
template<typename T>
struct SlopeTrick {
const T INF = numeric_limits<T>::max() / 3;
T min_f;
priority_queue <T, vector<T>, less<>> L;
priority_queue <T, vector<T>, greater<>> R;
T add_l, add_r;
void push_R(const T &a) {
R.push(a - add_r);
}
T top_R() const {
if (R.empty()) return INF;
else return R.top() + add_r;
}
T pop_R() {
T val = top_R();
if (not R.empty()) R.pop();
return val;
}
void push_L(const T &a) {
L.push(a - add_l);
}
T top_L() const {
if (L.empty()) return -INF;
else return L.top() + add_l;
}
T pop_L() {
T val = top_L();
if (not L.empty()) L.pop();
return val;
}
size_t size() {
return L.size() + R.size();
}
public:
SlopeTrick() : min_f(0), add_l(0), add_r(0) {}
struct Query {
T lx, rx, min_f;
};
// return min f(x)
Query query() const {
return (Query) {top_L(), top_R(), min_f};
}
// f(x) += a
void add_all(const T &a) {
min_f += a;
}
// add \_
// f(x) += max(a - x, 0)
void add_a_minus_x(const T &a) {
min_f += max(T(0), a - top_R());
push_R(a);
push_L(pop_R());
}
// add _/
// f(x) += max(x - a, 0)
void add_x_minus_a(const T &a) {
min_f += max(T(0), top_L() - a);
push_L(a);
push_R(pop_L());
}
// add \/
// f(x) += abs(x - a)
void add_abs(const T &a) {
add_a_minus_x(a);
add_x_minus_a(a);
}
// \/ -> \_
// f_{new} (x) = min f(y) (y <= x)
void clear_right() {
while (not R.empty()) R.pop();
}
// \/ -> _/
// f_{new} (x) = min f(y) (y >= x)
void clear_left() {
while (not L.empty()) L.pop();
}
// \/ -> \_/
// f_{new} (x) = min f(y) (x-b <= y <= x-a)
void shift(const T &a, const T &b) {
assert(a <= b);
add_l += a;
add_r += b;
}
// \/. -> .\/
// f_{new} (x) = f(x - a)
void shift(const T &a) {
shift(a, a);
}
// L, R will be destroyed(((((
T get(const T &x) {
T ret = min_f;
while (not L.empty()) {
ret += max(T(0), pop_L() - x);
}
while (not R.empty()) {
ret += max(T(0), x - pop_R());
}
return ret;
}
void mrg(SlopeTrick &st) {
if (st.size() > size()) {
swap(st.L, L);
swap(st.R, R);
swap(st.add_l, add_l);
swap(st.add_r, add_r);
swap(st.min_f, min_f);
}
while (not st.R.empty()) {
add_x_minus_a(st.pop_R());
}
while (not st.L.empty()) {
add_a_minus_x(st.pop_L());
}
min_f += st.min_f;
}
};
SlopeTrick<ll> st[N];
int p[N], c[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i < n + m; ++i) {
cin >> p[i] >> c[i];
--p[i];
}
for (int i = n; i < n + m; ++i) {
st[i].add_abs(c[i]);
st[p[i]].mrg(st[i]);
}
for (int i = n - 1; i > 0; --i) {
ll save = st[i].top_R();
st[i].clear_right();
st[i].push_R(save);
st[i].push_L(st[i].pop_L() + c[i]);
st[i].push_R(st[i].pop_R() + c[i]);
st[p[i]].mrg(st[i]);
}
cout << st[0].min_f;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
14 ms |
28500 KB |
Output is correct |
4 |
Correct |
13 ms |
28408 KB |
Output is correct |
5 |
Correct |
19 ms |
28508 KB |
Output is correct |
6 |
Correct |
15 ms |
28512 KB |
Output is correct |
7 |
Correct |
15 ms |
28468 KB |
Output is correct |
8 |
Correct |
16 ms |
28504 KB |
Output is correct |
9 |
Correct |
14 ms |
28500 KB |
Output is correct |
10 |
Correct |
14 ms |
28432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28480 KB |
Output is correct |
2 |
Correct |
13 ms |
28516 KB |
Output is correct |
3 |
Correct |
14 ms |
28500 KB |
Output is correct |
4 |
Correct |
14 ms |
28512 KB |
Output is correct |
5 |
Correct |
14 ms |
28512 KB |
Output is correct |
6 |
Correct |
14 ms |
28512 KB |
Output is correct |
7 |
Correct |
15 ms |
28500 KB |
Output is correct |
8 |
Correct |
16 ms |
28448 KB |
Output is correct |
9 |
Correct |
18 ms |
28500 KB |
Output is correct |
10 |
Correct |
16 ms |
28512 KB |
Output is correct |
11 |
Correct |
14 ms |
28500 KB |
Output is correct |
12 |
Correct |
14 ms |
28500 KB |
Output is correct |
13 |
Correct |
14 ms |
28500 KB |
Output is correct |
14 |
Correct |
14 ms |
28500 KB |
Output is correct |
15 |
Correct |
15 ms |
28500 KB |
Output is correct |
16 |
Correct |
14 ms |
28428 KB |
Output is correct |
17 |
Correct |
14 ms |
28500 KB |
Output is correct |
18 |
Correct |
14 ms |
28476 KB |
Output is correct |
19 |
Correct |
17 ms |
28500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
14 ms |
28500 KB |
Output is correct |
4 |
Correct |
13 ms |
28408 KB |
Output is correct |
5 |
Correct |
19 ms |
28508 KB |
Output is correct |
6 |
Correct |
15 ms |
28512 KB |
Output is correct |
7 |
Correct |
15 ms |
28468 KB |
Output is correct |
8 |
Correct |
16 ms |
28504 KB |
Output is correct |
9 |
Correct |
14 ms |
28500 KB |
Output is correct |
10 |
Correct |
14 ms |
28432 KB |
Output is correct |
11 |
Correct |
16 ms |
28480 KB |
Output is correct |
12 |
Correct |
13 ms |
28516 KB |
Output is correct |
13 |
Correct |
14 ms |
28500 KB |
Output is correct |
14 |
Correct |
14 ms |
28512 KB |
Output is correct |
15 |
Correct |
14 ms |
28512 KB |
Output is correct |
16 |
Correct |
14 ms |
28512 KB |
Output is correct |
17 |
Correct |
15 ms |
28500 KB |
Output is correct |
18 |
Correct |
16 ms |
28448 KB |
Output is correct |
19 |
Correct |
18 ms |
28500 KB |
Output is correct |
20 |
Correct |
16 ms |
28512 KB |
Output is correct |
21 |
Correct |
14 ms |
28500 KB |
Output is correct |
22 |
Correct |
14 ms |
28500 KB |
Output is correct |
23 |
Correct |
14 ms |
28500 KB |
Output is correct |
24 |
Correct |
14 ms |
28500 KB |
Output is correct |
25 |
Correct |
15 ms |
28500 KB |
Output is correct |
26 |
Correct |
14 ms |
28428 KB |
Output is correct |
27 |
Correct |
14 ms |
28500 KB |
Output is correct |
28 |
Correct |
14 ms |
28476 KB |
Output is correct |
29 |
Correct |
17 ms |
28500 KB |
Output is correct |
30 |
Correct |
16 ms |
28500 KB |
Output is correct |
31 |
Correct |
15 ms |
28500 KB |
Output is correct |
32 |
Correct |
15 ms |
28628 KB |
Output is correct |
33 |
Correct |
15 ms |
28628 KB |
Output is correct |
34 |
Correct |
15 ms |
28628 KB |
Output is correct |
35 |
Correct |
16 ms |
28716 KB |
Output is correct |
36 |
Correct |
16 ms |
28720 KB |
Output is correct |
37 |
Correct |
17 ms |
28776 KB |
Output is correct |
38 |
Correct |
21 ms |
28756 KB |
Output is correct |
39 |
Correct |
16 ms |
28852 KB |
Output is correct |
40 |
Correct |
19 ms |
28628 KB |
Output is correct |
41 |
Correct |
15 ms |
28564 KB |
Output is correct |
42 |
Correct |
17 ms |
28524 KB |
Output is correct |
43 |
Correct |
16 ms |
28756 KB |
Output is correct |
44 |
Correct |
17 ms |
28780 KB |
Output is correct |
45 |
Correct |
18 ms |
28840 KB |
Output is correct |
46 |
Correct |
17 ms |
29140 KB |
Output is correct |
47 |
Correct |
18 ms |
29140 KB |
Output is correct |
48 |
Correct |
18 ms |
28960 KB |
Output is correct |
49 |
Correct |
19 ms |
29012 KB |
Output is correct |
50 |
Correct |
17 ms |
28984 KB |
Output is correct |
51 |
Correct |
17 ms |
28876 KB |
Output is correct |
52 |
Correct |
17 ms |
29036 KB |
Output is correct |
53 |
Correct |
18 ms |
28984 KB |
Output is correct |
54 |
Correct |
21 ms |
29000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
14 ms |
28500 KB |
Output is correct |
4 |
Correct |
13 ms |
28408 KB |
Output is correct |
5 |
Correct |
19 ms |
28508 KB |
Output is correct |
6 |
Correct |
15 ms |
28512 KB |
Output is correct |
7 |
Correct |
15 ms |
28468 KB |
Output is correct |
8 |
Correct |
16 ms |
28504 KB |
Output is correct |
9 |
Correct |
14 ms |
28500 KB |
Output is correct |
10 |
Correct |
14 ms |
28432 KB |
Output is correct |
11 |
Correct |
16 ms |
28480 KB |
Output is correct |
12 |
Correct |
13 ms |
28516 KB |
Output is correct |
13 |
Correct |
14 ms |
28500 KB |
Output is correct |
14 |
Correct |
14 ms |
28512 KB |
Output is correct |
15 |
Correct |
14 ms |
28512 KB |
Output is correct |
16 |
Correct |
14 ms |
28512 KB |
Output is correct |
17 |
Correct |
15 ms |
28500 KB |
Output is correct |
18 |
Correct |
16 ms |
28448 KB |
Output is correct |
19 |
Correct |
18 ms |
28500 KB |
Output is correct |
20 |
Correct |
16 ms |
28512 KB |
Output is correct |
21 |
Correct |
14 ms |
28500 KB |
Output is correct |
22 |
Correct |
14 ms |
28500 KB |
Output is correct |
23 |
Correct |
14 ms |
28500 KB |
Output is correct |
24 |
Correct |
14 ms |
28500 KB |
Output is correct |
25 |
Correct |
15 ms |
28500 KB |
Output is correct |
26 |
Correct |
14 ms |
28428 KB |
Output is correct |
27 |
Correct |
14 ms |
28500 KB |
Output is correct |
28 |
Correct |
14 ms |
28476 KB |
Output is correct |
29 |
Correct |
17 ms |
28500 KB |
Output is correct |
30 |
Correct |
16 ms |
28500 KB |
Output is correct |
31 |
Correct |
15 ms |
28500 KB |
Output is correct |
32 |
Correct |
15 ms |
28628 KB |
Output is correct |
33 |
Correct |
15 ms |
28628 KB |
Output is correct |
34 |
Correct |
15 ms |
28628 KB |
Output is correct |
35 |
Correct |
16 ms |
28716 KB |
Output is correct |
36 |
Correct |
16 ms |
28720 KB |
Output is correct |
37 |
Correct |
17 ms |
28776 KB |
Output is correct |
38 |
Correct |
21 ms |
28756 KB |
Output is correct |
39 |
Correct |
16 ms |
28852 KB |
Output is correct |
40 |
Correct |
19 ms |
28628 KB |
Output is correct |
41 |
Correct |
15 ms |
28564 KB |
Output is correct |
42 |
Correct |
17 ms |
28524 KB |
Output is correct |
43 |
Correct |
16 ms |
28756 KB |
Output is correct |
44 |
Correct |
17 ms |
28780 KB |
Output is correct |
45 |
Correct |
18 ms |
28840 KB |
Output is correct |
46 |
Correct |
17 ms |
29140 KB |
Output is correct |
47 |
Correct |
18 ms |
29140 KB |
Output is correct |
48 |
Correct |
18 ms |
28960 KB |
Output is correct |
49 |
Correct |
19 ms |
29012 KB |
Output is correct |
50 |
Correct |
17 ms |
28984 KB |
Output is correct |
51 |
Correct |
17 ms |
28876 KB |
Output is correct |
52 |
Correct |
17 ms |
29036 KB |
Output is correct |
53 |
Correct |
18 ms |
28984 KB |
Output is correct |
54 |
Correct |
21 ms |
29000 KB |
Output is correct |
55 |
Correct |
20 ms |
29396 KB |
Output is correct |
56 |
Correct |
40 ms |
32124 KB |
Output is correct |
57 |
Correct |
57 ms |
34436 KB |
Output is correct |
58 |
Correct |
78 ms |
36176 KB |
Output is correct |
59 |
Correct |
100 ms |
38616 KB |
Output is correct |
60 |
Correct |
117 ms |
41248 KB |
Output is correct |
61 |
Correct |
138 ms |
43384 KB |
Output is correct |
62 |
Correct |
155 ms |
44736 KB |
Output is correct |
63 |
Correct |
194 ms |
47700 KB |
Output is correct |
64 |
Correct |
183 ms |
48504 KB |
Output is correct |
65 |
Correct |
96 ms |
35588 KB |
Output is correct |
66 |
Correct |
93 ms |
35704 KB |
Output is correct |
67 |
Correct |
91 ms |
35716 KB |
Output is correct |
68 |
Correct |
137 ms |
47144 KB |
Output is correct |
69 |
Correct |
151 ms |
47840 KB |
Output is correct |
70 |
Correct |
164 ms |
47796 KB |
Output is correct |
71 |
Correct |
258 ms |
67028 KB |
Output is correct |
72 |
Correct |
242 ms |
67080 KB |
Output is correct |
73 |
Correct |
218 ms |
64140 KB |
Output is correct |
74 |
Correct |
259 ms |
64588 KB |
Output is correct |
75 |
Correct |
232 ms |
63140 KB |
Output is correct |
76 |
Correct |
229 ms |
63224 KB |
Output is correct |
77 |
Correct |
250 ms |
61956 KB |
Output is correct |
78 |
Correct |
260 ms |
61756 KB |
Output is correct |
79 |
Correct |
194 ms |
57712 KB |
Output is correct |