// ~Be name khoda~ //
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define cl clear
#define endll '\n'
const int maxn = 1e6 + 10;
const int maxn5 = 2e5 + 10;
const int maxnt = 1e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 2e18;
struct seg{
ll set, add;
seg(){
set = -1;
add = 0;
}
inline void clear(){
set = -1;
add = 0;
}
}node[maxnt];
int m, ver[maxn5], no = 1, n;
vector <pair<int, ll>> have[maxn5];
vector <int> ex, ch, adj[maxn5], av, incy;
int sz[maxn5], big[maxn5], st[maxn5], ft[maxn5];
int ti = -1, h[maxn5], mark[maxn5];
ll c[maxn5], cnt[maxn5];
bool cy[maxn5];
inline void shift(int v){
if(node[v].set != -1){
node[v * 2].set = node[v * 2 + 1].set = node[v].set;
node[v].set = -1;
node[v * 2].add = node[v * 2 + 1].add = 0;
}
node[v * 2].add += node[v].add;
node[v * 2 + 1].add += node[v].add;
node[v].add = 0;
ch.pb(v * 2);
ch.pb(v * 2 + 1);
return;
}
inline void sset(int l, int r, int lq, int rq, ll val, int v){
if(rq <= l || r <= lq)
return;
if(lq <= l && r <= rq){
node[v].set = val;
node[v].add = 0;
return;
}
int mid = (l + r) >> 1; shift(v);
sset(l, mid, lq, rq, val, v * 2);
sset(mid, r, lq, rq, val, v * 2 + 1);
return;
}
inline void add(int l, int r, int lq, int rq, ll val, int v){
if(rq <= l || r <= lq)
return;
if(lq <= l && r <= rq){
node[v].add += val;
return;
}
int mid = (l + r) >> 1; shift(v);
add(l, mid, lq, rq, val, v * 2);
add(mid, r, lq, rq, val, v * 2 + 1);
return;
}
inline ll get(int l, int r, int ind, int v){
if(node[v].set > -1)
return node[v].set + node[v].add;
if(r - l == 1)
return node[v].add;
int mid = (l + r) >> 1;
if(ind < mid)
return get(l, mid, ind, v * 2) + node[v].add;
return get(mid, r, ind, v * 2 + 1) + node[v].add;
}
inline void upd(int id, ll val){
int lo = -1, hi = id + 1;
while(hi - lo > 1){
int mid = (lo + hi) >> 1;
if(get(0, m, mid, 1) < val)
hi = mid;
else
lo = mid;
}
if(hi > id)
return;
sset(0, m, hi, id + 1, val, 1);
//cout << "found out " << id << ' ' << val << ' ' << hi << endl;
return;
}
inline void dfs_det(int v){
sz[v] = 1;
big[v] = -1;
st[v] = ++ti;
ver[ti] = v;
for(auto u : adj[v]){
dfs_det(u);
sz[v] += sz[u];
if(big[v] == -1 || sz[u] >= sz[big[v]])
big[v] = u;
}
ft[v] = ti;
return;
}
inline void dfs(int v, bool keep){
for(auto u : adj[v]) if(big[v] != u)
dfs(u, false);
if(big[v] == -1){
if(keep){
sset(0, m, 0, h[v] + 1, c[v], 1);
}
else{
have[v].pb({0, c[v]});
if(h[v] + 1 < m)
have[v].pb({h[v] + 1, 0});
}
return;
}
dfs(big[v], true);
for(auto u : adj[v]) if(u != big[v]){
for(int i = 0; i < have[u].size(); i++){
int st = have[u][i].fi;
ll val = have[u][i].se;
int ft = m;
if(i + 1 < have[u].size())
ft = have[u][i + 1].fi;
add(0, m, st, ft, val, 1);
}
have[u].clear();
}
if(v == n)
return;
ll me = get(0, m, h[v], 1) + c[v];
upd(h[v], me);
if(keep) return;
av.clear();
for(int i = st[v]; i <= ft[v]; i++){
int u = ver[i];
av.pb(h[u]);
if(h[u] + 1 < m)
av.pb(h[u] + 1);
}
av.pb(0);
sort(all(av));
av.resize(unique(all(av)) - av.begin());
for(auto id : av){
have[v].pb({id, get(0, m, id, 1)});
}
while(!ch.empty()){
int u = ch.back();
ch.pop_back();
node[u].clear();
}
node[1].clear();
return;
}
inline bool dfs_cmp(int v){
cy[v] = false;
mark[v] = no;
for(auto u : adj[v]){
if(!mark[u]){
cy[v] |= dfs_cmp(u);
}
else if(mark[u] == mark[v])
cy[v] = true;
}
if(cy[v]) incy.pb(v);
return cy[v];
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n;
ll sum = 0;
for(int i = 0; i < n; i++){
int a; cin >> a >> h[i] >> c[i];
a--;
adj[a].pb(i);
ex.pb(h[i]);
sum += c[i];
}
sort(all(ex));
ex.resize(unique(all(ex)) - ex.begin());
for(int i = 0; i < n; i++){
h[i] = lower_bound(all(ex), h[i]) - ex.begin();
}
m = ex.size();
ll out = 0;
for(int i = 0; i < n; i++) if(!mark[i]){
incy.clear();
dfs_cmp(i);
if(!incy.empty()){
while(!ch.empty()){
int u = ch.back();
ch.pop_back();
node[u].clear();
}
node[1].clear();
ti = -1;
adj[n].clear();
have[n].clear();
ll ans = 0;
for(auto u : incy){
cnt[h[u]] += c[u];
for(auto z : adj[u]) if(!cy[z])
adj[n].pb(z);
}
dfs_det(n);
dfs(n, true);
ans = get(0, m, 0, 1);
for(auto u : incy){
ans = max(ans, get(0, m, h[u], 1) + cnt[h[u]]);
cnt[h[u]] = 0;
}
out += ans;
}
no++;
}
cout << sum - out << endl;
}
Compilation message
worst_reporter2.cpp: In function 'void dfs(int, bool)':
worst_reporter2.cpp:146:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
146 | for(int i = 0; i < have[u].size(); i++){
| ~~^~~~~~~~~~~~~~~~
worst_reporter2.cpp:150:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | if(i + 1 < have[u].size())
| ~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
25372 KB |
Output is correct |
2 |
Correct |
12 ms |
25392 KB |
Output is correct |
3 |
Correct |
13 ms |
25376 KB |
Output is correct |
4 |
Correct |
12 ms |
25348 KB |
Output is correct |
5 |
Correct |
30 ms |
28476 KB |
Output is correct |
6 |
Correct |
20 ms |
26408 KB |
Output is correct |
7 |
Correct |
17 ms |
26060 KB |
Output is correct |
8 |
Correct |
39 ms |
27564 KB |
Output is correct |
9 |
Correct |
23 ms |
26308 KB |
Output is correct |
10 |
Correct |
16 ms |
25992 KB |
Output is correct |
11 |
Correct |
14 ms |
25808 KB |
Output is correct |
12 |
Correct |
20 ms |
28700 KB |
Output is correct |
13 |
Correct |
15 ms |
27852 KB |
Output is correct |
14 |
Correct |
21 ms |
27792 KB |
Output is correct |
15 |
Correct |
19 ms |
26996 KB |
Output is correct |
16 |
Correct |
35 ms |
27668 KB |
Output is correct |
17 |
Correct |
22 ms |
26428 KB |
Output is correct |
18 |
Correct |
16 ms |
25880 KB |
Output is correct |
19 |
Correct |
24 ms |
27844 KB |
Output is correct |
20 |
Correct |
18 ms |
27296 KB |
Output is correct |
21 |
Correct |
16 ms |
26904 KB |
Output is correct |
22 |
Correct |
17 ms |
27168 KB |
Output is correct |
23 |
Correct |
16 ms |
26648 KB |
Output is correct |
24 |
Correct |
20 ms |
27848 KB |
Output is correct |
25 |
Correct |
17 ms |
27368 KB |
Output is correct |
26 |
Correct |
20 ms |
28200 KB |
Output is correct |
27 |
Correct |
25 ms |
27668 KB |
Output is correct |
28 |
Correct |
21 ms |
27964 KB |
Output is correct |
29 |
Correct |
21 ms |
28332 KB |
Output is correct |
30 |
Correct |
20 ms |
27304 KB |
Output is correct |
31 |
Correct |
21 ms |
27336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
25372 KB |
Output is correct |
2 |
Correct |
12 ms |
25392 KB |
Output is correct |
3 |
Correct |
13 ms |
25376 KB |
Output is correct |
4 |
Correct |
12 ms |
25348 KB |
Output is correct |
5 |
Correct |
30 ms |
28476 KB |
Output is correct |
6 |
Correct |
20 ms |
26408 KB |
Output is correct |
7 |
Correct |
17 ms |
26060 KB |
Output is correct |
8 |
Correct |
39 ms |
27564 KB |
Output is correct |
9 |
Correct |
23 ms |
26308 KB |
Output is correct |
10 |
Correct |
16 ms |
25992 KB |
Output is correct |
11 |
Correct |
14 ms |
25808 KB |
Output is correct |
12 |
Correct |
20 ms |
28700 KB |
Output is correct |
13 |
Correct |
15 ms |
27852 KB |
Output is correct |
14 |
Correct |
21 ms |
27792 KB |
Output is correct |
15 |
Correct |
19 ms |
26996 KB |
Output is correct |
16 |
Correct |
35 ms |
27668 KB |
Output is correct |
17 |
Correct |
22 ms |
26428 KB |
Output is correct |
18 |
Correct |
16 ms |
25880 KB |
Output is correct |
19 |
Correct |
24 ms |
27844 KB |
Output is correct |
20 |
Correct |
18 ms |
27296 KB |
Output is correct |
21 |
Correct |
16 ms |
26904 KB |
Output is correct |
22 |
Correct |
17 ms |
27168 KB |
Output is correct |
23 |
Correct |
16 ms |
26648 KB |
Output is correct |
24 |
Correct |
20 ms |
27848 KB |
Output is correct |
25 |
Correct |
17 ms |
27368 KB |
Output is correct |
26 |
Correct |
20 ms |
28200 KB |
Output is correct |
27 |
Correct |
25 ms |
27668 KB |
Output is correct |
28 |
Correct |
21 ms |
27964 KB |
Output is correct |
29 |
Correct |
21 ms |
28332 KB |
Output is correct |
30 |
Correct |
20 ms |
27304 KB |
Output is correct |
31 |
Correct |
21 ms |
27336 KB |
Output is correct |
32 |
Correct |
30 ms |
27448 KB |
Output is correct |
33 |
Correct |
1402 ms |
139800 KB |
Output is correct |
34 |
Correct |
689 ms |
65800 KB |
Output is correct |
35 |
Correct |
1329 ms |
137900 KB |
Output is correct |
36 |
Correct |
652 ms |
66236 KB |
Output is correct |
37 |
Correct |
248 ms |
45028 KB |
Output is correct |
38 |
Correct |
179 ms |
40728 KB |
Output is correct |
39 |
Correct |
532 ms |
171960 KB |
Output is correct |
40 |
Correct |
305 ms |
139220 KB |
Output is correct |
41 |
Correct |
143 ms |
110372 KB |
Output is correct |
42 |
Correct |
594 ms |
146208 KB |
Output is correct |
43 |
Correct |
293 ms |
90012 KB |
Output is correct |
44 |
Correct |
1916 ms |
153648 KB |
Output is correct |
45 |
Correct |
766 ms |
69688 KB |
Output is correct |
46 |
Correct |
114 ms |
40756 KB |
Output is correct |
47 |
Correct |
481 ms |
140656 KB |
Output is correct |
48 |
Correct |
259 ms |
107884 KB |
Output is correct |
49 |
Correct |
140 ms |
82956 KB |
Output is correct |
50 |
Correct |
278 ms |
111148 KB |
Output is correct |
51 |
Correct |
175 ms |
78144 KB |
Output is correct |
52 |
Correct |
493 ms |
141140 KB |
Output is correct |
53 |
Correct |
237 ms |
108304 KB |
Output is correct |
54 |
Correct |
449 ms |
139304 KB |
Output is correct |
55 |
Correct |
416 ms |
140948 KB |
Output is correct |
56 |
Correct |
395 ms |
156344 KB |
Output is correct |
57 |
Correct |
402 ms |
162568 KB |
Output is correct |
58 |
Correct |
553 ms |
110616 KB |
Output is correct |
59 |
Correct |
552 ms |
110608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
25372 KB |
Output is correct |
2 |
Correct |
12 ms |
25392 KB |
Output is correct |
3 |
Correct |
13 ms |
25376 KB |
Output is correct |
4 |
Correct |
12 ms |
25348 KB |
Output is correct |
5 |
Correct |
30 ms |
28476 KB |
Output is correct |
6 |
Correct |
20 ms |
26408 KB |
Output is correct |
7 |
Correct |
17 ms |
26060 KB |
Output is correct |
8 |
Correct |
39 ms |
27564 KB |
Output is correct |
9 |
Correct |
23 ms |
26308 KB |
Output is correct |
10 |
Correct |
16 ms |
25992 KB |
Output is correct |
11 |
Correct |
14 ms |
25808 KB |
Output is correct |
12 |
Correct |
20 ms |
28700 KB |
Output is correct |
13 |
Correct |
15 ms |
27852 KB |
Output is correct |
14 |
Correct |
21 ms |
27792 KB |
Output is correct |
15 |
Correct |
19 ms |
26996 KB |
Output is correct |
16 |
Correct |
35 ms |
27668 KB |
Output is correct |
17 |
Correct |
22 ms |
26428 KB |
Output is correct |
18 |
Correct |
16 ms |
25880 KB |
Output is correct |
19 |
Correct |
24 ms |
27844 KB |
Output is correct |
20 |
Correct |
18 ms |
27296 KB |
Output is correct |
21 |
Correct |
16 ms |
26904 KB |
Output is correct |
22 |
Correct |
17 ms |
27168 KB |
Output is correct |
23 |
Correct |
16 ms |
26648 KB |
Output is correct |
24 |
Correct |
20 ms |
27848 KB |
Output is correct |
25 |
Correct |
17 ms |
27368 KB |
Output is correct |
26 |
Correct |
20 ms |
28200 KB |
Output is correct |
27 |
Correct |
25 ms |
27668 KB |
Output is correct |
28 |
Correct |
21 ms |
27964 KB |
Output is correct |
29 |
Correct |
21 ms |
28332 KB |
Output is correct |
30 |
Correct |
20 ms |
27304 KB |
Output is correct |
31 |
Correct |
21 ms |
27336 KB |
Output is correct |
32 |
Correct |
30 ms |
27448 KB |
Output is correct |
33 |
Correct |
1402 ms |
139800 KB |
Output is correct |
34 |
Correct |
689 ms |
65800 KB |
Output is correct |
35 |
Correct |
1329 ms |
137900 KB |
Output is correct |
36 |
Correct |
652 ms |
66236 KB |
Output is correct |
37 |
Correct |
248 ms |
45028 KB |
Output is correct |
38 |
Correct |
179 ms |
40728 KB |
Output is correct |
39 |
Correct |
532 ms |
171960 KB |
Output is correct |
40 |
Correct |
305 ms |
139220 KB |
Output is correct |
41 |
Correct |
143 ms |
110372 KB |
Output is correct |
42 |
Correct |
594 ms |
146208 KB |
Output is correct |
43 |
Correct |
293 ms |
90012 KB |
Output is correct |
44 |
Correct |
1916 ms |
153648 KB |
Output is correct |
45 |
Correct |
766 ms |
69688 KB |
Output is correct |
46 |
Correct |
114 ms |
40756 KB |
Output is correct |
47 |
Correct |
481 ms |
140656 KB |
Output is correct |
48 |
Correct |
259 ms |
107884 KB |
Output is correct |
49 |
Correct |
140 ms |
82956 KB |
Output is correct |
50 |
Correct |
278 ms |
111148 KB |
Output is correct |
51 |
Correct |
175 ms |
78144 KB |
Output is correct |
52 |
Correct |
493 ms |
141140 KB |
Output is correct |
53 |
Correct |
237 ms |
108304 KB |
Output is correct |
54 |
Correct |
449 ms |
139304 KB |
Output is correct |
55 |
Correct |
416 ms |
140948 KB |
Output is correct |
56 |
Correct |
395 ms |
156344 KB |
Output is correct |
57 |
Correct |
402 ms |
162568 KB |
Output is correct |
58 |
Correct |
553 ms |
110616 KB |
Output is correct |
59 |
Correct |
552 ms |
110608 KB |
Output is correct |
60 |
Correct |
13 ms |
25364 KB |
Output is correct |
61 |
Correct |
14 ms |
25420 KB |
Output is correct |
62 |
Correct |
15 ms |
25432 KB |
Output is correct |
63 |
Correct |
12 ms |
25356 KB |
Output is correct |
64 |
Correct |
1300 ms |
137440 KB |
Output is correct |
65 |
Correct |
650 ms |
65348 KB |
Output is correct |
66 |
Correct |
1228 ms |
133072 KB |
Output is correct |
67 |
Correct |
609 ms |
70224 KB |
Output is correct |
68 |
Correct |
261 ms |
48472 KB |
Output is correct |
69 |
Correct |
218 ms |
44408 KB |
Output is correct |
70 |
Correct |
237 ms |
47264 KB |
Output is correct |
71 |
Correct |
172 ms |
44304 KB |
Output is correct |
72 |
Correct |
217 ms |
48152 KB |
Output is correct |
73 |
Correct |
191 ms |
47204 KB |
Output is correct |
74 |
Correct |
303 ms |
83708 KB |
Output is correct |
75 |
Correct |
265 ms |
65696 KB |
Output is correct |
76 |
Correct |
179 ms |
53048 KB |
Output is correct |
77 |
Correct |
286 ms |
84116 KB |
Output is correct |
78 |
Correct |
207 ms |
66004 KB |
Output is correct |
79 |
Correct |
825 ms |
97464 KB |
Output is correct |
80 |
Correct |
435 ms |
61960 KB |
Output is correct |
81 |
Correct |
256 ms |
49508 KB |
Output is correct |
82 |
Correct |
220 ms |
48536 KB |
Output is correct |
83 |
Correct |
250 ms |
42420 KB |
Output is correct |
84 |
Correct |
445 ms |
96004 KB |
Output is correct |
85 |
Correct |
430 ms |
95944 KB |
Output is correct |
86 |
Correct |
445 ms |
95680 KB |
Output is correct |
87 |
Correct |
416 ms |
96060 KB |
Output is correct |
88 |
Correct |
439 ms |
96080 KB |
Output is correct |