답안 #994479

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
994479 2024-06-07T17:10:13 Z abczz Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
626 ms 162704 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#define ll long long
 
using namespace std;
 
struct Node{
  ll chl = -1;
  ll chr = -1;
  ll val = 0;
  ll lazy = 0;
};
vector <Node> st;
vector <ll> U;
ll n, f, cnt, A[200000], H[200000], C[200000], in[200000];
queue <ll> Q;
map <ll, ll> mp;
struct SegTree{
  ll get(ll u) {
    if (u == -1) {
      st.push_back({-1, -1, 0});
      return st.size()-1;
    }
    return u;
  }
  ll rt = -1;
  void init() {
    rt = get(rt);
  }
  void push(ll id) {
    if (st[id].chl != -1) {
      st[st[id].chl].val += st[id].lazy;
      st[st[id].chl].lazy += st[id].lazy;
    }
    if (st[id].chr != -1) {
      st[st[id].chr].val += st[id].lazy;
      st[st[id].chr].lazy += st[id].lazy;
    }
    st[id].lazy = 0;
  }
  ll query(ll id, ll l, ll r, ll ql, ll qr) {
    if (id == -1 || qr < l || r < ql) return 0;
    else if (ql <= l && r <= qr) return st[id].val;
    push(id);
    ll mid = (l+r)/2;
    return max(query(st[id].chl, l, mid, ql, qr), query(st[id].chr, mid+1, r, ql, qr));
  }
  void push_up(ll id) {
    st[id].val = max((st[id].chl != -1 ? st[st[id].chl].val : 0), (st[id].chr != -1 ? st[st[id].chr].val : 0));
  }
  void update(ll id, ll l, ll r, ll q, ll w) {
    if (l == r) {
      st[id].val = max(st[id].val, w);
      return;
    }
    push(id);
    ll mid = (l+r)/2;
    if (q <= mid) {
      st[id].chl = get(st[id].chl);
      update(st[id].chl, l, mid, q, w);
    }
    else {
      st[id].chr = get(st[id].chr);
      update(st[id].chr, mid+1, r, q, w);
    }
    push_up(id);
  }
  ll wa, wb;
  void init_merge(ll i, ll j) {
    wa = wb = 0;
    merge(i, j, 0, mp.size()-1);
  }
  void merge(ll a, ll b, ll l, ll r) {
    push(a);
    push(b);
    ll mid = (l+r)/2, ta, tb;
    //cout << l << " " << r << " " << "R" << " " << wa << " " << wb << endl;
    if (st[a].chr != -1) ta = st[st[a].chr].val;
    if (st[b].chr != -1) tb = st[st[b].chr].val;
    if (st[a].chr != -1 && st[b].chr != -1) {
      merge(st[a].chr, st[b].chr, mid+1, r);
    }
    if (st[a].chr != -1) wa = max(wa, ta);
    if (st[b].chr != -1) wb = max(wb, tb);
    if (st[a].chr == -1 && st[b].chr != -1) {
      st[a].chr = st[b].chr;
      st[st[a].chr].val += wa;
      st[st[a].chr].lazy += wa;
    }
    else if (st[a].chr != -1 && st[b].chr == -1) {
      st[st[a].chr].val += wb;
      st[st[a].chr].lazy += wb;
    }
    if (st[a].chl != -1) ta = st[st[a].chl].val;
    if (st[b].chl != -1) tb = st[st[b].chl].val;
    if (st[a].chl != -1 && st[b].chl != -1) {
      merge(st[a].chl, st[b].chl, l, mid);
    }
    //cout << l << " " << r << " " << "L" << " " << wa << " " << wb << endl;
    //cout << st[a].chl << " " << st[b].chl << " " << st[a].chr << " " << st[b].chr << endl;
    if (st[a].chl != -1) wa = max(wa, ta);
    if (st[b].chl != -1) wb = max(wb, tb);
    if (st[a].chl == -1 && st[b].chl != -1) {
      st[a].chl = st[b].chl;
      st[st[a].chl].val += wa;
      st[st[a].chl].lazy += wa;
    }
    else if (st[a].chl != -1 && st[b].chl == -1) {
      st[st[a].chl].val += wb;
      st[st[a].chl].lazy += wb;
    }
    //cout << st[a].val << " " << st[b].val << " ";
    wa = max(wa, st[a].val);
    wb = max(wb, st[b].val);
    if (l == r) {
      st[a].val = max(st[a].val+wb, st[b].val+wa);
      //cout << st[b].val << "*\n";
    }
    else push_up(a);
    //cout << l << " " << r << " -> " << st[a].val << " " << wa << " " << wb << endl;
  }
}ST[200000];
 
void dfs(ll i, ll id) {
  --in[i];
  U.push_back(i);
  //cout << i+1 << endl;
  /*auto tmp = ST[i].query(ST[i].rt, 0, mp.size()-1, H[i], mp.size()-1);
  ST[i].update(ST[i].rt, 0, mp.size()-1, H[i], tmp+C[i]);
  cout << i+1 << "*" << H[i] << " " << tmp+C[i] << endl;*/
  if (id == -1) id = i;
  else {
    ST[id].init_merge(ST[id].rt, ST[i].rt);
    //cout << ST[id].query(ST[id].rt, 0, mp.size()-1, 0, mp.size()-1) << "*\n";
  }
  if (in[A[i]]) {
    dfs(A[i], id);
  }
}
 
int main() {
  cin >> n;
  for (int i=0; i<n; ++i) {
    cin >> A[i] >> H[i] >> C[i];
    f += C[i];
    --A[i];
    //cout << A[i]+1 << " " << i+1 << endl;
    ++in[A[i]];
    ++mp[H[i]];
    ST[i].init();
  }
  for (auto &[x, y] : mp) {
    y = cnt++;
  }
  for (int i=0; i<n; ++i) {
    H[i] = mp[H[i]];
    if (!in[i]) Q.push(i);
  }
  while (!Q.empty()) {
    auto u = Q.front();
    Q.pop();
    //cout << in[u] << endl;
    //cout << A[u]+1 << " " << u+1 << endl;
    auto tmp = ST[u].query(ST[u].rt, 0, mp.size()-1, H[u], mp.size()-1);
    //cout << H[u] << " " << tmp << " " << tmp+C[u] << endl;
    ST[u].update(ST[u].rt, 0, mp.size()-1, H[u], tmp+C[u]);
    --in[A[u]];
    if (!in[A[u]]) Q.push(A[u]);
    ST[A[u]].init_merge(ST[A[u]].rt, ST[u].rt);
    //cout << ST[A[u]].query(ST[A[u]].rt, 0, mp.size()-1, 0, mp.size()-1) << endl;
  }
  ll id = -1;
  for (int i=0; i<n; ++i) {
    if (in[i]) {
      U.clear();
      //cout << i+1 << endl;
      dfs(i, -1);
      //cout << ST[i].query(ST[i].rt, 0, mp.size()-1, 0, mp.size()-1) << endl;
      sort(U.begin(), U.end(), [](auto a, auto b) {
        return H[a] < H[b];
      });
      for (auto u : U) {
        auto tmp = ST[i].query(ST[i].rt, 0, mp.size()-1, H[u], mp.size()-1);
        ST[i].update(ST[i].rt, 0, mp.size()-1, H[u], tmp+C[u]);
        //cout << tmp+C[u] << endl;
        //cout << u+1 << "*" << H[u] << " " << tmp+C[u] << endl;
      }
      f -= ST[i].query(ST[i].rt, 0, mp.size()-1, 0, mp.size()-1);
    }
  }
  cout << f << '\n';
}

Compilation message

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:175:6: warning: unused variable 'id' [-Wunused-variable]
  175 |   ll id = -1;
      |      ^~
worst_reporter2.cpp: In member function 'void SegTree::merge(long long int, long long int, long long int, long long int)':
worst_reporter2.cpp:79:23: warning: 'ta' may be used uninitialized in this function [-Wmaybe-uninitialized]
   79 |     ll mid = (l+r)/2, ta, tb;
      |                       ^~
worst_reporter2.cpp:79:27: warning: 'tb' may be used uninitialized in this function [-Wmaybe-uninitialized]
   79 |     ll mid = (l+r)/2, ta, tb;
      |                           ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 1 ms 10844 KB Output is correct
3 Correct 1 ms 10856 KB Output is correct
4 Correct 1 ms 11000 KB Output is correct
5 Correct 10 ms 13272 KB Output is correct
6 Correct 7 ms 11984 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 9 ms 13264 KB Output is correct
9 Correct 7 ms 13008 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 10 ms 11768 KB Output is correct
13 Correct 8 ms 11204 KB Output is correct
14 Correct 9 ms 12244 KB Output is correct
15 Correct 6 ms 11356 KB Output is correct
16 Correct 9 ms 13260 KB Output is correct
17 Correct 6 ms 11976 KB Output is correct
18 Correct 5 ms 11356 KB Output is correct
19 Correct 8 ms 13264 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 5 ms 11480 KB Output is correct
22 Correct 10 ms 16072 KB Output is correct
23 Correct 6 ms 13008 KB Output is correct
24 Correct 8 ms 13264 KB Output is correct
25 Correct 6 ms 11988 KB Output is correct
26 Correct 7 ms 11700 KB Output is correct
27 Correct 8 ms 13264 KB Output is correct
28 Correct 9 ms 13264 KB Output is correct
29 Correct 7 ms 12288 KB Output is correct
30 Correct 7 ms 11808 KB Output is correct
31 Correct 6 ms 11768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 1 ms 10844 KB Output is correct
3 Correct 1 ms 10856 KB Output is correct
4 Correct 1 ms 11000 KB Output is correct
5 Correct 10 ms 13272 KB Output is correct
6 Correct 7 ms 11984 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 9 ms 13264 KB Output is correct
9 Correct 7 ms 13008 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 10 ms 11768 KB Output is correct
13 Correct 8 ms 11204 KB Output is correct
14 Correct 9 ms 12244 KB Output is correct
15 Correct 6 ms 11356 KB Output is correct
16 Correct 9 ms 13260 KB Output is correct
17 Correct 6 ms 11976 KB Output is correct
18 Correct 5 ms 11356 KB Output is correct
19 Correct 8 ms 13264 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 5 ms 11480 KB Output is correct
22 Correct 10 ms 16072 KB Output is correct
23 Correct 6 ms 13008 KB Output is correct
24 Correct 8 ms 13264 KB Output is correct
25 Correct 6 ms 11988 KB Output is correct
26 Correct 7 ms 11700 KB Output is correct
27 Correct 8 ms 13264 KB Output is correct
28 Correct 9 ms 13264 KB Output is correct
29 Correct 7 ms 12288 KB Output is correct
30 Correct 7 ms 11808 KB Output is correct
31 Correct 6 ms 11768 KB Output is correct
32 Correct 10 ms 13260 KB Output is correct
33 Correct 618 ms 159732 KB Output is correct
34 Correct 342 ms 80980 KB Output is correct
35 Correct 626 ms 160408 KB Output is correct
36 Correct 335 ms 82088 KB Output is correct
37 Correct 182 ms 32172 KB Output is correct
38 Correct 154 ms 23744 KB Output is correct
39 Correct 364 ms 61092 KB Output is correct
40 Correct 201 ms 23732 KB Output is correct
41 Correct 146 ms 23280 KB Output is correct
42 Correct 385 ms 63396 KB Output is correct
43 Correct 198 ms 24244 KB Output is correct
44 Correct 562 ms 162200 KB Output is correct
45 Correct 302 ms 84388 KB Output is correct
46 Correct 137 ms 24164 KB Output is correct
47 Correct 460 ms 162704 KB Output is correct
48 Correct 221 ms 83108 KB Output is correct
49 Correct 153 ms 34312 KB Output is correct
50 Correct 425 ms 161864 KB Output is correct
51 Correct 240 ms 149296 KB Output is correct
52 Correct 464 ms 96920 KB Output is correct
53 Correct 235 ms 83640 KB Output is correct
54 Correct 271 ms 62652 KB Output is correct
55 Correct 342 ms 161432 KB Output is correct
56 Correct 290 ms 96004 KB Output is correct
57 Correct 266 ms 95132 KB Output is correct
58 Correct 265 ms 61160 KB Output is correct
59 Correct 279 ms 61864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 1 ms 10844 KB Output is correct
3 Correct 1 ms 10856 KB Output is correct
4 Correct 1 ms 11000 KB Output is correct
5 Correct 10 ms 13272 KB Output is correct
6 Correct 7 ms 11984 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 9 ms 13264 KB Output is correct
9 Correct 7 ms 13008 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 10 ms 11768 KB Output is correct
13 Correct 8 ms 11204 KB Output is correct
14 Correct 9 ms 12244 KB Output is correct
15 Correct 6 ms 11356 KB Output is correct
16 Correct 9 ms 13260 KB Output is correct
17 Correct 6 ms 11976 KB Output is correct
18 Correct 5 ms 11356 KB Output is correct
19 Correct 8 ms 13264 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 5 ms 11480 KB Output is correct
22 Correct 10 ms 16072 KB Output is correct
23 Correct 6 ms 13008 KB Output is correct
24 Correct 8 ms 13264 KB Output is correct
25 Correct 6 ms 11988 KB Output is correct
26 Correct 7 ms 11700 KB Output is correct
27 Correct 8 ms 13264 KB Output is correct
28 Correct 9 ms 13264 KB Output is correct
29 Correct 7 ms 12288 KB Output is correct
30 Correct 7 ms 11808 KB Output is correct
31 Correct 6 ms 11768 KB Output is correct
32 Correct 10 ms 13260 KB Output is correct
33 Correct 618 ms 159732 KB Output is correct
34 Correct 342 ms 80980 KB Output is correct
35 Correct 626 ms 160408 KB Output is correct
36 Correct 335 ms 82088 KB Output is correct
37 Correct 182 ms 32172 KB Output is correct
38 Correct 154 ms 23744 KB Output is correct
39 Correct 364 ms 61092 KB Output is correct
40 Correct 201 ms 23732 KB Output is correct
41 Correct 146 ms 23280 KB Output is correct
42 Correct 385 ms 63396 KB Output is correct
43 Correct 198 ms 24244 KB Output is correct
44 Correct 562 ms 162200 KB Output is correct
45 Correct 302 ms 84388 KB Output is correct
46 Correct 137 ms 24164 KB Output is correct
47 Correct 460 ms 162704 KB Output is correct
48 Correct 221 ms 83108 KB Output is correct
49 Correct 153 ms 34312 KB Output is correct
50 Correct 425 ms 161864 KB Output is correct
51 Correct 240 ms 149296 KB Output is correct
52 Correct 464 ms 96920 KB Output is correct
53 Correct 235 ms 83640 KB Output is correct
54 Correct 271 ms 62652 KB Output is correct
55 Correct 342 ms 161432 KB Output is correct
56 Correct 290 ms 96004 KB Output is correct
57 Correct 266 ms 95132 KB Output is correct
58 Correct 265 ms 61160 KB Output is correct
59 Correct 279 ms 61864 KB Output is correct
60 Correct 1 ms 10840 KB Output is correct
61 Correct 1 ms 10844 KB Output is correct
62 Correct 2 ms 10844 KB Output is correct
63 Correct 2 ms 10844 KB Output is correct
64 Correct 606 ms 161472 KB Output is correct
65 Correct 343 ms 82848 KB Output is correct
66 Correct 591 ms 161688 KB Output is correct
67 Correct 359 ms 84640 KB Output is correct
68 Correct 171 ms 34220 KB Output is correct
69 Correct 153 ms 24500 KB Output is correct
70 Correct 382 ms 73384 KB Output is correct
71 Correct 249 ms 31148 KB Output is correct
72 Correct 381 ms 75944 KB Output is correct
73 Correct 253 ms 36452 KB Output is correct
74 Correct 465 ms 101268 KB Output is correct
75 Correct 312 ms 83112 KB Output is correct
76 Correct 181 ms 38312 KB Output is correct
77 Correct 442 ms 101412 KB Output is correct
78 Correct 237 ms 84648 KB Output is correct
79 Correct 497 ms 98196 KB Output is correct
80 Correct 280 ms 83872 KB Output is correct
81 Correct 172 ms 34708 KB Output is correct
82 Correct 392 ms 75736 KB Output is correct
83 Correct 354 ms 161956 KB Output is correct
84 Correct 460 ms 95584 KB Output is correct
85 Correct 418 ms 95468 KB Output is correct
86 Correct 475 ms 93120 KB Output is correct
87 Correct 455 ms 96160 KB Output is correct
88 Correct 442 ms 95140 KB Output is correct