#include <cstdio>
#include <algorithm>
#include <vector>
#define PB push_back
using namespace std;
const int N = 4e5 + 500;
const int OFF = (1 << 18);
typedef long long ll;
struct node{
node *L, *R;
ll sum;
node(){
sum = 0, L = NULL, R = NULL;
}
};
typedef node* pnode;
ll get_sum(pnode x) { return x ? x -> sum : 0LL;}
int bio[N];
void update(pnode &x, int a, int b, int gdj, ll dod){
if(!x) x = new node();
x -> sum += dod;
if(a != b && gdj <= (a + b) / 2)
update(x -> L, a, (a + b) / 2, gdj, dod);
else if(a != b)
update(x -> R, (a + b) / 2 + 1, b, gdj, dod);
}
ll query(pnode &x, int a, int b, int gdj){
if(!x || a > gdj) return 0;
if(gdj == b) return x -> sum;
if(gdj <= (a + b) / 2)
return query(x -> L, a, (a + b) / 2, gdj);
else
return get_sum(x -> L) + query(x -> R, (a + b) / 2 + 1, b, gdj);
}
void brisi(pnode &x, int a, int b, int lo, int hi){
if(!x) return;
if(a > hi || b < lo) return;
brisi(x -> L, a, (a + b) / 2, lo, hi);
brisi(x -> R, (a + b) / 2 + 1, b, lo, hi);
if(lo <= a && b <= hi){
delete x; x = NULL;
}
else{
x -> sum = get_sum(x -> L) + get_sum(x -> R);
}
}
ll OSTALO;
int bar(pnode &x, int a, int b, ll kol){
if(!x || a == b) {
OSTALO = kol;
return b;
}
if(get_sum(x -> L) >= kol)
return bar(x -> L, a, (a + b) / 2, kol);
return bar(x -> R, (a + b) / 2 + 1, b, kol - get_sum(x -> L));
}
void prodi_sve(pnode &x, int a, int b, pnode novi){
if(!x || (x -> sum == 0)) return;
if(a == b){
update(novi, 0, OFF - 1, a, x -> sum);
return;
}
prodi_sve(x -> L, a, (a + b) / 2, novi);
prodi_sve(x -> R, (a + b) / 2 + 1, b, novi);
delete x; x = NULL;
}
pnode root[N];
int siz[N], C[N], A[N], P[N], boja[N];
vector < int > v[N], cik[N];
void print(pnode x, int a, int b, int mks){
if(a > mks || !x) return;
if(a == b)
printf("(%d, %lld) ", a, x -> sum);
else{
print(x -> L, a, (a + b) / 2, mks);
print(x -> R, (a + b) / 2 + 1, b, mks);
}
}
int n;
void primjeni(pnode &x, int AA, int CC){
ll ja = query(x, 0, OFF - 1, AA) + CC;
int gr = bar(x, 0, OFF - 1, ja);
if(gr != OFF - 1){
update(x, 0, OFF - 1, gr, -OSTALO);
brisi(x, 0, OFF - 1, AA + 1, gr - 1);
}
else{
brisi(x, 0, OFF - 1, AA + 1, OFF - 1);
}
update(x, 0, OFF - 1, AA, CC);
}
bool cmp(int x, int y){
return A[x] > A[y];
}
void dfs(int x){
siz[x] = 0;
for(int y : v[x]){
dfs(y);
if(siz[y] > siz[x]){
swap(siz[x], siz[y]);
swap(root[x], root[y]);
}
prodi_sve(root[y], 0, OFF - 1, root[x]);
siz[x] += siz[y];
}
siz[x]++;
if(x <= n){
primjeni(root[x], A[x], C[x]);
}
else{
sort(cik[x].begin(), cik[x].end(), cmp);
for(int xx : cik[x]){
primjeni(root[x], A[xx], C[xx]);
}
}
}
vector < int > saz;
vector < int > g[N];
ll sve = 0;
int novi = 0;
void biooo(int x){
if(bio[x] == 2) return;
bio[x] = 2;
for(int y : g[x])
biooo(y);
}
int main(){
scanf("%d", &n); novi = n + 1;
for(int i = 1;i <= n;i++){
scanf("%d%d%d", P + i, A + i, C + i);
saz.PB(-A[i]);
sve += C[i];
g[P[i]].PB(i);
g[i].PB(P[i]);
}
sort(saz.begin(), saz.end());
saz.erase(unique(saz.begin(), saz.end()), saz.end());
for(int i = 1;i <= n;i++)
A[i] = lower_bound(saz.begin(), saz.end(), -A[i]) - saz.begin() + 1;
for(int i = 1;i <= n;i++){
if(bio[i]) continue;
int cur = i;
while(!bio[cur]){
bio[cur] = 1;
cur = P[cur];
}
biooo(cur);
int poc = cur, st = 1;
while(cur != poc || st){
boja[cur] = novi;
cik[novi].PB(cur);
cur = P[cur];
st = 0;
}
novi++;
}
for(int i = 1;i <= n;i++){
if(boja[i]) continue;
if(boja[P[i]]) v[boja[P[i]]].PB(i);
else v[P[i]].PB(i);
}
for(int i = n + 1;i < novi;i++){
dfs(i);
sve -= query(root[i], 0, OFF - 1, OFF - 1);
}
printf("%lld\n", sve);
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:150:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
150 | scanf("%d", &n); novi = n + 1;
| ~~~~~^~~~~~~~~~
worst_reporter2.cpp:152:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
152 | scanf("%d%d%d", P + i, A + i, C + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
17 ms |
28476 KB |
Output is correct |
3 |
Correct |
20 ms |
28508 KB |
Output is correct |
4 |
Correct |
18 ms |
28508 KB |
Output is correct |
5 |
Correct |
30 ms |
29712 KB |
Output is correct |
6 |
Correct |
31 ms |
29240 KB |
Output is correct |
7 |
Correct |
24 ms |
29132 KB |
Output is correct |
8 |
Correct |
35 ms |
29712 KB |
Output is correct |
9 |
Correct |
32 ms |
29320 KB |
Output is correct |
10 |
Correct |
25 ms |
29252 KB |
Output is correct |
11 |
Correct |
26 ms |
29040 KB |
Output is correct |
12 |
Correct |
26 ms |
29464 KB |
Output is correct |
13 |
Correct |
23 ms |
29432 KB |
Output is correct |
14 |
Correct |
26 ms |
29388 KB |
Output is correct |
15 |
Correct |
25 ms |
29260 KB |
Output is correct |
16 |
Correct |
32 ms |
29980 KB |
Output is correct |
17 |
Correct |
26 ms |
29424 KB |
Output is correct |
18 |
Correct |
21 ms |
29132 KB |
Output is correct |
19 |
Correct |
25 ms |
29548 KB |
Output is correct |
20 |
Correct |
24 ms |
29284 KB |
Output is correct |
21 |
Correct |
23 ms |
29324 KB |
Output is correct |
22 |
Correct |
29 ms |
29456 KB |
Output is correct |
23 |
Correct |
24 ms |
29156 KB |
Output is correct |
24 |
Correct |
27 ms |
29536 KB |
Output is correct |
25 |
Correct |
24 ms |
29360 KB |
Output is correct |
26 |
Correct |
25 ms |
29788 KB |
Output is correct |
27 |
Correct |
37 ms |
29540 KB |
Output is correct |
28 |
Correct |
26 ms |
29536 KB |
Output is correct |
29 |
Correct |
28 ms |
29640 KB |
Output is correct |
30 |
Correct |
26 ms |
29636 KB |
Output is correct |
31 |
Correct |
25 ms |
29716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
29676 KB |
Output is correct |
2 |
Correct |
960 ms |
84432 KB |
Output is correct |
3 |
Correct |
556 ms |
64412 KB |
Output is correct |
4 |
Correct |
964 ms |
81816 KB |
Output is correct |
5 |
Correct |
538 ms |
64380 KB |
Output is correct |
6 |
Correct |
359 ms |
54848 KB |
Output is correct |
7 |
Correct |
369 ms |
52396 KB |
Output is correct |
8 |
Correct |
429 ms |
68316 KB |
Output is correct |
9 |
Correct |
312 ms |
68160 KB |
Output is correct |
10 |
Correct |
219 ms |
68116 KB |
Output is correct |
11 |
Correct |
454 ms |
60880 KB |
Output is correct |
12 |
Correct |
326 ms |
60216 KB |
Output is correct |
13 |
Correct |
1139 ms |
98996 KB |
Output is correct |
14 |
Correct |
516 ms |
72348 KB |
Output is correct |
15 |
Correct |
268 ms |
52360 KB |
Output is correct |
16 |
Correct |
492 ms |
69024 KB |
Output is correct |
17 |
Correct |
304 ms |
60216 KB |
Output is correct |
18 |
Correct |
247 ms |
60100 KB |
Output is correct |
19 |
Correct |
414 ms |
65584 KB |
Output is correct |
20 |
Correct |
292 ms |
53164 KB |
Output is correct |
21 |
Correct |
435 ms |
70056 KB |
Output is correct |
22 |
Correct |
289 ms |
61024 KB |
Output is correct |
23 |
Correct |
337 ms |
80812 KB |
Output is correct |
24 |
Correct |
461 ms |
70092 KB |
Output is correct |
25 |
Correct |
425 ms |
74060 KB |
Output is correct |
26 |
Correct |
373 ms |
76092 KB |
Output is correct |
27 |
Correct |
373 ms |
75144 KB |
Output is correct |
28 |
Correct |
370 ms |
75236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
17 ms |
28476 KB |
Output is correct |
3 |
Correct |
20 ms |
28508 KB |
Output is correct |
4 |
Correct |
18 ms |
28508 KB |
Output is correct |
5 |
Correct |
30 ms |
29712 KB |
Output is correct |
6 |
Correct |
31 ms |
29240 KB |
Output is correct |
7 |
Correct |
24 ms |
29132 KB |
Output is correct |
8 |
Correct |
35 ms |
29712 KB |
Output is correct |
9 |
Correct |
32 ms |
29320 KB |
Output is correct |
10 |
Correct |
25 ms |
29252 KB |
Output is correct |
11 |
Correct |
26 ms |
29040 KB |
Output is correct |
12 |
Correct |
26 ms |
29464 KB |
Output is correct |
13 |
Correct |
23 ms |
29432 KB |
Output is correct |
14 |
Correct |
26 ms |
29388 KB |
Output is correct |
15 |
Correct |
25 ms |
29260 KB |
Output is correct |
16 |
Correct |
32 ms |
29980 KB |
Output is correct |
17 |
Correct |
26 ms |
29424 KB |
Output is correct |
18 |
Correct |
21 ms |
29132 KB |
Output is correct |
19 |
Correct |
25 ms |
29548 KB |
Output is correct |
20 |
Correct |
24 ms |
29284 KB |
Output is correct |
21 |
Correct |
23 ms |
29324 KB |
Output is correct |
22 |
Correct |
29 ms |
29456 KB |
Output is correct |
23 |
Correct |
24 ms |
29156 KB |
Output is correct |
24 |
Correct |
27 ms |
29536 KB |
Output is correct |
25 |
Correct |
24 ms |
29360 KB |
Output is correct |
26 |
Correct |
25 ms |
29788 KB |
Output is correct |
27 |
Correct |
37 ms |
29540 KB |
Output is correct |
28 |
Correct |
26 ms |
29536 KB |
Output is correct |
29 |
Correct |
28 ms |
29640 KB |
Output is correct |
30 |
Correct |
26 ms |
29636 KB |
Output is correct |
31 |
Correct |
25 ms |
29716 KB |
Output is correct |
32 |
Correct |
31 ms |
29676 KB |
Output is correct |
33 |
Correct |
960 ms |
84432 KB |
Output is correct |
34 |
Correct |
556 ms |
64412 KB |
Output is correct |
35 |
Correct |
964 ms |
81816 KB |
Output is correct |
36 |
Correct |
538 ms |
64380 KB |
Output is correct |
37 |
Correct |
359 ms |
54848 KB |
Output is correct |
38 |
Correct |
369 ms |
52396 KB |
Output is correct |
39 |
Correct |
429 ms |
68316 KB |
Output is correct |
40 |
Correct |
312 ms |
68160 KB |
Output is correct |
41 |
Correct |
219 ms |
68116 KB |
Output is correct |
42 |
Correct |
454 ms |
60880 KB |
Output is correct |
43 |
Correct |
326 ms |
60216 KB |
Output is correct |
44 |
Correct |
1139 ms |
98996 KB |
Output is correct |
45 |
Correct |
516 ms |
72348 KB |
Output is correct |
46 |
Correct |
268 ms |
52360 KB |
Output is correct |
47 |
Correct |
492 ms |
69024 KB |
Output is correct |
48 |
Correct |
304 ms |
60216 KB |
Output is correct |
49 |
Correct |
247 ms |
60100 KB |
Output is correct |
50 |
Correct |
414 ms |
65584 KB |
Output is correct |
51 |
Correct |
292 ms |
53164 KB |
Output is correct |
52 |
Correct |
435 ms |
70056 KB |
Output is correct |
53 |
Correct |
289 ms |
61024 KB |
Output is correct |
54 |
Correct |
337 ms |
80812 KB |
Output is correct |
55 |
Correct |
461 ms |
70092 KB |
Output is correct |
56 |
Correct |
425 ms |
74060 KB |
Output is correct |
57 |
Correct |
373 ms |
76092 KB |
Output is correct |
58 |
Correct |
373 ms |
75144 KB |
Output is correct |
59 |
Correct |
370 ms |
75236 KB |
Output is correct |
60 |
Correct |
19 ms |
28492 KB |
Output is correct |
61 |
Correct |
20 ms |
28560 KB |
Output is correct |
62 |
Correct |
16 ms |
28432 KB |
Output is correct |
63 |
Correct |
17 ms |
28476 KB |
Output is correct |
64 |
Correct |
849 ms |
79848 KB |
Output is correct |
65 |
Correct |
535 ms |
61876 KB |
Output is correct |
66 |
Correct |
857 ms |
79176 KB |
Output is correct |
67 |
Correct |
554 ms |
61644 KB |
Output is correct |
68 |
Correct |
385 ms |
55092 KB |
Output is correct |
69 |
Correct |
352 ms |
53316 KB |
Output is correct |
70 |
Correct |
442 ms |
50732 KB |
Output is correct |
71 |
Correct |
343 ms |
48972 KB |
Output is correct |
72 |
Correct |
424 ms |
52400 KB |
Output is correct |
73 |
Correct |
333 ms |
52280 KB |
Output is correct |
74 |
Correct |
465 ms |
63372 KB |
Output is correct |
75 |
Correct |
359 ms |
53952 KB |
Output is correct |
76 |
Correct |
325 ms |
54084 KB |
Output is correct |
77 |
Correct |
429 ms |
63816 KB |
Output is correct |
78 |
Correct |
328 ms |
54448 KB |
Output is correct |
79 |
Correct |
698 ms |
72016 KB |
Output is correct |
80 |
Correct |
450 ms |
60344 KB |
Output is correct |
81 |
Correct |
349 ms |
54968 KB |
Output is correct |
82 |
Correct |
440 ms |
64404 KB |
Output is correct |
83 |
Correct |
379 ms |
172096 KB |
Output is correct |
84 |
Correct |
540 ms |
68472 KB |
Output is correct |
85 |
Correct |
557 ms |
67576 KB |
Output is correct |
86 |
Correct |
558 ms |
67452 KB |
Output is correct |
87 |
Correct |
557 ms |
67764 KB |
Output is correct |
88 |
Correct |
503 ms |
67512 KB |
Output is correct |