#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> a(n + 1), h(n + 1), c(n + 1);
for (int i = 1; i <= n; i++){
cin>>a[i]>>h[i]>>c[i];
}
vector<pii> all;
for (int i = 1; i <= n; i++) all.pb({h[i], i});
sort(all.begin(), all.end());
int i = 0, cc = 0;
while (i < all.size()){
int j = i;
while (j < all.size() && all[i] == all[j]){
j++;
}
cc++;
while (i < j){
h[all[i].ss] = cc;
i++;
}
}
vector<int> g1[n + 1];
for (int i = 1; i <= n; i++){
if (a[i] != i) g1[a[i]].pb(i);
}
vector<bool> used(n + 1), seen(n + 1);
vector<int> dd(n + 1), d[n + 1], pp(n + 1);
function<void(int)> dfs = [&](int v){
seen[v] = used[v] = 1;
for (int i: g1[v]){
if (used[i]){
if (seen[i]){
dd[i] = v;
for (int j = v; j != i; j = pp[j]){
dd[j] = v;
}
}
continue;
}
pp[i] = v;
dfs(i);
}
seen[v] = 0;
};
for (int i = 1; i <= n; i++){
if (!used[i]){
dfs(i);
}
}
vector<int> g[n + 1];
for (int i = 1; i <= n; i++){
if (!dd[i]) dd[i] = i;
d[dd[i]].pb(i);
}
fill(used.begin(), used.end(), 0);
for (int i = 1; i <= n; i++){
int x = dd[a[i]], y = dd[i];
if (x != y){
g[x].pb(y);
used[y] = 1;
}
}
vector<ll> sum(cc + 1);
map<int, ll> mp[n + 1];
auto add = [&](int v, int l, int r, ll x){
mp[v][l] += x;
if (r < cc) mp[v][r + 1] -= x;
};
function<void(int)> solve = [&](int v){
for (int i: g[v]) solve(i);
for (int i: g[v]){
if (mp[v].size() < mp[i].size()) swap(mp[v], mp[i]);
for (auto [p, x]: mp[i]){
mp[v][p] += x;
}
}
ll S = 0;
for (int i: d[v]) S += c[i];
map<int, ll> t;
for (int i: d[v]){
t[h[i]] += c[i];
}
add(v, 1, cc, S);
for (auto [i, S]: t){
auto it = mp[v].upper_bound(i);
if (it == mp[v].begin()) continue;
it--;
if (i < cc) mp[v][i + 1] += S;
ll x = S; int r = -1;
while ((*it).ss < x) {
r = (*it).ff;
x -= (*it).ss;
if (it == mp[v].begin()) break;
it--;
}
it -> ss -= x;
if (r != -1){
while (true){
it = mp[v].lower_bound(r);
if (it == mp[v].end() || (*it).ff > i) break;
mp[v].erase(it);
}
}
}
};
ll out = 0;
for (int i = 1; i <= n; i++){
if (!used[i]){
solve(i);
out += (*mp[i].begin()).ss;
}
}
cout<<out<<"\n";
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:25:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while (i < all.size()){
| ~~^~~~~~~~~~~~
worst_reporter2.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | while (j < all.size() && all[i] == all[j]){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
6 ms |
2652 KB |
Output is correct |
7 |
Correct |
5 ms |
2684 KB |
Output is correct |
8 |
Correct |
6 ms |
2652 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2640 KB |
Output is correct |
11 |
Correct |
6 ms |
2944 KB |
Output is correct |
12 |
Correct |
4 ms |
2908 KB |
Output is correct |
13 |
Correct |
4 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
5 ms |
2136 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
6 ms |
3676 KB |
Output is correct |
19 |
Correct |
5 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2652 KB |
Output is correct |
21 |
Correct |
4 ms |
2652 KB |
Output is correct |
22 |
Correct |
4 ms |
2396 KB |
Output is correct |
23 |
Correct |
4 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
2556 KB |
Output is correct |
25 |
Correct |
4 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
3160 KB |
Output is correct |
27 |
Correct |
5 ms |
2652 KB |
Output is correct |
28 |
Correct |
4 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2916 KB |
Output is correct |
30 |
Correct |
4 ms |
2648 KB |
Output is correct |
31 |
Correct |
4 ms |
2812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
6 ms |
2652 KB |
Output is correct |
7 |
Correct |
5 ms |
2684 KB |
Output is correct |
8 |
Correct |
6 ms |
2652 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2640 KB |
Output is correct |
11 |
Correct |
6 ms |
2944 KB |
Output is correct |
12 |
Correct |
4 ms |
2908 KB |
Output is correct |
13 |
Correct |
4 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
5 ms |
2136 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
6 ms |
3676 KB |
Output is correct |
19 |
Correct |
5 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2652 KB |
Output is correct |
21 |
Correct |
4 ms |
2652 KB |
Output is correct |
22 |
Correct |
4 ms |
2396 KB |
Output is correct |
23 |
Correct |
4 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
2556 KB |
Output is correct |
25 |
Correct |
4 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
3160 KB |
Output is correct |
27 |
Correct |
5 ms |
2652 KB |
Output is correct |
28 |
Correct |
4 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2916 KB |
Output is correct |
30 |
Correct |
4 ms |
2648 KB |
Output is correct |
31 |
Correct |
4 ms |
2812 KB |
Output is correct |
32 |
Correct |
6 ms |
2652 KB |
Output is correct |
33 |
Correct |
391 ms |
109284 KB |
Output is correct |
34 |
Correct |
409 ms |
109008 KB |
Output is correct |
35 |
Correct |
406 ms |
106444 KB |
Output is correct |
36 |
Correct |
365 ms |
109000 KB |
Output is correct |
37 |
Correct |
361 ms |
110504 KB |
Output is correct |
38 |
Correct |
375 ms |
120776 KB |
Output is correct |
39 |
Correct |
181 ms |
98760 KB |
Output is correct |
40 |
Correct |
173 ms |
98660 KB |
Output is correct |
41 |
Correct |
181 ms |
101320 KB |
Output is correct |
42 |
Correct |
171 ms |
77000 KB |
Output is correct |
43 |
Correct |
159 ms |
77000 KB |
Output is correct |
44 |
Correct |
359 ms |
134104 KB |
Output is correct |
45 |
Correct |
357 ms |
134328 KB |
Output is correct |
46 |
Correct |
413 ms |
162968 KB |
Output is correct |
47 |
Correct |
263 ms |
89804 KB |
Output is correct |
48 |
Correct |
264 ms |
89288 KB |
Output is correct |
49 |
Correct |
210 ms |
90340 KB |
Output is correct |
50 |
Correct |
209 ms |
80584 KB |
Output is correct |
51 |
Correct |
213 ms |
80588 KB |
Output is correct |
52 |
Correct |
219 ms |
90984 KB |
Output is correct |
53 |
Correct |
238 ms |
90064 KB |
Output is correct |
54 |
Correct |
205 ms |
111340 KB |
Output is correct |
55 |
Correct |
240 ms |
91592 KB |
Output is correct |
56 |
Correct |
211 ms |
99812 KB |
Output is correct |
57 |
Correct |
187 ms |
103668 KB |
Output is correct |
58 |
Correct |
179 ms |
94812 KB |
Output is correct |
59 |
Correct |
185 ms |
94924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
6 ms |
2652 KB |
Output is correct |
7 |
Correct |
5 ms |
2684 KB |
Output is correct |
8 |
Correct |
6 ms |
2652 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2640 KB |
Output is correct |
11 |
Correct |
6 ms |
2944 KB |
Output is correct |
12 |
Correct |
4 ms |
2908 KB |
Output is correct |
13 |
Correct |
4 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
5 ms |
2136 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
6 ms |
3676 KB |
Output is correct |
19 |
Correct |
5 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2652 KB |
Output is correct |
21 |
Correct |
4 ms |
2652 KB |
Output is correct |
22 |
Correct |
4 ms |
2396 KB |
Output is correct |
23 |
Correct |
4 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
2556 KB |
Output is correct |
25 |
Correct |
4 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
3160 KB |
Output is correct |
27 |
Correct |
5 ms |
2652 KB |
Output is correct |
28 |
Correct |
4 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2916 KB |
Output is correct |
30 |
Correct |
4 ms |
2648 KB |
Output is correct |
31 |
Correct |
4 ms |
2812 KB |
Output is correct |
32 |
Correct |
6 ms |
2652 KB |
Output is correct |
33 |
Correct |
391 ms |
109284 KB |
Output is correct |
34 |
Correct |
409 ms |
109008 KB |
Output is correct |
35 |
Correct |
406 ms |
106444 KB |
Output is correct |
36 |
Correct |
365 ms |
109000 KB |
Output is correct |
37 |
Correct |
361 ms |
110504 KB |
Output is correct |
38 |
Correct |
375 ms |
120776 KB |
Output is correct |
39 |
Correct |
181 ms |
98760 KB |
Output is correct |
40 |
Correct |
173 ms |
98660 KB |
Output is correct |
41 |
Correct |
181 ms |
101320 KB |
Output is correct |
42 |
Correct |
171 ms |
77000 KB |
Output is correct |
43 |
Correct |
159 ms |
77000 KB |
Output is correct |
44 |
Correct |
359 ms |
134104 KB |
Output is correct |
45 |
Correct |
357 ms |
134328 KB |
Output is correct |
46 |
Correct |
413 ms |
162968 KB |
Output is correct |
47 |
Correct |
263 ms |
89804 KB |
Output is correct |
48 |
Correct |
264 ms |
89288 KB |
Output is correct |
49 |
Correct |
210 ms |
90340 KB |
Output is correct |
50 |
Correct |
209 ms |
80584 KB |
Output is correct |
51 |
Correct |
213 ms |
80588 KB |
Output is correct |
52 |
Correct |
219 ms |
90984 KB |
Output is correct |
53 |
Correct |
238 ms |
90064 KB |
Output is correct |
54 |
Correct |
205 ms |
111340 KB |
Output is correct |
55 |
Correct |
240 ms |
91592 KB |
Output is correct |
56 |
Correct |
211 ms |
99812 KB |
Output is correct |
57 |
Correct |
187 ms |
103668 KB |
Output is correct |
58 |
Correct |
179 ms |
94812 KB |
Output is correct |
59 |
Correct |
185 ms |
94924 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
0 ms |
416 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
600 KB |
Output is correct |
64 |
Correct |
339 ms |
93576 KB |
Output is correct |
65 |
Incorrect |
330 ms |
92616 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |