Submission #337342

#TimeUsernameProblemLanguageResultExecution timeMemory
337342arborRegions (IOI09_regions)C++17
100 / 100
2929 ms79332 KiB
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; using ll = long long; using pii = pair<int, int>; const int MN = 2e5 + 5, MR = 2.5e4 + 5; int N, R, Q, a[MN], sz[MN], tin[MN], tout[MN], tt; int lb[MR], sub[MN], cnt1[450][MR], cnt2[MR][450]; vector<int> g[MN]; vector<pii> rg[MR]; void tour(int u) { tin[u] = ++tt; for (int v : g[u]) tour(v); tout[u] = tt; rg[a[u]].emplace_back(tin[u], tout[u]); } void go(int u, int r, int cnt) { cnt1[lb[r]][a[u]] += cnt; sub[u] = a[u] == r; for (int v : g[u]) { go(v, r, cnt + (a[v] == r)); sub[u] += sub[v]; } cnt2[a[u]][lb[r]] += sub[u]; } int solve(int r1, int r2) { int p1 = 0, p2 = 0, ret = 0; stack<pii> st; while (p2 < rg[r2].size()) { if (p1 == rg[r1].size() || rg[r1][p1].first > rg[r2][p2].first) { while (!st.empty() && st.top().second < rg[r2][p2].first) st.pop(); p2++; ret += st.size(); } else if (rg[r1][p1].first < rg[r2][p2].first) { while (!st.empty() && st.top().second < rg[r1][p1].first) st.pop(); st.push(rg[r1][p1]); p1++; } } return ret; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> N >> R >> Q >> a[1]; int B = sqrt(N); for (int i = 2; i <= N; i++) { int p; cin >> p >> a[i]; g[p].push_back(i); sz[a[i]]++; } tour(1); int id = 0; for (int i = 1; i <= R; i++) { if (sz[i] >= B) { lb[i] = ++id; go(1, i, a[1] == i); } sort(all(rg[i])); } for (int i = 0; i < Q; i++) { int r1, r2; cin >> r1 >> r2; if (sz[r1] >= B) cout << cnt1[lb[r1]][r2] << endl; else if (sz[r2] >= B) cout << cnt2[r1][lb[r2]] << endl; else cout << solve(r1, r2) << endl; } return 0; }

Compilation message (stderr)

regions.cpp: In function 'int solve(int, int)':
regions.cpp:32:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     while (p2 < rg[r2].size()) {
      |            ~~~^~~~~~~~~~~~~~~
regions.cpp:33:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         if (p1 == rg[r1].size() || rg[r1][p1].first > rg[r2][p2].first) {
      |             ~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...