Submission #1080836

# Submission time Handle Problem Language Result Execution time Memory
1080836 2024-08-29T14:45:01 Z juicy Regions (IOI09_regions) C++17
100 / 100
1643 ms 32848 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5, R = 25005, S = 450;

int n, r, q;
int h[N], tin[N], tout[N], down[S][R], up[S][R], pr[N], ind[N], f[N];
vector<int> g[N], pos[R];
vector<array<int, 2>> events[R];

int order;

void dfs(int u) {
  tin[u] = ++order;
  for (int v : g[u]) {
    dfs(v);
  }
  tout[u] = order;
}

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

  cin >> n >> r >> q;
  for (int i = 1; i <= n; ++i) {
    if (i > 1) {
      cin >> pr[i] >> h[i];
      g[pr[i]].push_back(i);
    } else {
      cin >> h[i];
    }
    pos[h[i]].push_back(i);
  }
  dfs(1);
  int k = 0;
  for (int i = 1; i <= r; ++i) {
    if (pos[i].size() >= S) {
      ind[i] = ++k;
      fill(f + 1, f + n + 1, 0);
      for (auto x : pos[i]) {
        ++f[x];
      }
      for (int j = 1; j <= n; ++j) {
        f[j] += f[pr[j]];
        down[k][h[j]] += f[j];
      }
      fill(f + 1, f + n + 1, 0);
      for (auto x : pos[i]) {
        ++f[x];
      }
      for (int j = n; j >= 1; --j) {
        for (int x : g[j]) {
          f[j] += f[x];
        }
        up[k][h[j]] += f[j];
      }
    }
  }
  for (int i = 1; i <= r; ++i) {
    for (auto &x : pos[i]) {
      events[i].push_back({tin[x] - 1, -1});
      events[i].push_back({tout[x], 1});
      x = tin[x];
    }
    sort(pos[i].begin(), pos[i].end());
    sort(events[i].begin(), events[i].end());
  }
  while (q--) {
    int x, y; cin >> x >> y;
    if (ind[x]) {
      cout << down[ind[x]][y] << endl;
      continue;
    }
    if (ind[y]) {
      cout << up[ind[y]][x] << endl;
      continue;
    }
    int j = 0, res = 0;
    for (auto [t, d] : events[x]) {
      while (j < pos[y].size() && pos[y][j] <= t) {
        ++j;
      }
      res += d * j;
    }
    cout << res << endl;
  }
  return 0;
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:87:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |       while (j < pos[y].size() && pos[y][j] <= t) {
      |              ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6232 KB Output is correct
2 Correct 3 ms 6232 KB Output is correct
3 Correct 4 ms 6232 KB Output is correct
4 Correct 5 ms 6232 KB Output is correct
5 Correct 7 ms 6208 KB Output is correct
6 Correct 12 ms 6232 KB Output is correct
7 Correct 14 ms 6232 KB Output is correct
8 Correct 23 ms 6488 KB Output is correct
9 Correct 36 ms 6744 KB Output is correct
10 Correct 47 ms 7000 KB Output is correct
11 Correct 75 ms 7256 KB Output is correct
12 Correct 76 ms 7768 KB Output is correct
13 Correct 116 ms 7768 KB Output is correct
14 Correct 129 ms 8612 KB Output is correct
15 Correct 144 ms 10576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 601 ms 12512 KB Output is correct
2 Correct 655 ms 11680 KB Output is correct
3 Correct 1082 ms 14052 KB Output is correct
4 Correct 149 ms 8536 KB Output is correct
5 Correct 233 ms 9560 KB Output is correct
6 Correct 381 ms 13904 KB Output is correct
7 Correct 716 ms 12880 KB Output is correct
8 Correct 604 ms 24400 KB Output is correct
9 Correct 1131 ms 19024 KB Output is correct
10 Correct 1383 ms 32848 KB Output is correct
11 Correct 1643 ms 19628 KB Output is correct
12 Correct 720 ms 21272 KB Output is correct
13 Correct 901 ms 21148 KB Output is correct
14 Correct 1107 ms 24852 KB Output is correct
15 Correct 1557 ms 24532 KB Output is correct
16 Correct 1544 ms 26968 KB Output is correct
17 Correct 1481 ms 30292 KB Output is correct