답안 #511188

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
511188 2022-01-15T11:17:51 Z 600Mihnea Regions (IOI09_regions) C++17
100 / 100
4141 ms 41808 KB
#include <bits/stdc++.h>

using namespace std;

const int N = (int) 2e5 + 7;
const int R = 25000 + 7;
const int CNT = 100;
int n;
int r;
int q;
int par[N];
int id[N];
int type[N];
vector<int> g[N];
vector<int> pos[R];
vector<int> verts[R];
int low[N];
int high[N];
int top;
int solution[CNT + 7][R];
int act[CNT + 7];
int who[CNT + 7];
int y;

void build(int a) {
  low[a] = ++top;
  verts[type[a]].push_back(a);
  pos[type[a]].push_back(top);
  for (auto &b : g[a]) {
    build(b);
  }
  high[a] = top;
}

int getcnt(int prefix, int value) {
  int sol = 0, low = 0, high = (int) pos[value].size() - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (pos[value][mid] <= prefix) {
      sol = mid + 1;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return sol;
}

void dfs(int a) {
  int t = type[a];
  if (id[t]) {
    act[id[t]]++;
  }
  for (auto &b : g[a]) {
    dfs(b);
  }
  for (int j = 1; j <= y; j++) {
    solution[j][type[a]] += act[j];
  }
  if (id[t]) {
    act[id[t]]--;
  }
}

signed main() {
  ///freopen ("input", "r", stdin);

  cin >> n >> r >> q;
  cin >> type[1];
  for (int i = 2; i <= n; i++) {
    cin >> par[i] >> type[i];
    g[par[i]].push_back(i);
  }
  build(1);
  vector<pair<int, int>> ord;
  for (int i = 1; i <= n; i++) {
    ord.push_back({(int) verts[i].size(), i});
  }
  sort(ord.rbegin(), ord.rend());
  for (int j = 0; j < min(n, CNT); j++) {
    int i = ord[j].second;
    y++;
    id[i] = y;
    who[y] = i;
  }
  dfs(1);

  for (int iq = 1; iq <= q; iq++) {
    int a, b;
    cin >> a >> b;
    if (id[a]) {
      cout << solution[id[a]][b] << "\n";
      continue;
    }
    int sol = 0;
    for (auto &v : verts[a]) {
      sol += getcnt(high[v], b) - getcnt(low[v] - 1, b);
    }
    cout << sol << endl;
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6216 KB Output is correct
2 Correct 3 ms 6216 KB Output is correct
3 Correct 4 ms 6344 KB Output is correct
4 Correct 8 ms 6600 KB Output is correct
5 Correct 14 ms 6600 KB Output is correct
6 Correct 19 ms 6728 KB Output is correct
7 Correct 33 ms 6728 KB Output is correct
8 Correct 32 ms 6856 KB Output is correct
9 Correct 49 ms 7368 KB Output is correct
10 Correct 95 ms 7496 KB Output is correct
11 Correct 124 ms 7844 KB Output is correct
12 Correct 145 ms 8524 KB Output is correct
13 Correct 177 ms 8160 KB Output is correct
14 Correct 248 ms 8768 KB Output is correct
15 Correct 258 ms 11992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1510 ms 12520 KB Output is correct
2 Correct 1569 ms 11312 KB Output is correct
3 Correct 2330 ms 14820 KB Output is correct
4 Correct 263 ms 10396 KB Output is correct
5 Correct 426 ms 12740 KB Output is correct
6 Correct 771 ms 13156 KB Output is correct
7 Correct 1000 ms 15592 KB Output is correct
8 Correct 1168 ms 21820 KB Output is correct
9 Correct 2013 ms 24196 KB Output is correct
10 Correct 3166 ms 34000 KB Output is correct
11 Correct 4141 ms 27904 KB Output is correct
12 Correct 1427 ms 26016 KB Output is correct
13 Correct 2193 ms 26540 KB Output is correct
14 Correct 2317 ms 27700 KB Output is correct
15 Correct 3161 ms 34708 KB Output is correct
16 Correct 2904 ms 41808 KB Output is correct
17 Correct 2916 ms 38668 KB Output is correct