제출 #984994

#제출 시각아이디문제언어결과실행 시간메모리
984994Br3adRegions (IOI09_regions)C++17
30 / 100
7311 ms131072 KiB
#include <iostream> #include <fstream> #include <iomanip> #include <algorithm> #include <functional> #include <numeric> #include <cstring> #include <string> #include <cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define ll long long #define ull unsigned long long #define f first #define s second #define PF push_front #define PB push_back #define MP make_pair #define max(a, b) ((a > b)? a : b) #define min(a, b) ((a < b)? a : b) #define max3(a, b, c) max(max(a, b), c) #define min3(a, b, c) min(min(a, b), c) const int N = 2e5 + 5; const int M = 1e9 + 7; const int inf = 0x3f3f3f3f; const ll int INF = 1e18; struct BIT { int sz; vector<int> tree; BIT(int n) : sz(n+1), tree(n+1, 0) {}; void add(int pos, int val){ for(; pos < sz; pos += pos&-pos) tree[pos] += val; } int sum(int pos){ int ans = 0; for(; pos >= 1; pos -= pos&-pos) ans += tree[pos]; return ans; } }; vector<vector<int>> adj(N, vector<int>()); vector<int> to(N), tin(N), sz(N, 1); int timer = 1; void dfs(int cur, int prev){ tin[cur] = timer++; for(int child : adj[cur]){ if(child == prev) continue; dfs(child, cur); sz[cur] += sz[child]; } } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); // ifstream cin(); // ofstream cout(); int n, r, q, reg[N]; cin >> n >> r >> q >> reg[1]; vector<vector<int>> region(r+1, vector<int>()); region[reg[1]].PB(1); for(int i = 2; i <= n; i++){ cin >> to[i] >> reg[i]; region[reg[i]].PB(i); adj[i].PB(to[i]); adj[to[i]].PB(i); } dfs(1, -1); vector<BIT> bit(r+1, BIT(n)); // O(nlogn) pre-build fenwick tree for(int i = 1; i <= n; i++){ bit[reg[i]].add(tin[i], 1); bit[reg[i]].add(tin[i] + sz[i], -1); } while(q--){ int r1, r2; cin >> r1 >> r2; // O(max(r2)logn) = O(nlogn) query... too slow // Search for how many ancestor with region r above a person i can be achieved in O(logn) ll num = 0; for(int i : region[r2]){ num += bit[r1].sum(tin[i]); } cout << num << endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...