//khodaya khodet komak kon
# include <bits/stdc++.h>
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:77:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | int mid = l + r >> 1;
| ~~^~~
worst_reporter2.cpp: In function 'void upd(int, int, int, int, int, ll)':
worst_reporter2.cpp:95:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
95 | int mid = l + r >> 1;
| ~~^~~
worst_reporter2.cpp: In function 'll get(int, int, int, int)':
worst_reporter2.cpp:105:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
105 | 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 |
8 ms |
15948 KB |
Output is correct |
4 |
Correct |
8 ms |
15932 KB |
Output is correct |
5 |
Correct |
23 ms |
17188 KB |
Output is correct |
6 |
Correct |
16 ms |
16516 KB |
Output is correct |
7 |
Correct |
13 ms |
16368 KB |
Output is correct |
8 |
Correct |
25 ms |
17176 KB |
Output is correct |
9 |
Correct |
15 ms |
16520 KB |
Output is correct |
10 |
Correct |
12 ms |
16380 KB |
Output is correct |
11 |
Correct |
11 ms |
16332 KB |
Output is correct |
12 |
Correct |
15 ms |
17612 KB |
Output is correct |
13 |
Correct |
11 ms |
16972 KB |
Output is correct |
14 |
Correct |
16 ms |
17404 KB |
Output is correct |
15 |
Correct |
15 ms |
16716 KB |
Output is correct |
16 |
Correct |
32 ms |
17348 KB |
Output is correct |
17 |
Correct |
19 ms |
16588 KB |
Output is correct |
18 |
Correct |
10 ms |
16332 KB |
Output is correct |
19 |
Correct |
16 ms |
17228 KB |
Output is correct |
20 |
Correct |
12 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16644 KB |
Output is correct |
22 |
Correct |
20 ms |
16996 KB |
Output is correct |
23 |
Correct |
17 ms |
16348 KB |
Output is correct |
24 |
Correct |
16 ms |
17312 KB |
Output is correct |
25 |
Correct |
13 ms |
16728 KB |
Output is correct |
26 |
Correct |
13 ms |
17616 KB |
Output is correct |
27 |
Correct |
16 ms |
17228 KB |
Output is correct |
28 |
Correct |
17 ms |
17356 KB |
Output is correct |
29 |
Correct |
20 ms |
17492 KB |
Output is correct |
30 |
Correct |
15 ms |
17356 KB |
Output is correct |
31 |
Correct |
16 ms |
17384 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 |
8 ms |
15948 KB |
Output is correct |
4 |
Correct |
8 ms |
15932 KB |
Output is correct |
5 |
Correct |
23 ms |
17188 KB |
Output is correct |
6 |
Correct |
16 ms |
16516 KB |
Output is correct |
7 |
Correct |
13 ms |
16368 KB |
Output is correct |
8 |
Correct |
25 ms |
17176 KB |
Output is correct |
9 |
Correct |
15 ms |
16520 KB |
Output is correct |
10 |
Correct |
12 ms |
16380 KB |
Output is correct |
11 |
Correct |
11 ms |
16332 KB |
Output is correct |
12 |
Correct |
15 ms |
17612 KB |
Output is correct |
13 |
Correct |
11 ms |
16972 KB |
Output is correct |
14 |
Correct |
16 ms |
17404 KB |
Output is correct |
15 |
Correct |
15 ms |
16716 KB |
Output is correct |
16 |
Correct |
32 ms |
17348 KB |
Output is correct |
17 |
Correct |
19 ms |
16588 KB |
Output is correct |
18 |
Correct |
10 ms |
16332 KB |
Output is correct |
19 |
Correct |
16 ms |
17228 KB |
Output is correct |
20 |
Correct |
12 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16644 KB |
Output is correct |
22 |
Correct |
20 ms |
16996 KB |
Output is correct |
23 |
Correct |
17 ms |
16348 KB |
Output is correct |
24 |
Correct |
16 ms |
17312 KB |
Output is correct |
25 |
Correct |
13 ms |
16728 KB |
Output is correct |
26 |
Correct |
13 ms |
17616 KB |
Output is correct |
27 |
Correct |
16 ms |
17228 KB |
Output is correct |
28 |
Correct |
17 ms |
17356 KB |
Output is correct |
29 |
Correct |
20 ms |
17492 KB |
Output is correct |
30 |
Correct |
15 ms |
17356 KB |
Output is correct |
31 |
Correct |
16 ms |
17384 KB |
Output is correct |
32 |
Correct |
26 ms |
17164 KB |
Output is correct |
33 |
Correct |
1430 ms |
64960 KB |
Output is correct |
34 |
Correct |
608 ms |
37832 KB |
Output is correct |
35 |
Correct |
1381 ms |
63868 KB |
Output is correct |
36 |
Correct |
603 ms |
37828 KB |
Output is correct |
37 |
Correct |
266 ms |
30136 KB |
Output is correct |
38 |
Correct |
196 ms |
28196 KB |
Output is correct |
39 |
Correct |
478 ms |
78504 KB |
Output is correct |
40 |
Correct |
207 ms |
56860 KB |
Output is correct |
41 |
Correct |
121 ms |
56764 KB |
Output is correct |
42 |
Correct |
579 ms |
66264 KB |
Output is correct |
43 |
Correct |
207 ms |
42696 KB |
Output is correct |
44 |
Execution timed out |
2041 ms |
73324 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15948 KB |
Output is correct |
2 |
Correct |
8 ms |
15948 KB |
Output is correct |
3 |
Correct |
8 ms |
15948 KB |
Output is correct |
4 |
Correct |
8 ms |
15932 KB |
Output is correct |
5 |
Correct |
23 ms |
17188 KB |
Output is correct |
6 |
Correct |
16 ms |
16516 KB |
Output is correct |
7 |
Correct |
13 ms |
16368 KB |
Output is correct |
8 |
Correct |
25 ms |
17176 KB |
Output is correct |
9 |
Correct |
15 ms |
16520 KB |
Output is correct |
10 |
Correct |
12 ms |
16380 KB |
Output is correct |
11 |
Correct |
11 ms |
16332 KB |
Output is correct |
12 |
Correct |
15 ms |
17612 KB |
Output is correct |
13 |
Correct |
11 ms |
16972 KB |
Output is correct |
14 |
Correct |
16 ms |
17404 KB |
Output is correct |
15 |
Correct |
15 ms |
16716 KB |
Output is correct |
16 |
Correct |
32 ms |
17348 KB |
Output is correct |
17 |
Correct |
19 ms |
16588 KB |
Output is correct |
18 |
Correct |
10 ms |
16332 KB |
Output is correct |
19 |
Correct |
16 ms |
17228 KB |
Output is correct |
20 |
Correct |
12 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16644 KB |
Output is correct |
22 |
Correct |
20 ms |
16996 KB |
Output is correct |
23 |
Correct |
17 ms |
16348 KB |
Output is correct |
24 |
Correct |
16 ms |
17312 KB |
Output is correct |
25 |
Correct |
13 ms |
16728 KB |
Output is correct |
26 |
Correct |
13 ms |
17616 KB |
Output is correct |
27 |
Correct |
16 ms |
17228 KB |
Output is correct |
28 |
Correct |
17 ms |
17356 KB |
Output is correct |
29 |
Correct |
20 ms |
17492 KB |
Output is correct |
30 |
Correct |
15 ms |
17356 KB |
Output is correct |
31 |
Correct |
16 ms |
17384 KB |
Output is correct |
32 |
Correct |
26 ms |
17164 KB |
Output is correct |
33 |
Correct |
1430 ms |
64960 KB |
Output is correct |
34 |
Correct |
608 ms |
37832 KB |
Output is correct |
35 |
Correct |
1381 ms |
63868 KB |
Output is correct |
36 |
Correct |
603 ms |
37828 KB |
Output is correct |
37 |
Correct |
266 ms |
30136 KB |
Output is correct |
38 |
Correct |
196 ms |
28196 KB |
Output is correct |
39 |
Correct |
478 ms |
78504 KB |
Output is correct |
40 |
Correct |
207 ms |
56860 KB |
Output is correct |
41 |
Correct |
121 ms |
56764 KB |
Output is correct |
42 |
Correct |
579 ms |
66264 KB |
Output is correct |
43 |
Correct |
207 ms |
42696 KB |
Output is correct |
44 |
Execution timed out |
2041 ms |
73324 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |