답안 #914817

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
914817 2024-01-22T17:38:55 Z nima_aryan Inside information (BOI21_servers) C++17
100 / 100
567 ms 35524 KB
/**
 *    author:  NimaAryan
 *    created: 2024-01-22 15:04:53
**/
#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template <typename T, class C = less<>>
using indexed_set = tree<T, null_type, C, rb_tree_tag,
      tree_order_statistics_node_update>;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

struct Solver {
  int n, k;
  vector<vector<pair<int, int>>> adj;

  vector<vector<pair<int, int>>> qry1;
  vector<vector<int>> qry2;

  vector<int> siz;
  vector<bool> alive;

  vector<int> join;
  vector<int> leave;
  vector<int> reach;

  vector<int> label;

  vector<int> ans;

  Solver(int n, int k) : n(n), k(k) {
    adj.resize(n);

    qry1.resize(n);
    qry2.resize(n);

    siz.resize(n);
    alive.assign(n, true);

    join.assign(n, -1);
    leave.assign(n, -1);
    reach.assign(n, -1);

    label.resize(n);

    ans.resize(n - 1 + k);
  }

  void add_edge(int a, int b, int w) {
    adj[a].emplace_back(b, w);
    adj[b].emplace_back(a, w);
  }

  void add_query1(int a, int d, int t) {
    if (a == d) {
      ans[t] = -1;
      return;
    }
    qry1[d].emplace_back(a, t);
  }
  void add_query2(int d, int t) {
    qry2[d].push_back(t);
  }

  void dfs(int v, int p = -1) {
    siz[v] = 1;
    for (auto [u, _] : adj[v]) {
      if (u != p && alive[u]) {
        dfs(u, v);
        siz[v] += siz[u];
      }
    }
    leave[v] = -1;
    join[v] = reach[v] = n + k;
  }
  int find_centroid(int v, int z, int p = -1) {
    for (auto [u, _] : adj[v]) {
      if (u != p && alive[u] && siz[u] > z / 2) {
        return find_centroid(u, z, v);
      }
    }
    return v;
  }

  void solve(int v = 0) {
    dfs(v);
    int c = find_centroid(v, siz[v]);

    {
      /* increasing */
      auto downward = [&](auto self, int x, int p) -> void {
        for (auto [y, z] : adj[x]) {
          if (y != p && alive[y] && reach[x] < z) {
            leave[y] = leave[x];
            reach[y] = z;
            self(self, y, x);
          }
        }
      };
      for (auto [u, w] : adj[c]) {
        if (alive[u]) {
          leave[u] = reach[u] = w;
          downward(downward, u, c);
        }
      }
    }
    {
      /* decreasing */
      auto upward = [&](auto self, int x, int w, int p) -> void {
        for (auto [y, z] : adj[x]) {
          if (y != p && alive[y] && w > z) {
            join[y] = join[x];
            self(self, y, z, x);
          }
        }
      };
      for (auto [u, w] : adj[c]) {
        if (alive[u]) {
          join[u] = w;
          upward(upward, u, w, c);
        }
      }
    }

    sort(adj[c].begin(), adj[c].end(), [&](auto p, auto q) {
      return p.second > q.second;
    });

    vector<vector<int>> subs;
    auto trav = [&](auto self, int x, int p) -> void {
      subs.back().push_back(x);
      label[x] = subs.size();
      for (auto [y, _] : adj[x]) {
        if (y != p && alive[y]) {
          self(self, y, x);
        }
      }
    };
    for (auto [u, _] : adj[c]) {
      if (alive[u]) {
        subs.emplace_back();
        trav(trav, u, c);
      }
    }

    indexed_set<int> can;
    for (auto s : subs) {
      for (int d : s) {
        for (auto [a, t] : qry1[d]) {
          if (ans[t]) {
            continue;
          }
          if (a == c) {
            ans[t] = -1 + -(join[d] >= t);
            continue;
          }
          if (label[a] == label[d]) {
            continue;
          }
          ans[t] = -1 + -(join[d] >= leave[a] || reach[a] >= t);
        }
        for (int t : qry2[d]) {
          if (join[d] < t) {
            ans[t] += can.order_of_key(t) + 1;
          }
        }
      }
      for (int x : s) {
        can.insert(reach[x]);
      }
    }

    {
      int d = c;
      for (auto [a, t] : qry1[d]) {
        if (ans[t]) {
          continue;
        }
        ans[t] = -1 + -(reach[a] >= t);
      }
      for (int t : qry2[d]) {
        ans[t] += can.order_of_key(t) + 1;
      }
    }

    alive[c] = false;
    for (auto [u, _] : adj[c]) {
      if (alive[u]) {
        solve(u);
      }
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;

  Solver t(n, k);
  for (int i = 0; i < n - 1 + k; ++i) {
    char op;
    cin >> op;
    if (op == 'S') {
      int a, b;
      cin >> a >> b;
      --a, --b;
      t.add_edge(a, b, i);
    } else if (op == 'Q') {
      int a, d;
      cin >> a >> d;
      --a, --d;
      t.add_query1(a, d, i);
    } else {
      int d;
      cin >> d;
      --d;
      t.add_query2(d, i);
    }
  }
  t.solve();

  for (int i = 0; i < n - 1 + k; ++i) {
    int res = t.ans[i];
    if (res) {
      if (res > 0) {
        cout << res << "\n";
      } else {
        cout << (res == -1 ? "yes" : "no") << "\n";
      }
    }
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 3676 KB Output is correct
2 Correct 31 ms 4428 KB Output is correct
3 Correct 25 ms 4380 KB Output is correct
4 Correct 36 ms 4444 KB Output is correct
5 Correct 32 ms 4564 KB Output is correct
6 Correct 27 ms 4520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 3676 KB Output is correct
2 Correct 31 ms 4428 KB Output is correct
3 Correct 25 ms 4380 KB Output is correct
4 Correct 36 ms 4444 KB Output is correct
5 Correct 32 ms 4564 KB Output is correct
6 Correct 27 ms 4520 KB Output is correct
7 Correct 17 ms 3412 KB Output is correct
8 Correct 30 ms 3924 KB Output is correct
9 Correct 25 ms 3924 KB Output is correct
10 Correct 32 ms 4132 KB Output is correct
11 Correct 31 ms 4176 KB Output is correct
12 Correct 25 ms 4188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 3544 KB Output is correct
2 Correct 224 ms 33212 KB Output is correct
3 Correct 255 ms 33012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 3544 KB Output is correct
2 Correct 224 ms 33212 KB Output is correct
3 Correct 255 ms 33012 KB Output is correct
4 Correct 18 ms 3420 KB Output is correct
5 Correct 245 ms 33032 KB Output is correct
6 Correct 197 ms 30908 KB Output is correct
7 Correct 243 ms 30964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3676 KB Output is correct
2 Correct 322 ms 26168 KB Output is correct
3 Correct 303 ms 26252 KB Output is correct
4 Correct 417 ms 32876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3676 KB Output is correct
2 Correct 322 ms 26168 KB Output is correct
3 Correct 303 ms 26252 KB Output is correct
4 Correct 417 ms 32876 KB Output is correct
5 Correct 18 ms 3532 KB Output is correct
6 Correct 320 ms 26576 KB Output is correct
7 Correct 422 ms 32764 KB Output is correct
8 Correct 304 ms 26460 KB Output is correct
9 Correct 285 ms 26212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3564 KB Output is correct
2 Correct 353 ms 29384 KB Output is correct
3 Correct 241 ms 21968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3564 KB Output is correct
2 Correct 353 ms 29384 KB Output is correct
3 Correct 241 ms 21968 KB Output is correct
4 Correct 17 ms 3408 KB Output is correct
5 Correct 346 ms 29472 KB Output is correct
6 Correct 262 ms 22272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3676 KB Output is correct
2 Correct 325 ms 26188 KB Output is correct
3 Correct 294 ms 26468 KB Output is correct
4 Correct 405 ms 32960 KB Output is correct
5 Correct 18 ms 3676 KB Output is correct
6 Correct 371 ms 29384 KB Output is correct
7 Correct 246 ms 21964 KB Output is correct
8 Correct 306 ms 23424 KB Output is correct
9 Correct 326 ms 22828 KB Output is correct
10 Correct 423 ms 27652 KB Output is correct
11 Correct 400 ms 27028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3676 KB Output is correct
2 Correct 325 ms 26188 KB Output is correct
3 Correct 294 ms 26468 KB Output is correct
4 Correct 405 ms 32960 KB Output is correct
5 Correct 18 ms 3676 KB Output is correct
6 Correct 371 ms 29384 KB Output is correct
7 Correct 246 ms 21964 KB Output is correct
8 Correct 306 ms 23424 KB Output is correct
9 Correct 326 ms 22828 KB Output is correct
10 Correct 423 ms 27652 KB Output is correct
11 Correct 400 ms 27028 KB Output is correct
12 Correct 27 ms 3528 KB Output is correct
13 Correct 291 ms 26572 KB Output is correct
14 Correct 403 ms 32608 KB Output is correct
15 Correct 271 ms 26212 KB Output is correct
16 Correct 280 ms 26396 KB Output is correct
17 Correct 18 ms 3672 KB Output is correct
18 Correct 349 ms 29468 KB Output is correct
19 Correct 266 ms 22196 KB Output is correct
20 Correct 290 ms 23500 KB Output is correct
21 Correct 289 ms 23356 KB Output is correct
22 Correct 460 ms 27772 KB Output is correct
23 Correct 567 ms 28484 KB Output is correct
24 Correct 479 ms 28556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 3500 KB Output is correct
2 Correct 30 ms 4416 KB Output is correct
3 Correct 40 ms 4416 KB Output is correct
4 Correct 48 ms 4436 KB Output is correct
5 Correct 33 ms 4444 KB Output is correct
6 Correct 31 ms 4692 KB Output is correct
7 Correct 29 ms 3664 KB Output is correct
8 Correct 213 ms 32984 KB Output is correct
9 Correct 229 ms 32960 KB Output is correct
10 Correct 18 ms 3668 KB Output is correct
11 Correct 271 ms 26236 KB Output is correct
12 Correct 280 ms 26440 KB Output is correct
13 Correct 393 ms 32868 KB Output is correct
14 Correct 17 ms 3596 KB Output is correct
15 Correct 351 ms 29468 KB Output is correct
16 Correct 256 ms 21964 KB Output is correct
17 Correct 309 ms 23552 KB Output is correct
18 Correct 307 ms 22648 KB Output is correct
19 Correct 409 ms 27292 KB Output is correct
20 Correct 411 ms 27184 KB Output is correct
21 Correct 309 ms 35524 KB Output is correct
22 Correct 284 ms 30840 KB Output is correct
23 Correct 257 ms 23520 KB Output is correct
24 Correct 243 ms 23520 KB Output is correct
25 Correct 330 ms 32416 KB Output is correct
26 Correct 259 ms 20988 KB Output is correct
27 Correct 250 ms 20648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 3500 KB Output is correct
2 Correct 30 ms 4416 KB Output is correct
3 Correct 40 ms 4416 KB Output is correct
4 Correct 48 ms 4436 KB Output is correct
5 Correct 33 ms 4444 KB Output is correct
6 Correct 31 ms 4692 KB Output is correct
7 Correct 29 ms 3664 KB Output is correct
8 Correct 213 ms 32984 KB Output is correct
9 Correct 229 ms 32960 KB Output is correct
10 Correct 18 ms 3668 KB Output is correct
11 Correct 271 ms 26236 KB Output is correct
12 Correct 280 ms 26440 KB Output is correct
13 Correct 393 ms 32868 KB Output is correct
14 Correct 17 ms 3596 KB Output is correct
15 Correct 351 ms 29468 KB Output is correct
16 Correct 256 ms 21964 KB Output is correct
17 Correct 309 ms 23552 KB Output is correct
18 Correct 307 ms 22648 KB Output is correct
19 Correct 409 ms 27292 KB Output is correct
20 Correct 411 ms 27184 KB Output is correct
21 Correct 309 ms 35524 KB Output is correct
22 Correct 284 ms 30840 KB Output is correct
23 Correct 257 ms 23520 KB Output is correct
24 Correct 243 ms 23520 KB Output is correct
25 Correct 330 ms 32416 KB Output is correct
26 Correct 259 ms 20988 KB Output is correct
27 Correct 250 ms 20648 KB Output is correct
28 Correct 18 ms 3420 KB Output is correct
29 Correct 30 ms 3928 KB Output is correct
30 Correct 26 ms 3928 KB Output is correct
31 Correct 32 ms 4164 KB Output is correct
32 Correct 31 ms 4176 KB Output is correct
33 Correct 25 ms 4380 KB Output is correct
34 Correct 18 ms 3420 KB Output is correct
35 Correct 208 ms 33020 KB Output is correct
36 Correct 178 ms 30656 KB Output is correct
37 Correct 167 ms 31052 KB Output is correct
38 Correct 17 ms 3416 KB Output is correct
39 Correct 286 ms 26424 KB Output is correct
40 Correct 406 ms 32360 KB Output is correct
41 Correct 276 ms 26364 KB Output is correct
42 Correct 292 ms 26408 KB Output is correct
43 Correct 17 ms 3420 KB Output is correct
44 Correct 343 ms 29472 KB Output is correct
45 Correct 263 ms 22304 KB Output is correct
46 Correct 291 ms 23500 KB Output is correct
47 Correct 332 ms 23356 KB Output is correct
48 Correct 382 ms 27592 KB Output is correct
49 Correct 537 ms 28460 KB Output is correct
50 Correct 500 ms 28512 KB Output is correct
51 Correct 188 ms 34212 KB Output is correct
52 Correct 156 ms 31328 KB Output is correct
53 Correct 186 ms 31028 KB Output is correct
54 Correct 166 ms 31340 KB Output is correct
55 Correct 179 ms 31640 KB Output is correct
56 Correct 218 ms 22672 KB Output is correct
57 Correct 268 ms 29764 KB Output is correct
58 Correct 310 ms 22332 KB Output is correct
59 Correct 238 ms 21128 KB Output is correct