//khodaya khodet komak kon
# include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, ll> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define lc id << 1
# define rc id << 1 | 1
# define fast_io ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 2e5 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const ld eps = 1e-15;
const int mod = 998244353;
const int base = 257;
int n, a[xn], f[xn], c[xn], sz[xn], hvy[xn], ptr;
ll lazy[xn << 2];
ll sum, ans, seg[xn << 2], cnt[xn], mn[xn << 2], lz[xn << 2];
vector <int> adj[xn], vec, cprs;
vector <ppi> Q[xn];
deque <int> dq;
map <int, int> mp;
bool mark[xn];
void shift(int id, int l, int r){
seg[id] += lz[id];
mn[id] += lz[id];
if (lazy[id] + 1)
seg[id] = mn[id] = lazy[id];
if (1 < r - l){
if (lazy[lc] + 1)
lazy[lc] += lz[id];
else
lz[lc] += lz[id];
if (lazy[id] + 1)
lazy[lc] = lazy[id];
if (lazy[rc] + 1)
lazy[rc] += lz[id];
else
lz[rc] += lz[id];
if (lazy[id] + 1)
lazy[rc] = lazy[id];
}
lazy[id] = - 1;
lz[id] = 0;
}
void update(int id, int l, int r, int ql, int qr, ll val){
shift(id, l, r);
if (qr <= l || r <= ql || qr <= ql || val <= mn[id])
return;
if (ql <= l && r <= qr && seg[id] <= val){
lazy[id] = val;
shift(id, l, r);
return;
}
int mid = l + r >> 1;
update(lc, l, mid, ql, qr, val);
update(rc, mid, r, ql, qr, val);
seg[id] = max(seg[lc], seg[rc]);
mn[id] = min(mn[lc], mn[rc]);
}
void upd(int id, int l, int r, int ql, int qr, ll val){
shift(id, l, r);
if (qr <= l || r <= ql || qr <= ql)
return;
if (ql <= l && r <= qr){
if (lazy[id] + 1)
lazy[id] += val;
else
lz[id] += val;
shift(id, l, r);
return;
}
int mid = l + r >> 1;
upd(lc, l, mid, ql, qr, val);
upd(rc, mid, r, ql, qr, val);
seg[id] = max(seg[lc], seg[rc]);
mn[id] = min(mn[lc], mn[rc]);
}
ll get(int id, int l, int r, int pos){
shift(id, l, r);
if (r - l == 1)
return seg[id];
int mid = l + r >> 1;
if (pos < mid)
return get(lc, l, mid, pos);
else
return get(rc, mid, r, pos);
}
void find_cyc(int v){
dq.push_back(v);
if (mark[v])
return;
mark[v] = true;
find_cyc(f[v]);
}
void preDFS(int v){
sz[v] = 1;
hvy[v] = n + 1;
for (int u : adj[v]){
preDFS(u);
sz[v] += sz[u];
if (sz[hvy[v]] < sz[u])
hvy[v] = u;
}
}
void gooni(int v, bool f = 0){
if (f)
mark[v] = true;
vec.push_back(a[v]);
for (int u : adj[v])
gooni(u, f);
}
void DFS(int v){
Q[v].clear();
for (int u : adj[v]){
if (u == hvy[v])
continue;
DFS(u);
vec.clear();
gooni(u);
int last = 0;
sort(all(vec));
for (int x : vec){
if (last < x){
ll y = get(1, 1, ptr + 1, x);
Q[v].push_back({{last + 1, x}, y});
last = x;
}
}
lazy[1] = 0;
}
if (hvy[v] <= n)
DFS(hvy[v]);
for (ppi x : Q[v])
upd(1, 1, ptr + 1, x.A.A, x.A.B + 1, x.B);
ll x = get(1, 1, ptr + 1, a[v]) + c[v];
update(1, 1, ptr + 1, 1, a[v] + 1, x);
}
int main(){
fast_io;
cin >> n;
for (int i = 1; i <= n; ++ i){
cin >> f[i] >> a[i] >> c[i];
adj[f[i]].push_back(i);
cprs.push_back(a[i]);
sum += c[i];
}
sort(all(cprs));
for (int i = 0; i < SZ(cprs); ++ i)
if (!i || cprs[i] != cprs[i - 1])
mp[cprs[i]] = ++ ptr;
for (int i = 1; i <= n; ++ i)
a[i] = mp[a[i]];
fill(lazy, lazy + (xn << 2), - 1);
for (int i = 1; i <= n; ++ i){
if (mark[i])
continue;
dq.clear();
find_cyc(i);
while (dq.front() != dq.back())
mark[dq.front()] = false, dq.pop_front();
dq.pop_front();
adj[0].clear();
for (int v : dq)
for (int u : adj[v])
if (!mark[u])
adj[0].push_back(u);
preDFS(0), DFS(0);
ll mx = get(1, 1, ptr + 1, 1);
for (int x : dq){
cnt[a[x]] += c[x];
mx = max(mx, get(1, 1, ptr + 1, a[x]) + cnt[a[x]]);
}
ans += mx;
for (int x : dq)
cnt[a[x]] -= c[x];
vec.clear();
gooni(0, 1);
lazy[1] = 0;
}
cout << sum - ans << endl;
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'void update(int, int, int, int, int, ll)':
worst_reporter2.cpp:78:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | int mid = l + r >> 1;
| ~~^~~
worst_reporter2.cpp: In function 'void upd(int, int, int, int, int, ll)':
worst_reporter2.cpp:96:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
96 | int mid = l + r >> 1;
| ~~^~~
worst_reporter2.cpp: In function 'll get(int, int, int, int)':
worst_reporter2.cpp:106:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
106 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15948 KB |
Output is correct |
2 |
Correct |
8 ms |
15948 KB |
Output is correct |
3 |
Correct |
7 ms |
15948 KB |
Output is correct |
4 |
Correct |
8 ms |
15948 KB |
Output is correct |
5 |
Correct |
23 ms |
17152 KB |
Output is correct |
6 |
Correct |
17 ms |
16420 KB |
Output is correct |
7 |
Correct |
13 ms |
16332 KB |
Output is correct |
8 |
Correct |
24 ms |
17108 KB |
Output is correct |
9 |
Correct |
16 ms |
16460 KB |
Output is correct |
10 |
Correct |
13 ms |
16332 KB |
Output is correct |
11 |
Correct |
11 ms |
16332 KB |
Output is correct |
12 |
Correct |
15 ms |
17684 KB |
Output is correct |
13 |
Correct |
11 ms |
16972 KB |
Output is correct |
14 |
Correct |
17 ms |
17348 KB |
Output is correct |
15 |
Correct |
12 ms |
16716 KB |
Output is correct |
16 |
Correct |
30 ms |
17252 KB |
Output is correct |
17 |
Correct |
17 ms |
16544 KB |
Output is correct |
18 |
Correct |
11 ms |
16284 KB |
Output is correct |
19 |
Correct |
15 ms |
17336 KB |
Output is correct |
20 |
Correct |
13 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
16 ms |
16972 KB |
Output is correct |
23 |
Correct |
13 ms |
16332 KB |
Output is correct |
24 |
Correct |
20 ms |
17444 KB |
Output is correct |
25 |
Correct |
13 ms |
16676 KB |
Output is correct |
26 |
Correct |
13 ms |
17668 KB |
Output is correct |
27 |
Correct |
15 ms |
17228 KB |
Output is correct |
28 |
Correct |
15 ms |
17356 KB |
Output is correct |
29 |
Correct |
16 ms |
17552 KB |
Output is correct |
30 |
Correct |
15 ms |
17356 KB |
Output is correct |
31 |
Correct |
15 ms |
17388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15948 KB |
Output is correct |
2 |
Correct |
8 ms |
15948 KB |
Output is correct |
3 |
Correct |
7 ms |
15948 KB |
Output is correct |
4 |
Correct |
8 ms |
15948 KB |
Output is correct |
5 |
Correct |
23 ms |
17152 KB |
Output is correct |
6 |
Correct |
17 ms |
16420 KB |
Output is correct |
7 |
Correct |
13 ms |
16332 KB |
Output is correct |
8 |
Correct |
24 ms |
17108 KB |
Output is correct |
9 |
Correct |
16 ms |
16460 KB |
Output is correct |
10 |
Correct |
13 ms |
16332 KB |
Output is correct |
11 |
Correct |
11 ms |
16332 KB |
Output is correct |
12 |
Correct |
15 ms |
17684 KB |
Output is correct |
13 |
Correct |
11 ms |
16972 KB |
Output is correct |
14 |
Correct |
17 ms |
17348 KB |
Output is correct |
15 |
Correct |
12 ms |
16716 KB |
Output is correct |
16 |
Correct |
30 ms |
17252 KB |
Output is correct |
17 |
Correct |
17 ms |
16544 KB |
Output is correct |
18 |
Correct |
11 ms |
16284 KB |
Output is correct |
19 |
Correct |
15 ms |
17336 KB |
Output is correct |
20 |
Correct |
13 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
16 ms |
16972 KB |
Output is correct |
23 |
Correct |
13 ms |
16332 KB |
Output is correct |
24 |
Correct |
20 ms |
17444 KB |
Output is correct |
25 |
Correct |
13 ms |
16676 KB |
Output is correct |
26 |
Correct |
13 ms |
17668 KB |
Output is correct |
27 |
Correct |
15 ms |
17228 KB |
Output is correct |
28 |
Correct |
15 ms |
17356 KB |
Output is correct |
29 |
Correct |
16 ms |
17552 KB |
Output is correct |
30 |
Correct |
15 ms |
17356 KB |
Output is correct |
31 |
Correct |
15 ms |
17388 KB |
Output is correct |
32 |
Correct |
24 ms |
17160 KB |
Output is correct |
33 |
Correct |
1467 ms |
64844 KB |
Output is correct |
34 |
Correct |
581 ms |
37932 KB |
Output is correct |
35 |
Correct |
1320 ms |
63844 KB |
Output is correct |
36 |
Correct |
615 ms |
37840 KB |
Output is correct |
37 |
Correct |
239 ms |
30148 KB |
Output is correct |
38 |
Correct |
176 ms |
28240 KB |
Output is correct |
39 |
Correct |
491 ms |
78508 KB |
Output is correct |
40 |
Correct |
211 ms |
56892 KB |
Output is correct |
41 |
Correct |
125 ms |
56792 KB |
Output is correct |
42 |
Correct |
557 ms |
66256 KB |
Output is correct |
43 |
Correct |
211 ms |
42804 KB |
Output is correct |
44 |
Correct |
1930 ms |
73344 KB |
Output is correct |
45 |
Correct |
708 ms |
43140 KB |
Output is correct |
46 |
Correct |
112 ms |
28336 KB |
Output is correct |
47 |
Correct |
513 ms |
64332 KB |
Output is correct |
48 |
Correct |
227 ms |
42888 KB |
Output is correct |
49 |
Correct |
126 ms |
42668 KB |
Output is correct |
50 |
Correct |
563 ms |
49012 KB |
Output is correct |
51 |
Correct |
248 ms |
27480 KB |
Output is correct |
52 |
Correct |
495 ms |
63512 KB |
Output is correct |
53 |
Correct |
229 ms |
42128 KB |
Output is correct |
54 |
Correct |
264 ms |
78504 KB |
Output is correct |
55 |
Correct |
527 ms |
64016 KB |
Output is correct |
56 |
Correct |
440 ms |
71484 KB |
Output is correct |
57 |
Correct |
413 ms |
74172 KB |
Output is correct |
58 |
Correct |
385 ms |
66300 KB |
Output is correct |
59 |
Correct |
378 ms |
66244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15948 KB |
Output is correct |
2 |
Correct |
8 ms |
15948 KB |
Output is correct |
3 |
Correct |
7 ms |
15948 KB |
Output is correct |
4 |
Correct |
8 ms |
15948 KB |
Output is correct |
5 |
Correct |
23 ms |
17152 KB |
Output is correct |
6 |
Correct |
17 ms |
16420 KB |
Output is correct |
7 |
Correct |
13 ms |
16332 KB |
Output is correct |
8 |
Correct |
24 ms |
17108 KB |
Output is correct |
9 |
Correct |
16 ms |
16460 KB |
Output is correct |
10 |
Correct |
13 ms |
16332 KB |
Output is correct |
11 |
Correct |
11 ms |
16332 KB |
Output is correct |
12 |
Correct |
15 ms |
17684 KB |
Output is correct |
13 |
Correct |
11 ms |
16972 KB |
Output is correct |
14 |
Correct |
17 ms |
17348 KB |
Output is correct |
15 |
Correct |
12 ms |
16716 KB |
Output is correct |
16 |
Correct |
30 ms |
17252 KB |
Output is correct |
17 |
Correct |
17 ms |
16544 KB |
Output is correct |
18 |
Correct |
11 ms |
16284 KB |
Output is correct |
19 |
Correct |
15 ms |
17336 KB |
Output is correct |
20 |
Correct |
13 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
16 ms |
16972 KB |
Output is correct |
23 |
Correct |
13 ms |
16332 KB |
Output is correct |
24 |
Correct |
20 ms |
17444 KB |
Output is correct |
25 |
Correct |
13 ms |
16676 KB |
Output is correct |
26 |
Correct |
13 ms |
17668 KB |
Output is correct |
27 |
Correct |
15 ms |
17228 KB |
Output is correct |
28 |
Correct |
15 ms |
17356 KB |
Output is correct |
29 |
Correct |
16 ms |
17552 KB |
Output is correct |
30 |
Correct |
15 ms |
17356 KB |
Output is correct |
31 |
Correct |
15 ms |
17388 KB |
Output is correct |
32 |
Correct |
24 ms |
17160 KB |
Output is correct |
33 |
Correct |
1467 ms |
64844 KB |
Output is correct |
34 |
Correct |
581 ms |
37932 KB |
Output is correct |
35 |
Correct |
1320 ms |
63844 KB |
Output is correct |
36 |
Correct |
615 ms |
37840 KB |
Output is correct |
37 |
Correct |
239 ms |
30148 KB |
Output is correct |
38 |
Correct |
176 ms |
28240 KB |
Output is correct |
39 |
Correct |
491 ms |
78508 KB |
Output is correct |
40 |
Correct |
211 ms |
56892 KB |
Output is correct |
41 |
Correct |
125 ms |
56792 KB |
Output is correct |
42 |
Correct |
557 ms |
66256 KB |
Output is correct |
43 |
Correct |
211 ms |
42804 KB |
Output is correct |
44 |
Correct |
1930 ms |
73344 KB |
Output is correct |
45 |
Correct |
708 ms |
43140 KB |
Output is correct |
46 |
Correct |
112 ms |
28336 KB |
Output is correct |
47 |
Correct |
513 ms |
64332 KB |
Output is correct |
48 |
Correct |
227 ms |
42888 KB |
Output is correct |
49 |
Correct |
126 ms |
42668 KB |
Output is correct |
50 |
Correct |
563 ms |
49012 KB |
Output is correct |
51 |
Correct |
248 ms |
27480 KB |
Output is correct |
52 |
Correct |
495 ms |
63512 KB |
Output is correct |
53 |
Correct |
229 ms |
42128 KB |
Output is correct |
54 |
Correct |
264 ms |
78504 KB |
Output is correct |
55 |
Correct |
527 ms |
64016 KB |
Output is correct |
56 |
Correct |
440 ms |
71484 KB |
Output is correct |
57 |
Correct |
413 ms |
74172 KB |
Output is correct |
58 |
Correct |
385 ms |
66300 KB |
Output is correct |
59 |
Correct |
378 ms |
66244 KB |
Output is correct |
60 |
Correct |
9 ms |
15948 KB |
Output is correct |
61 |
Correct |
10 ms |
16032 KB |
Output is correct |
62 |
Correct |
8 ms |
15996 KB |
Output is correct |
63 |
Correct |
10 ms |
15996 KB |
Output is correct |
64 |
Correct |
1276 ms |
67952 KB |
Output is correct |
65 |
Correct |
553 ms |
40600 KB |
Output is correct |
66 |
Correct |
1301 ms |
66848 KB |
Output is correct |
67 |
Correct |
568 ms |
41928 KB |
Output is correct |
68 |
Correct |
268 ms |
35008 KB |
Output is correct |
69 |
Correct |
210 ms |
32204 KB |
Output is correct |
70 |
Correct |
364 ms |
54648 KB |
Output is correct |
71 |
Correct |
159 ms |
31152 KB |
Output is correct |
72 |
Correct |
339 ms |
54844 KB |
Output is correct |
73 |
Correct |
154 ms |
31624 KB |
Output is correct |
74 |
Correct |
512 ms |
55812 KB |
Output is correct |
75 |
Correct |
246 ms |
33052 KB |
Output is correct |
76 |
Correct |
143 ms |
32944 KB |
Output is correct |
77 |
Correct |
477 ms |
56184 KB |
Output is correct |
78 |
Correct |
209 ms |
33460 KB |
Output is correct |
79 |
Correct |
890 ms |
63436 KB |
Output is correct |
80 |
Correct |
375 ms |
38132 KB |
Output is correct |
81 |
Correct |
197 ms |
34228 KB |
Output is correct |
82 |
Correct |
275 ms |
54952 KB |
Output is correct |
83 |
Correct |
347 ms |
53944 KB |
Output is correct |
84 |
Correct |
517 ms |
63376 KB |
Output is correct |
85 |
Correct |
470 ms |
63340 KB |
Output is correct |
86 |
Correct |
432 ms |
60564 KB |
Output is correct |
87 |
Correct |
453 ms |
63288 KB |
Output is correct |
88 |
Correct |
453 ms |
63468 KB |
Output is correct |