#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MXN = 2e5 + 1;
const int MX = 1e9;
struct node {
ll v;
node *left, *right;
node() { v = 0, left = right = 0; }
void print(int lb, int rb) {
if(lb == rb) cout << lb << ":" << v << " ";
else {
int m = (lb + rb) / 2;
if(left) left->print(lb, m);
if(right) right->print(m + 1, rb);
}
}
ll del(ll rem, int r, int lb, int rb) {
r = min(r, rb);
if(r < lb) return 0;
if(rb == r && v <= rem) {
ll d = v;
v -= d;
left = right = 0;
return d;
}
if(rb == r && lb == rb) {
v -= rem;
return rem;
}
int m = (lb + rb) / 2;
ll d = right ? right->del(rem, r, m + 1, rb) : 0;
if(rem - d > 0) {
d += left ? left->del(rem - d, r, lb, m) : 0;
}
v -= d;
return d;
}
};
void upd(node* cur, int k, ll v) {
int lb = 1, rb = MX;
while(lb != rb) {
cur->v += v;
int m = (lb + rb) / 2;
if(k <= m) {
if(!cur->left) cur->left = new node();
cur = cur->left;
rb = m;
} else {
if(!cur->right) cur->right = new node();
cur = cur->right;
lb = m + 1;
}
}
cur->v += v;
}
void merge(node* cur, node* other) {
other->v += cur->v;
if(cur->left) {
if(other->left) {
merge(cur->left, other->left);
} else {
other->left = cur->left;
}
}
if(cur->right) {
if(other->right) {
merge(cur->right, other->right);
} else {
other->right = cur->right;
}
}
delete(cur);
}
int A[MXN], H[MXN], C[MXN], in[MXN];
node *segts[MXN];
int main() {
int N; cin >> N;
ll sm = 0;
for(int i = 1; i <= N; i++) {
cin >> A[i] >> H[i] >> C[i];
in[A[i]]++;
segts[i] = new node();
sm += C[i];
}
queue<int> q;
for(int i = 1; i <= N; i++) if(in[i] == 0) q.push(i);
while(!q.empty()) {
int s = q.front();
q.pop();
// cout << s << endl;
// segts[s]->print(1, MX);
// cout << endl;
segts[s]->del(C[s], H[s] - 1, 1, MX);
upd(segts[s], H[s], C[s]);
merge(segts[s], segts[A[s]]);
if(--in[A[s]] == 0) q.push(A[s]);
}
for(int i = 1; i <= N; i++) {
if(in[i] != 0) {
// cout << i << endl;
vector<int> cyc; cyc.push_back(i);
while(in[A[cyc.back()]]) {
in[A[cyc.back()]] = 0;
cyc.push_back(A[cyc.back()]);
}
cyc.pop_back();
for(int u : cyc) {
if(u != i) {
merge(segts[u], segts[i]);
}
}
for(int u : cyc) {
segts[i]->del(C[u], H[u] - 1, 1, MX);
upd(segts[i], H[u], C[u]);
}
// segts[i]->print(1, MX);
// cout << endl;
sm -= segts[i]->v;
}
}
cout << sm << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4536 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
12 ms |
8636 KB |
Output is correct |
6 |
Correct |
14 ms |
8284 KB |
Output is correct |
7 |
Correct |
11 ms |
7512 KB |
Output is correct |
8 |
Correct |
11 ms |
8540 KB |
Output is correct |
9 |
Correct |
12 ms |
8284 KB |
Output is correct |
10 |
Correct |
11 ms |
7628 KB |
Output is correct |
11 |
Correct |
8 ms |
6488 KB |
Output is correct |
12 |
Correct |
10 ms |
8028 KB |
Output is correct |
13 |
Correct |
6 ms |
4956 KB |
Output is correct |
14 |
Correct |
10 ms |
8284 KB |
Output is correct |
15 |
Correct |
8 ms |
6492 KB |
Output is correct |
16 |
Correct |
10 ms |
8408 KB |
Output is correct |
17 |
Correct |
12 ms |
8284 KB |
Output is correct |
18 |
Correct |
7 ms |
5724 KB |
Output is correct |
19 |
Correct |
9 ms |
7516 KB |
Output is correct |
20 |
Correct |
10 ms |
7052 KB |
Output is correct |
21 |
Correct |
9 ms |
7004 KB |
Output is correct |
22 |
Correct |
9 ms |
7516 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
10 ms |
7612 KB |
Output is correct |
25 |
Correct |
10 ms |
4936 KB |
Output is correct |
26 |
Correct |
8 ms |
7512 KB |
Output is correct |
27 |
Correct |
9 ms |
8028 KB |
Output is correct |
28 |
Correct |
9 ms |
8028 KB |
Output is correct |
29 |
Correct |
9 ms |
7772 KB |
Output is correct |
30 |
Correct |
9 ms |
7512 KB |
Output is correct |
31 |
Correct |
9 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4536 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
12 ms |
8636 KB |
Output is correct |
6 |
Correct |
14 ms |
8284 KB |
Output is correct |
7 |
Correct |
11 ms |
7512 KB |
Output is correct |
8 |
Correct |
11 ms |
8540 KB |
Output is correct |
9 |
Correct |
12 ms |
8284 KB |
Output is correct |
10 |
Correct |
11 ms |
7628 KB |
Output is correct |
11 |
Correct |
8 ms |
6488 KB |
Output is correct |
12 |
Correct |
10 ms |
8028 KB |
Output is correct |
13 |
Correct |
6 ms |
4956 KB |
Output is correct |
14 |
Correct |
10 ms |
8284 KB |
Output is correct |
15 |
Correct |
8 ms |
6492 KB |
Output is correct |
16 |
Correct |
10 ms |
8408 KB |
Output is correct |
17 |
Correct |
12 ms |
8284 KB |
Output is correct |
18 |
Correct |
7 ms |
5724 KB |
Output is correct |
19 |
Correct |
9 ms |
7516 KB |
Output is correct |
20 |
Correct |
10 ms |
7052 KB |
Output is correct |
21 |
Correct |
9 ms |
7004 KB |
Output is correct |
22 |
Correct |
9 ms |
7516 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
10 ms |
7612 KB |
Output is correct |
25 |
Correct |
10 ms |
4936 KB |
Output is correct |
26 |
Correct |
8 ms |
7512 KB |
Output is correct |
27 |
Correct |
9 ms |
8028 KB |
Output is correct |
28 |
Correct |
9 ms |
8028 KB |
Output is correct |
29 |
Correct |
9 ms |
7772 KB |
Output is correct |
30 |
Correct |
9 ms |
7512 KB |
Output is correct |
31 |
Correct |
9 ms |
7512 KB |
Output is correct |
32 |
Correct |
11 ms |
8540 KB |
Output is correct |
33 |
Correct |
787 ms |
169140 KB |
Output is correct |
34 |
Correct |
831 ms |
167508 KB |
Output is correct |
35 |
Correct |
758 ms |
169040 KB |
Output is correct |
36 |
Correct |
865 ms |
167656 KB |
Output is correct |
37 |
Correct |
587 ms |
114780 KB |
Output is correct |
38 |
Correct |
473 ms |
82768 KB |
Output is correct |
39 |
Correct |
360 ms |
135508 KB |
Output is correct |
40 |
Correct |
330 ms |
87624 KB |
Output is correct |
41 |
Correct |
205 ms |
16584 KB |
Output is correct |
42 |
Correct |
379 ms |
138700 KB |
Output is correct |
43 |
Correct |
345 ms |
98720 KB |
Output is correct |
44 |
Correct |
662 ms |
166424 KB |
Output is correct |
45 |
Correct |
671 ms |
164944 KB |
Output is correct |
46 |
Correct |
343 ms |
60240 KB |
Output is correct |
47 |
Correct |
467 ms |
108552 KB |
Output is correct |
48 |
Correct |
474 ms |
106832 KB |
Output is correct |
49 |
Correct |
374 ms |
107236 KB |
Output is correct |
50 |
Correct |
367 ms |
93460 KB |
Output is correct |
51 |
Correct |
305 ms |
16976 KB |
Output is correct |
52 |
Correct |
415 ms |
95380 KB |
Output is correct |
53 |
Correct |
297 ms |
17236 KB |
Output is correct |
54 |
Correct |
267 ms |
94380 KB |
Output is correct |
55 |
Correct |
427 ms |
130640 KB |
Output is correct |
56 |
Correct |
325 ms |
112464 KB |
Output is correct |
57 |
Correct |
304 ms |
105336 KB |
Output is correct |
58 |
Correct |
340 ms |
93604 KB |
Output is correct |
59 |
Correct |
314 ms |
93668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4536 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
12 ms |
8636 KB |
Output is correct |
6 |
Correct |
14 ms |
8284 KB |
Output is correct |
7 |
Correct |
11 ms |
7512 KB |
Output is correct |
8 |
Correct |
11 ms |
8540 KB |
Output is correct |
9 |
Correct |
12 ms |
8284 KB |
Output is correct |
10 |
Correct |
11 ms |
7628 KB |
Output is correct |
11 |
Correct |
8 ms |
6488 KB |
Output is correct |
12 |
Correct |
10 ms |
8028 KB |
Output is correct |
13 |
Correct |
6 ms |
4956 KB |
Output is correct |
14 |
Correct |
10 ms |
8284 KB |
Output is correct |
15 |
Correct |
8 ms |
6492 KB |
Output is correct |
16 |
Correct |
10 ms |
8408 KB |
Output is correct |
17 |
Correct |
12 ms |
8284 KB |
Output is correct |
18 |
Correct |
7 ms |
5724 KB |
Output is correct |
19 |
Correct |
9 ms |
7516 KB |
Output is correct |
20 |
Correct |
10 ms |
7052 KB |
Output is correct |
21 |
Correct |
9 ms |
7004 KB |
Output is correct |
22 |
Correct |
9 ms |
7516 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
10 ms |
7612 KB |
Output is correct |
25 |
Correct |
10 ms |
4936 KB |
Output is correct |
26 |
Correct |
8 ms |
7512 KB |
Output is correct |
27 |
Correct |
9 ms |
8028 KB |
Output is correct |
28 |
Correct |
9 ms |
8028 KB |
Output is correct |
29 |
Correct |
9 ms |
7772 KB |
Output is correct |
30 |
Correct |
9 ms |
7512 KB |
Output is correct |
31 |
Correct |
9 ms |
7512 KB |
Output is correct |
32 |
Correct |
11 ms |
8540 KB |
Output is correct |
33 |
Correct |
787 ms |
169140 KB |
Output is correct |
34 |
Correct |
831 ms |
167508 KB |
Output is correct |
35 |
Correct |
758 ms |
169040 KB |
Output is correct |
36 |
Correct |
865 ms |
167656 KB |
Output is correct |
37 |
Correct |
587 ms |
114780 KB |
Output is correct |
38 |
Correct |
473 ms |
82768 KB |
Output is correct |
39 |
Correct |
360 ms |
135508 KB |
Output is correct |
40 |
Correct |
330 ms |
87624 KB |
Output is correct |
41 |
Correct |
205 ms |
16584 KB |
Output is correct |
42 |
Correct |
379 ms |
138700 KB |
Output is correct |
43 |
Correct |
345 ms |
98720 KB |
Output is correct |
44 |
Correct |
662 ms |
166424 KB |
Output is correct |
45 |
Correct |
671 ms |
164944 KB |
Output is correct |
46 |
Correct |
343 ms |
60240 KB |
Output is correct |
47 |
Correct |
467 ms |
108552 KB |
Output is correct |
48 |
Correct |
474 ms |
106832 KB |
Output is correct |
49 |
Correct |
374 ms |
107236 KB |
Output is correct |
50 |
Correct |
367 ms |
93460 KB |
Output is correct |
51 |
Correct |
305 ms |
16976 KB |
Output is correct |
52 |
Correct |
415 ms |
95380 KB |
Output is correct |
53 |
Correct |
297 ms |
17236 KB |
Output is correct |
54 |
Correct |
267 ms |
94380 KB |
Output is correct |
55 |
Correct |
427 ms |
130640 KB |
Output is correct |
56 |
Correct |
325 ms |
112464 KB |
Output is correct |
57 |
Correct |
304 ms |
105336 KB |
Output is correct |
58 |
Correct |
340 ms |
93604 KB |
Output is correct |
59 |
Correct |
314 ms |
93668 KB |
Output is correct |
60 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |