답안 #932621

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932621 2024-02-24T00:48:20 Z ksujay2 Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
832 ms 164348 KB
#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;
}
ll query(node* cur, int k) {
    ll ans = 0;
    int lb = 1, rb = MX;
    while(lb != rb) {
        int m = (lb + rb) / 2;
        if(k <= m) {
            if(cur->right) ans += cur->right->v;
            if(!cur->left) return ans;
            cur = cur->left;
            rb = m;
        } else {
            if(!cur->right) return ans;
            cur = cur->right;
            lb = m + 1;
        }
    }
    return ans + cur->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();
            map<int, ll> cnt;
            for(int u : cyc) {
                cnt[H[u]] += C[u];
                if(u != i) {
                    merge(segts[u], segts[i]);
                }
            }
            ll best = segts[i]->v;
            for(auto [u, v] : cnt) {
                best = max(best, v + query(segts[i], u));
            }
            sm -= best;
        }
    }
    cout << sm << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4596 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 11 ms 8496 KB Output is correct
6 Correct 12 ms 8284 KB Output is correct
7 Correct 10 ms 7468 KB Output is correct
8 Correct 11 ms 8540 KB Output is correct
9 Correct 12 ms 8284 KB Output is correct
10 Correct 10 ms 7256 KB Output is correct
11 Correct 9 ms 6236 KB Output is correct
12 Correct 11 ms 8032 KB Output is correct
13 Correct 6 ms 4700 KB Output is correct
14 Correct 10 ms 8028 KB Output is correct
15 Correct 8 ms 6448 KB Output is correct
16 Correct 10 ms 8284 KB Output is correct
17 Correct 12 ms 8028 KB Output is correct
18 Correct 7 ms 5756 KB Output is correct
19 Correct 10 ms 7516 KB Output is correct
20 Correct 9 ms 6972 KB Output is correct
21 Correct 9 ms 7004 KB Output is correct
22 Correct 9 ms 7484 KB Output is correct
23 Correct 8 ms 4696 KB Output is correct
24 Correct 9 ms 7516 KB Output is correct
25 Correct 7 ms 4732 KB Output is correct
26 Correct 8 ms 7512 KB Output is correct
27 Correct 9 ms 8028 KB Output is correct
28 Correct 10 ms 7772 KB Output is correct
29 Correct 9 ms 7772 KB Output is correct
30 Correct 10 ms 7516 KB Output is correct
31 Correct 9 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4596 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 11 ms 8496 KB Output is correct
6 Correct 12 ms 8284 KB Output is correct
7 Correct 10 ms 7468 KB Output is correct
8 Correct 11 ms 8540 KB Output is correct
9 Correct 12 ms 8284 KB Output is correct
10 Correct 10 ms 7256 KB Output is correct
11 Correct 9 ms 6236 KB Output is correct
12 Correct 11 ms 8032 KB Output is correct
13 Correct 6 ms 4700 KB Output is correct
14 Correct 10 ms 8028 KB Output is correct
15 Correct 8 ms 6448 KB Output is correct
16 Correct 10 ms 8284 KB Output is correct
17 Correct 12 ms 8028 KB Output is correct
18 Correct 7 ms 5756 KB Output is correct
19 Correct 10 ms 7516 KB Output is correct
20 Correct 9 ms 6972 KB Output is correct
21 Correct 9 ms 7004 KB Output is correct
22 Correct 9 ms 7484 KB Output is correct
23 Correct 8 ms 4696 KB Output is correct
24 Correct 9 ms 7516 KB Output is correct
25 Correct 7 ms 4732 KB Output is correct
26 Correct 8 ms 7512 KB Output is correct
27 Correct 9 ms 8028 KB Output is correct
28 Correct 10 ms 7772 KB Output is correct
29 Correct 9 ms 7772 KB Output is correct
30 Correct 10 ms 7516 KB Output is correct
31 Correct 9 ms 7516 KB Output is correct
32 Correct 11 ms 8472 KB Output is correct
33 Correct 805 ms 164348 KB Output is correct
34 Correct 832 ms 162644 KB Output is correct
35 Correct 809 ms 164184 KB Output is correct
36 Correct 829 ms 162836 KB Output is correct
37 Correct 605 ms 109728 KB Output is correct
38 Correct 472 ms 77700 KB Output is correct
39 Correct 386 ms 130288 KB Output is correct
40 Correct 327 ms 82512 KB Output is correct
41 Correct 212 ms 11236 KB Output is correct
42 Correct 380 ms 133416 KB Output is correct
43 Correct 353 ms 93788 KB Output is correct
44 Correct 664 ms 161156 KB Output is correct
45 Correct 696 ms 159824 KB Output is correct
46 Correct 345 ms 55304 KB Output is correct
47 Correct 460 ms 103308 KB Output is correct
48 Correct 434 ms 101800 KB Output is correct
49 Correct 372 ms 102164 KB Output is correct
50 Correct 356 ms 89172 KB Output is correct
51 Correct 324 ms 12728 KB Output is correct
52 Correct 424 ms 90112 KB Output is correct
53 Correct 294 ms 12320 KB Output is correct
54 Correct 285 ms 89128 KB Output is correct
55 Correct 394 ms 126320 KB Output is correct
56 Correct 327 ms 108168 KB Output is correct
57 Correct 315 ms 101244 KB Output is correct
58 Correct 316 ms 89216 KB Output is correct
59 Correct 317 ms 89076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4596 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 11 ms 8496 KB Output is correct
6 Correct 12 ms 8284 KB Output is correct
7 Correct 10 ms 7468 KB Output is correct
8 Correct 11 ms 8540 KB Output is correct
9 Correct 12 ms 8284 KB Output is correct
10 Correct 10 ms 7256 KB Output is correct
11 Correct 9 ms 6236 KB Output is correct
12 Correct 11 ms 8032 KB Output is correct
13 Correct 6 ms 4700 KB Output is correct
14 Correct 10 ms 8028 KB Output is correct
15 Correct 8 ms 6448 KB Output is correct
16 Correct 10 ms 8284 KB Output is correct
17 Correct 12 ms 8028 KB Output is correct
18 Correct 7 ms 5756 KB Output is correct
19 Correct 10 ms 7516 KB Output is correct
20 Correct 9 ms 6972 KB Output is correct
21 Correct 9 ms 7004 KB Output is correct
22 Correct 9 ms 7484 KB Output is correct
23 Correct 8 ms 4696 KB Output is correct
24 Correct 9 ms 7516 KB Output is correct
25 Correct 7 ms 4732 KB Output is correct
26 Correct 8 ms 7512 KB Output is correct
27 Correct 9 ms 8028 KB Output is correct
28 Correct 10 ms 7772 KB Output is correct
29 Correct 9 ms 7772 KB Output is correct
30 Correct 10 ms 7516 KB Output is correct
31 Correct 9 ms 7516 KB Output is correct
32 Correct 11 ms 8472 KB Output is correct
33 Correct 805 ms 164348 KB Output is correct
34 Correct 832 ms 162644 KB Output is correct
35 Correct 809 ms 164184 KB Output is correct
36 Correct 829 ms 162836 KB Output is correct
37 Correct 605 ms 109728 KB Output is correct
38 Correct 472 ms 77700 KB Output is correct
39 Correct 386 ms 130288 KB Output is correct
40 Correct 327 ms 82512 KB Output is correct
41 Correct 212 ms 11236 KB Output is correct
42 Correct 380 ms 133416 KB Output is correct
43 Correct 353 ms 93788 KB Output is correct
44 Correct 664 ms 161156 KB Output is correct
45 Correct 696 ms 159824 KB Output is correct
46 Correct 345 ms 55304 KB Output is correct
47 Correct 460 ms 103308 KB Output is correct
48 Correct 434 ms 101800 KB Output is correct
49 Correct 372 ms 102164 KB Output is correct
50 Correct 356 ms 89172 KB Output is correct
51 Correct 324 ms 12728 KB Output is correct
52 Correct 424 ms 90112 KB Output is correct
53 Correct 294 ms 12320 KB Output is correct
54 Correct 285 ms 89128 KB Output is correct
55 Correct 394 ms 126320 KB Output is correct
56 Correct 327 ms 108168 KB Output is correct
57 Correct 315 ms 101244 KB Output is correct
58 Correct 316 ms 89216 KB Output is correct
59 Correct 317 ms 89076 KB Output is correct
60 Correct 1 ms 4444 KB Output is correct
61 Correct 2 ms 4444 KB Output is correct
62 Correct 1 ms 4444 KB Output is correct
63 Correct 1 ms 4548 KB Output is correct
64 Correct 735 ms 161268 KB Output is correct
65 Correct 811 ms 158252 KB Output is correct
66 Correct 729 ms 160800 KB Output is correct
67 Correct 808 ms 158100 KB Output is correct
68 Correct 556 ms 99140 KB Output is correct
69 Correct 454 ms 72236 KB Output is correct
70 Correct 262 ms 20956 KB Output is correct
71 Correct 190 ms 17208 KB Output is correct
72 Correct 279 ms 24348 KB Output is correct
73 Correct 193 ms 17560 KB Output is correct
74 Correct 524 ms 107652 KB Output is correct
75 Correct 464 ms 107512 KB Output is correct
76 Correct 451 ms 107212 KB Output is correct
77 Correct 347 ms 63148 KB Output is correct
78 Correct 256 ms 17748 KB Output is correct
79 Correct 583 ms 93360 KB Output is correct
80 Correct 554 ms 92528 KB Output is correct
81 Correct 383 ms 65264 KB Output is correct
82 Correct 229 ms 24448 KB Output is correct
83 Correct 170 ms 16428 KB Output is correct
84 Correct 405 ms 64960 KB Output is correct
85 Correct 421 ms 64884 KB Output is correct
86 Correct 413 ms 62572 KB Output is correct
87 Correct 408 ms 64720 KB Output is correct
88 Correct 405 ms 65180 KB Output is correct