#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt")
using ll = long long int;
using ull = unsigned long long int;
using vi = vector<ll>;
using ii = pair<ll,ll>;
using vii = vector<ii>;
using ld = long double;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using ordered_set = tree < T, null_type, less<T>,
rb_tree_tag,
tree_order_statistics_node_update >;
#ifdef SA_DEBUG
template <class T>
void print(T a) {cerr << a << endl;}
template <class T, class... V>
void print(T a, V... b) {cerr << a << ", "; print(b...);}
#define dbg(...) cerr << "[" << __LINE__ << "] " << #__VA_ARGS__ << " :: ", print(__VA_ARGS__)
#else
#define dbg(...) 7
#endif
#define eb emplace_back
#define fi first
#define se second
const ll INFL = 1e18;
const int INF = 1e9;
const double PI = acos(-1);
const ll mod = 1e9+7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T, class V>
ostream& operator << (ostream &s, pair<T, V> a){
s << a.fi << ' ' << a.se; return s;
}
template<class T, class V>
istream& operator >> (istream &s, pair<T, V> &a){
s >> a.fi >> a.se; return s;
}
template<class T>
ostream& operator << (ostream &s, vector<T> a){
for(int i = 0; i < (int)a.size(); i++){
if(i > 0)s << ' ' ;
s << a[i];
} return s;
}
template<class T>
istream& operator >> (istream &s, vector<T> &a){
for(T &x : a)s >> x;
return s;
}
template<class T>
void input(T a[], int l, int r, istream &s = cin){
while(l <= r)s >> a[l++];
}
template<class T>
void output(T a[], int l, int r, bool en = true, ostream &s = cout){
while(l <= r){ s << a[l++];
if(l <= r) s << ' ';
} if(en)s << "\n";
}
const int N = 4e5+3, K = 17, M = 2e5 + 5;
//====================================================================//
template< typename T >
struct SlopeTrick {
T INF = numeric_limits< T >::max() / 3;
T min_f, add_l, add_r;
priority_queue< T, vector< T >, less<> > L;
priority_queue< T, vector< T >, greater<> > R;
void push_R(const T &a) { R.push(a - add_r); }
T top_R() const { return R.top() + add_r; }
T pop_R() { T val=top_R(); R.pop(); return val;}
void push_L(const T &a) { L.push(a - add_l); }
T top_L() const { return L.top() + add_l; }
T pop_L() { T val=top_L(); L.pop(); return val;}
public:
SlopeTrick() : min_f(0), add_l(0), add_r(0) {
L.push(-INF); R.push(INF);
}
// 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) {
if (a > top_R()) {
min_f+=a-top_R(); push_L(pop_R());push_R(a);
} else {
push_L(a);
}
}
// add _/ ; f(x) += max(x - a, 0)
void add_x_minus_a(const T &a) {
if (top_L() > a) {
min_f+=top_L()-a;push_R(pop_L());push_L(a);
} else {
push_R(a);
}
}
// add \/ ; f(x) += max(x - a, 0)
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(R.size()>=2)R.pop(); }
// \/ -> _/ ; f_{new} (x) = min f(y) (y >= x)
void clear_left(){ while(L.size()>=2) L.pop(); }
// \/. -> .\/ ; f_{new} (x) = f(x - a)
void shift(const T &a) { add_l+=a; add_r+=a; }
T get(const T &x) {
T ret = min_f;
if (!L.empty() && x < top_L()) {
while(!L.empty())ret += max(T(0),pop_L()-x);
}
if (!R.empty() && top_R() < x) {
while(!R.empty())ret += max(T(0),x-pop_R());
}
return ret;
}
int size(){
return L.size() + R.size();
}
};
using stp = SlopeTrick<ll>*;
vii adj[N];
stp merge(stp a, stp b){
if(a == NULL)return b;
if(b == NULL)return a;
if(a -> size() > b -> size())swap(a, b);
while(a -> L.size() > 1)b -> add_a_minus_x(a -> pop_L());
while(a -> R.size() > 1)b -> add_x_minus_a(a -> pop_R());
b -> min_f += a -> min_f;
return b;
}
stp dfs(int ind){
stp ret = NULL;
for(auto [x, y] : adj[ind]){
stp z = dfs(x);
if(z == NULL){
z = new SlopeTrick<ll>();
z -> add_abs(y);
}else{
z -> push_L(z -> pop_L() + y);
z -> push_R(z -> pop_R() + y);
}
ret = merge(ret, z);
}
if(ret != NULL){
ll temp = ret -> top_R();
ret -> clear_right();
ret -> push_R(temp);
}
return ret;
}
void testcase(){
int n, m;
cin >> n >> m;
for(int i = 2; i <= n + m; i++){
int a, b;
cin >> a >> b;
adj[a].eb(i, b);
}
stp ans = dfs(1);
cout << ans -> min_f << "\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
//cin >> T;
for(int qq = 1; qq <= T; ++qq){
//cout << "Case #" << qq << ": ";
testcase();
}
return 0;
}
/*
6 1597352862016328480
*/
Compilation message
fireworks.cpp: In function 'SlopeTrick<long long int>* dfs(int)':
fireworks.cpp:158:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
158 | for(auto [x, y] : adj[ind]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9624 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9672 KB |
Output is correct |
6 |
Correct |
5 ms |
9608 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9736 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9624 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9672 KB |
Output is correct |
16 |
Correct |
5 ms |
9608 KB |
Output is correct |
17 |
Correct |
7 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9736 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9684 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
5 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
6 ms |
9812 KB |
Output is correct |
32 |
Correct |
6 ms |
9788 KB |
Output is correct |
33 |
Correct |
6 ms |
9940 KB |
Output is correct |
34 |
Correct |
7 ms |
9988 KB |
Output is correct |
35 |
Correct |
9 ms |
10196 KB |
Output is correct |
36 |
Correct |
7 ms |
10196 KB |
Output is correct |
37 |
Correct |
8 ms |
10196 KB |
Output is correct |
38 |
Correct |
8 ms |
10280 KB |
Output is correct |
39 |
Correct |
8 ms |
10324 KB |
Output is correct |
40 |
Correct |
7 ms |
10708 KB |
Output is correct |
41 |
Correct |
7 ms |
10668 KB |
Output is correct |
42 |
Correct |
7 ms |
9860 KB |
Output is correct |
43 |
Correct |
8 ms |
10736 KB |
Output is correct |
44 |
Correct |
8 ms |
10636 KB |
Output is correct |
45 |
Correct |
10 ms |
10580 KB |
Output is correct |
46 |
Correct |
12 ms |
10876 KB |
Output is correct |
47 |
Correct |
8 ms |
10880 KB |
Output is correct |
48 |
Correct |
7 ms |
10632 KB |
Output is correct |
49 |
Correct |
9 ms |
10756 KB |
Output is correct |
50 |
Correct |
8 ms |
10628 KB |
Output is correct |
51 |
Correct |
8 ms |
10580 KB |
Output is correct |
52 |
Correct |
9 ms |
10708 KB |
Output is correct |
53 |
Correct |
10 ms |
10708 KB |
Output is correct |
54 |
Correct |
11 ms |
10752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9624 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9672 KB |
Output is correct |
16 |
Correct |
5 ms |
9608 KB |
Output is correct |
17 |
Correct |
7 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9736 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9684 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
5 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
6 ms |
9812 KB |
Output is correct |
32 |
Correct |
6 ms |
9788 KB |
Output is correct |
33 |
Correct |
6 ms |
9940 KB |
Output is correct |
34 |
Correct |
7 ms |
9988 KB |
Output is correct |
35 |
Correct |
9 ms |
10196 KB |
Output is correct |
36 |
Correct |
7 ms |
10196 KB |
Output is correct |
37 |
Correct |
8 ms |
10196 KB |
Output is correct |
38 |
Correct |
8 ms |
10280 KB |
Output is correct |
39 |
Correct |
8 ms |
10324 KB |
Output is correct |
40 |
Correct |
7 ms |
10708 KB |
Output is correct |
41 |
Correct |
7 ms |
10668 KB |
Output is correct |
42 |
Correct |
7 ms |
9860 KB |
Output is correct |
43 |
Correct |
8 ms |
10736 KB |
Output is correct |
44 |
Correct |
8 ms |
10636 KB |
Output is correct |
45 |
Correct |
10 ms |
10580 KB |
Output is correct |
46 |
Correct |
12 ms |
10876 KB |
Output is correct |
47 |
Correct |
8 ms |
10880 KB |
Output is correct |
48 |
Correct |
7 ms |
10632 KB |
Output is correct |
49 |
Correct |
9 ms |
10756 KB |
Output is correct |
50 |
Correct |
8 ms |
10628 KB |
Output is correct |
51 |
Correct |
8 ms |
10580 KB |
Output is correct |
52 |
Correct |
9 ms |
10708 KB |
Output is correct |
53 |
Correct |
10 ms |
10708 KB |
Output is correct |
54 |
Correct |
11 ms |
10752 KB |
Output is correct |
55 |
Correct |
12 ms |
11272 KB |
Output is correct |
56 |
Correct |
30 ms |
15676 KB |
Output is correct |
57 |
Correct |
63 ms |
19940 KB |
Output is correct |
58 |
Correct |
71 ms |
22736 KB |
Output is correct |
59 |
Correct |
104 ms |
27076 KB |
Output is correct |
60 |
Correct |
128 ms |
31356 KB |
Output is correct |
61 |
Correct |
185 ms |
34368 KB |
Output is correct |
62 |
Correct |
152 ms |
36980 KB |
Output is correct |
63 |
Correct |
190 ms |
42340 KB |
Output is correct |
64 |
Correct |
213 ms |
44044 KB |
Output is correct |
65 |
Correct |
132 ms |
70968 KB |
Output is correct |
66 |
Correct |
119 ms |
70896 KB |
Output is correct |
67 |
Correct |
102 ms |
24232 KB |
Output is correct |
68 |
Correct |
169 ms |
71860 KB |
Output is correct |
69 |
Correct |
209 ms |
69020 KB |
Output is correct |
70 |
Correct |
198 ms |
68900 KB |
Output is correct |
71 |
Correct |
222 ms |
83332 KB |
Output is correct |
72 |
Correct |
224 ms |
83256 KB |
Output is correct |
73 |
Correct |
207 ms |
79796 KB |
Output is correct |
74 |
Correct |
215 ms |
79988 KB |
Output is correct |
75 |
Correct |
220 ms |
78156 KB |
Output is correct |
76 |
Correct |
213 ms |
78280 KB |
Output is correct |
77 |
Correct |
220 ms |
75732 KB |
Output is correct |
78 |
Correct |
230 ms |
74780 KB |
Output is correct |
79 |
Correct |
210 ms |
74248 KB |
Output is correct |