Submission #397832

#TimeUsernameProblemLanguageResultExecution timeMemory
397832sinamhdvRegions (IOI09_regions)C++11
100 / 100
4352 ms87460 KiB
// IOI09_regions #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int mod = 1000 * 1000 * 1000 + 7; const int INF = 1e9 + 100; const ll LINF = 1e18 + 100; #ifdef DEBUG #define dbg(x) cout << #x << " = " << (x) << endl << flush; #define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; } #else #define dbg(x) ; #define dbgr(s, f) ; #endif #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define pb push_back #define mp make_pair #define fr first #define sc second #define MAXN 200100 #define SQRT 120 #define BLK (MAXN / SQRT + 10) int n, rc, q; int a[MAXN]; vector<int> col[MAXN]; int par[MAXN]; int precnt[BLK][MAXN]; vector<int> cld[MAXN]; vector<int> big; int sttime[MAXN], fntime[MAXN], timer; int cnt[MAXN]; int colcnt[MAXN]; void dfs(int v) { FOR(i, 0, (int)big.size()) if (big[i] != a[v]) precnt[i][a[v]] += cnt[big[i]]; col[a[v]].pb(v); sttime[v] = timer++; for (int u : cld[v]) { cnt[a[v]]++; dfs(u); cnt[a[v]]--; } fntime[v] = timer; } bool cmp(int x, int y) { return sttime[x] < sttime[y]; } int32_t main(void) { fast_io; cin >> n >> rc >> q; cin >> a[1]; colcnt[a[1]]++; FOR(i, 2, n + 1) { cin >> par[i] >> a[i]; colcnt[a[i]]++; cld[par[i]].pb(i); } FOR(i, 1, rc + 1) if (colcnt[i] > SQRT) big.pb(i); dfs(1); dbgr(big.begin(), big.end()); dbgr(sttime + 1, sttime + n + 1); dbgr(fntime + 1, fntime + n + 1); while (q--) { int x, y; cin >> x >> y; if (colcnt[x] > SQRT) { x = lower_bound(all(big), x) - big.begin(); cout << precnt[x][y] << endl; continue; } int ans = 0; for (int u : col[x]) { sttime[MAXN - 1] = fntime[u] - 1; ans += upper_bound(all(col[y]), MAXN - 1, cmp) - lower_bound(all(col[y]), u, cmp); } cout << ans << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...