제출 #914019

#제출 시각아이디문제언어결과실행 시간메모리
914019VMaksimoski008Regions (IOI09_regions)C++14
80 / 100
8096 ms22752 KiB
#include <bits/stdc++.h> #define pb push_back #define eb emplace_back #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define uniq(x) x.erase(unique(all(x)), x.end()) #define rall(x) x.rbegin(), x.rend() //#define int long long using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1e9 + 7; const int LOG = 20; const int maxn = 2e5 + 5; const double eps = 1e-9; void setIO() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int n, r, q, home[maxn], in[maxn], out[maxn], timer = 0; vector<vector<int> > graph; vector<vector<pii> > by_home; void dfs(int u) { in[u] = timer++; for(int &v : graph[u]) dfs(v); out[u] = timer; } int32_t main() { setIO(); cin >> n >> r >> q; graph.resize(n+1); by_home.resize(r+1); cin >> home[1]; for(int i=2; i<=n; i++) { int p; cin >> p >> home[i]; graph[p].push_back(i); } dfs(1); for(int i=1; i<=n; i++) by_home[home[i]].push_back({ in[i], out[i] }); for(int i=1; i<=r; i++) sort(all(by_home[i])); while(q--) { int r1, r2, ans = 0; cin >> r1 >> r2; for(pii &u : by_home[r1]) { //cout << u.first << " " << u.second << '\n'; int l=0, r=sz(by_home[r2])-1; int p1 = 1e9; while(l <= r) { int mid = (l + r) / 2; if(by_home[r2][mid].first > u.first) p1 = mid, r = mid - 1; else l = mid + 1; } if(p1 == 1e9) continue; l=p1, r=sz(by_home[r2])-1; int p2 = 1e9; while(l <= r) { int mid = (l + r) / 2; if(by_home[r2][mid].first < u.second) p2 = mid, l = mid + 1; else r = mid - 1; } if(p2 == 1e9) continue; ans += (p2 - p1 + 1); } cout << ans << '\n'; cout.flush(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...