답안 #807552

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
807552 2023-08-04T19:17:14 Z dong_liu Inside information (BOI21_servers) C++17
100 / 100
637 ms 96724 KB
#include "bits/stdc++.h"
using namespace std;

const int N = 120000, L = 17;

int pp[L][N], dd[N], vv[N], up[N], dn[N];
vector<pair<int, int>> g[N];

void dfs(int i) {
  for (int l = 1; l < L; l++) pp[l][i] = pp[l - 1][pp[l - 1][i]];
  for (auto [j, h] : g[i])
    if (j != pp[0][i]) {
      vv[j] = h;
      if (i == 0)
        up[j] = dn[j] = i;
      else if (vv[j] > vv[i])
        up[j] = up[i], dn[j] = i;
      else
        up[j] = i, dn[j] = dn[i];
      dd[j] = dd[i] + 1;
      pp[0][j] = i;
      dfs(j);
    }
}
int jmp(int i, int k) {
  for (int l = 0; l < L; l++)
    if (k >> l & 1) i = pp[l][i];
  return i;
}
int lca(int i, int j) {
  if (dd[i] < dd[j]) swap(i, j);
  i = jmp(i, dd[i] - dd[j]);
  if (i == j) return i;
  for (int l = L - 1; l >= 0; l--)
    if (pp[l][i] != pp[l][j]) i = pp[l][i], j = pp[l][j];
  return pp[0][i];
}
bool has(int i, int j, int h) {
  if (i == j) return 1;
  int p = lca(i, j);
  if (p == i)
    return (dd[dn[j]] <= dd[i] && vv[jmp(j, dd[j] - dd[i] - 1)] < h);
  else if (p == j)
    return (dd[up[i]] <= dd[j] && vv[i] < h);
  else
    return (dd[up[i]] <= dd[p] && dd[dn[j]] <= dd[p] &&
            vv[jmp(i, dd[i] - dd[p] - 1)] > vv[jmp(j, dd[j] - dd[p] - 1)] &&
            vv[i] < h);
}

int cp[N], sz[N];
bool dead[N];
vector<int> cup[N], cen[N], eds;
vector<pair<int, int>> upd[N * 2];

struct FT {
  vector<int> t, inds;
  int all;

  void bld(vector<int> inds_) {
    inds = inds_;
    sort(inds.begin(), inds.end());
    t.assign(inds.size(), 0);
    all = 0;
  }
  void upd(int i) {
    i = lower_bound(inds.begin(), inds.end(), i) - inds.begin();
    while (i < (int)inds.size()) t[i]++, i |= i + 1;
    all++;
  }
  int qry(int i) {  // # > i
    i = lower_bound(inds.begin(), inds.end(), i) - inds.begin();
    int x = all;
    while (i >= 0) x -= t[i], i &= i + 1, i--;
    return x;
  }
} ft[N];

int dfs1(int p, int i) {
  sz[i] = 1;
  for (auto [j, h] : g[i])
    if (p != j && !dead[j]) sz[i] += dfs1(i, j);
  return sz[i];
}
int dfs2(int p, int i, int n) {
  for (auto [j, h] : g[i])
    if (p != j && !dead[j] && sz[j] * 2 > n) return dfs2(i, j, n);
  return i;
}
void dfs3(int c, int p, int i, int htop, int hlast, bool up, bool dn) {
  cen[i].push_back(c);
  cup[i].push_back(up ? htop : -1);
  if (dn) {
    upd[hlast].push_back({c, htop});
  }
  for (auto [j, h] : g[i])
    if (p != j && !dead[j]) {
      eds.push_back(h);
      dfs3(c, i, j, htop, h, up && h < hlast, dn && h > hlast);
    }
}
void cd(int p, int i) {
  i = dfs2(-1, i, dfs1(-1, i));
  cp[i] = p;
  dead[i] = 1;
  for (auto [j, h] : g[i])
    if (!dead[j]) {
      eds.push_back(h);
      dfs3(i, i, j, h, h, 1, 1);
    }
  ft[i].bld(eds);
  vector<int>().swap(eds);
  for (auto [j, h] : g[i])
    if (!dead[j]) cd(i, j);
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  vector<array<int, 3>> qry;
  for (int h = 0; h < n + q - 1; h++) {
    char t;
    cin >> t;
    if (t == 'S') {
      int i, j;
      cin >> i >> j, i--, j--;
      g[i].push_back({j, h}), g[j].push_back({i, h});
    } else if (t == 'Q') {
      int i, j;
      cin >> i >> j, i--, j--;
      qry.push_back({i, j, h});
    } else {
      int i;
      cin >> i, i--;
      qry.push_back({-1, i, h});
    }
  }
  pp[0][0] = 0;
  dfs(0);
  for (int i = 0; i < n; i++) cp[i] = -1;
  cd(-1, 0);
  for (int i = 0; i < n; i++) {
    reverse(cen[i].begin(), cen[i].end());
    reverse(cup[i].begin(), cup[i].end());
  }
  int ch = 0;
  for (int h_ = 0; h_ < q; h_++) {
    auto [i, j, h] = qry[h_];
    while (ch <= h) {
      for (auto [c, h] : upd[ch]) ft[c].upd(h);
      ch++;
    }
    if (i == -1) {
      i = j;
      int cnt = 1 + ft[i].all, c = cp[i], d = 0;
      while (~c) {
        if (~cup[i][d] && cup[i][d] < h) {
          int x = 1 + ft[c].qry(cup[i][d]);
          cnt += x;
        }
        c = cp[c], d++;
      }
      cout << cnt << '\n';
    } else {
      cout << (has(i, j, h) ? "yes\n" : "no\n");
    }
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 23892 KB Output is correct
2 Correct 45 ms 25484 KB Output is correct
3 Correct 42 ms 25196 KB Output is correct
4 Correct 54 ms 25956 KB Output is correct
5 Correct 47 ms 26060 KB Output is correct
6 Correct 40 ms 25188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 23892 KB Output is correct
2 Correct 45 ms 25484 KB Output is correct
3 Correct 42 ms 25196 KB Output is correct
4 Correct 54 ms 25956 KB Output is correct
5 Correct 47 ms 26060 KB Output is correct
6 Correct 40 ms 25188 KB Output is correct
7 Correct 34 ms 23924 KB Output is correct
8 Correct 44 ms 25204 KB Output is correct
9 Correct 40 ms 24992 KB Output is correct
10 Correct 50 ms 25664 KB Output is correct
11 Correct 48 ms 25692 KB Output is correct
12 Correct 39 ms 25060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 23896 KB Output is correct
2 Correct 171 ms 54360 KB Output is correct
3 Correct 157 ms 54384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 23896 KB Output is correct
2 Correct 171 ms 54360 KB Output is correct
3 Correct 157 ms 54384 KB Output is correct
4 Correct 34 ms 23832 KB Output is correct
5 Correct 158 ms 54316 KB Output is correct
6 Correct 153 ms 53464 KB Output is correct
7 Correct 143 ms 53672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 23816 KB Output is correct
2 Correct 427 ms 87464 KB Output is correct
3 Correct 412 ms 87520 KB Output is correct
4 Correct 327 ms 96572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 23816 KB Output is correct
2 Correct 427 ms 87464 KB Output is correct
3 Correct 412 ms 87520 KB Output is correct
4 Correct 327 ms 96572 KB Output is correct
5 Correct 28 ms 23816 KB Output is correct
6 Correct 419 ms 86964 KB Output is correct
7 Correct 345 ms 95340 KB Output is correct
8 Correct 402 ms 86640 KB Output is correct
9 Correct 409 ms 86524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 23888 KB Output is correct
2 Correct 300 ms 86564 KB Output is correct
3 Correct 386 ms 79504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 23888 KB Output is correct
2 Correct 300 ms 86564 KB Output is correct
3 Correct 386 ms 79504 KB Output is correct
4 Correct 31 ms 23776 KB Output is correct
5 Correct 395 ms 86132 KB Output is correct
6 Correct 388 ms 79048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 23908 KB Output is correct
2 Correct 398 ms 87376 KB Output is correct
3 Correct 401 ms 87420 KB Output is correct
4 Correct 333 ms 96724 KB Output is correct
5 Correct 32 ms 23864 KB Output is correct
6 Correct 297 ms 86580 KB Output is correct
7 Correct 398 ms 79640 KB Output is correct
8 Correct 442 ms 75308 KB Output is correct
9 Correct 484 ms 75220 KB Output is correct
10 Correct 522 ms 87344 KB Output is correct
11 Correct 516 ms 87296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 23908 KB Output is correct
2 Correct 398 ms 87376 KB Output is correct
3 Correct 401 ms 87420 KB Output is correct
4 Correct 333 ms 96724 KB Output is correct
5 Correct 32 ms 23864 KB Output is correct
6 Correct 297 ms 86580 KB Output is correct
7 Correct 398 ms 79640 KB Output is correct
8 Correct 442 ms 75308 KB Output is correct
9 Correct 484 ms 75220 KB Output is correct
10 Correct 522 ms 87344 KB Output is correct
11 Correct 516 ms 87296 KB Output is correct
12 Correct 28 ms 23872 KB Output is correct
13 Correct 469 ms 87044 KB Output is correct
14 Correct 342 ms 95324 KB Output is correct
15 Correct 403 ms 86536 KB Output is correct
16 Correct 430 ms 86772 KB Output is correct
17 Correct 32 ms 23868 KB Output is correct
18 Correct 340 ms 86168 KB Output is correct
19 Correct 399 ms 79108 KB Output is correct
20 Correct 520 ms 75088 KB Output is correct
21 Correct 476 ms 75116 KB Output is correct
22 Correct 537 ms 86740 KB Output is correct
23 Correct 637 ms 90456 KB Output is correct
24 Correct 569 ms 89856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 23872 KB Output is correct
2 Correct 45 ms 25524 KB Output is correct
3 Correct 41 ms 25276 KB Output is correct
4 Correct 53 ms 25936 KB Output is correct
5 Correct 44 ms 26028 KB Output is correct
6 Correct 39 ms 25196 KB Output is correct
7 Correct 34 ms 23896 KB Output is correct
8 Correct 154 ms 54364 KB Output is correct
9 Correct 153 ms 54328 KB Output is correct
10 Correct 27 ms 23856 KB Output is correct
11 Correct 403 ms 87420 KB Output is correct
12 Correct 379 ms 87480 KB Output is correct
13 Correct 361 ms 96672 KB Output is correct
14 Correct 31 ms 23896 KB Output is correct
15 Correct 291 ms 86564 KB Output is correct
16 Correct 411 ms 79664 KB Output is correct
17 Correct 449 ms 75276 KB Output is correct
18 Correct 445 ms 75192 KB Output is correct
19 Correct 510 ms 87376 KB Output is correct
20 Correct 509 ms 87348 KB Output is correct
21 Correct 232 ms 55020 KB Output is correct
22 Correct 202 ms 55016 KB Output is correct
23 Correct 395 ms 62656 KB Output is correct
24 Correct 393 ms 62444 KB Output is correct
25 Correct 436 ms 74132 KB Output is correct
26 Correct 404 ms 70004 KB Output is correct
27 Correct 380 ms 69896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 23872 KB Output is correct
2 Correct 45 ms 25524 KB Output is correct
3 Correct 41 ms 25276 KB Output is correct
4 Correct 53 ms 25936 KB Output is correct
5 Correct 44 ms 26028 KB Output is correct
6 Correct 39 ms 25196 KB Output is correct
7 Correct 34 ms 23896 KB Output is correct
8 Correct 154 ms 54364 KB Output is correct
9 Correct 153 ms 54328 KB Output is correct
10 Correct 27 ms 23856 KB Output is correct
11 Correct 403 ms 87420 KB Output is correct
12 Correct 379 ms 87480 KB Output is correct
13 Correct 361 ms 96672 KB Output is correct
14 Correct 31 ms 23896 KB Output is correct
15 Correct 291 ms 86564 KB Output is correct
16 Correct 411 ms 79664 KB Output is correct
17 Correct 449 ms 75276 KB Output is correct
18 Correct 445 ms 75192 KB Output is correct
19 Correct 510 ms 87376 KB Output is correct
20 Correct 509 ms 87348 KB Output is correct
21 Correct 232 ms 55020 KB Output is correct
22 Correct 202 ms 55016 KB Output is correct
23 Correct 395 ms 62656 KB Output is correct
24 Correct 393 ms 62444 KB Output is correct
25 Correct 436 ms 74132 KB Output is correct
26 Correct 404 ms 70004 KB Output is correct
27 Correct 380 ms 69896 KB Output is correct
28 Correct 32 ms 23812 KB Output is correct
29 Correct 46 ms 25112 KB Output is correct
30 Correct 39 ms 25012 KB Output is correct
31 Correct 49 ms 25564 KB Output is correct
32 Correct 45 ms 25696 KB Output is correct
33 Correct 39 ms 24988 KB Output is correct
34 Correct 33 ms 23832 KB Output is correct
35 Correct 164 ms 54304 KB Output is correct
36 Correct 131 ms 53428 KB Output is correct
37 Correct 133 ms 53632 KB Output is correct
38 Correct 28 ms 23764 KB Output is correct
39 Correct 406 ms 87028 KB Output is correct
40 Correct 340 ms 95292 KB Output is correct
41 Correct 405 ms 86584 KB Output is correct
42 Correct 403 ms 86528 KB Output is correct
43 Correct 31 ms 23844 KB Output is correct
44 Correct 337 ms 86176 KB Output is correct
45 Correct 448 ms 79112 KB Output is correct
46 Correct 491 ms 75208 KB Output is correct
47 Correct 465 ms 75116 KB Output is correct
48 Correct 597 ms 86536 KB Output is correct
49 Correct 634 ms 90512 KB Output is correct
50 Correct 598 ms 89876 KB Output is correct
51 Correct 173 ms 54776 KB Output is correct
52 Correct 146 ms 54404 KB Output is correct
53 Correct 126 ms 53996 KB Output is correct
54 Correct 141 ms 54644 KB Output is correct
55 Correct 178 ms 54404 KB Output is correct
56 Correct 370 ms 62716 KB Output is correct
57 Correct 431 ms 73968 KB Output is correct
58 Correct 475 ms 70316 KB Output is correct
59 Correct 412 ms 69508 KB Output is correct