#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#include "debug.h"
#else
#pragma GCC optimize("O3")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define dbg(...) 42
template<typename T>ostream&operator<<(ostream&os,vector<T>&vec){for(signed i=0;i+1<vec.size();++i){os<<vec[i]<<" ";}if(vec.size()>0)os<<vec.back();return os;}
#endif
const int N = 2e5 + 6;
const int R = 25001;
int n;
int col[N];
int tin[N], tout[N];
vector<int> tree[N], tour;
void build_tour(int v, int p=-1) {
tour.push_back(v);
tin[v] = tour.size() - 1;
for (int u : tree[v]) if (u != p) {
build_tour(u, v);
}
tout[v] = tour.size() - 1;
}
vector<pair<int, int>> seg[2 * N];
int get_node_count(const vector<pair<int, int>>& node, const int& element) {
auto it = lower_bound(node.begin(), node.end(), make_pair(element, -1));
if (it == node.end() || it->first != element) return 0;
return it->second;
}
void combine(vector<pair<int, int>>& dest, const vector<pair<int, int>>& v1, const vector<pair<int, int>>& v2) {
int p1 = 0, p2 = 0;
while (p1 < v1.size() && p2 < v2.size()) {
if (v1[p1] < v2[p2]) dest.push_back(v1[p1++]);
else if (v2[p2] < v1[p1]) dest.push_back({v2[p2++]});
else if (v1[p1] == v2[p2]) dest.emplace_back(v1[p1].first, v1[p1].second + v2[p2].second), ++p1, ++p2;
}
while (p1 < v1.size()) dest.push_back(v1[p1++]);
while (p2 < v2.size()) dest.push_back(v2[p2++]);
}
void build() {
for (int i = 0; i < n; ++i) {
seg[n + i].push_back({col[tour[i]], 1});
}
for (int i = n - 1; i > 0; --i) {
combine(seg[i], seg[i << 1], seg[i << 1 | 1]);
}
}
const int BLOCK = 200;
int cnt[N / BLOCK + 3][R] = {};
vector<int> comp[R];
int bigindex[N] = {};
signed main(void) {
cin.tie(0)->sync_with_stdio(false);
int r, q;
cin >> n >> r >> q;
cin >> col[1];
--col[1];
comp[col[1]].push_back(1);
for (int i = 2; i <= n; ++i) {
int p;
cin >> p >> col[i];
--col[i];
comp[col[i]].push_back(i);
tree[p].push_back(i);
tree[i].push_back(p);
}
build_tour(1);
build();
int index = 0;
for (int i = 0; i < r; ++i) {
if (comp[i].size() >= BLOCK) {
int cnt_now = 0;
bigindex[i] = index;
function<void(int, int)> dfs = [&] (int v, int p) -> void {
if (col[v] == i) ++cnt_now;
else cnt[index][col[v]] += cnt_now;
for (int u : tree[v]) if (u != p) {
dfs(u, v);
}
if (col[v] == i) --cnt_now;
};
dfs(1, 1);
++index;
}
}
while (q--) {
int a, b;
cin >> a >> b;
--a, --b;
if (comp[a].size() >= BLOCK) {
cout << cnt[bigindex[a]][b] << endl;
continue;
}
int ans = 0;
for (int v : comp[a]) {
int tl = tin[v] + n, tr = tout[v] + n + 1;
for (; tl < tr; tl >>= 1, tr >>= 1) {
if (tl&1) ans += get_node_count(seg[tl], b), ++tl;
if (tr&1) --tr, ans += get_node_count(seg[tr], b);
}
}
cout << ans << endl;
}
}
Compilation message
regions.cpp: In function 'void combine(std::vector<std::pair<int, int> >&, const std::vector<std::pair<int, int> >&, const std::vector<std::pair<int, int> >&)':
regions.cpp:35:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (p1 < v1.size() && p2 < v2.size()) {
| ~~~^~~~~~~~~~~
regions.cpp:35:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (p1 < v1.size() && p2 < v2.size()) {
| ~~~^~~~~~~~~~~
regions.cpp:40:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | while (p1 < v1.size()) dest.push_back(v1[p1++]);
| ~~~^~~~~~~~~~~
regions.cpp:41:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while (p2 < v2.size()) dest.push_back(v2[p2++]);
| ~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18776 KB |
Output is correct |
2 |
Incorrect |
4 ms |
18776 KB |
Output isn't correct |
3 |
Incorrect |
5 ms |
18776 KB |
Output isn't correct |
4 |
Incorrect |
7 ms |
18776 KB |
Output isn't correct |
5 |
Incorrect |
10 ms |
18776 KB |
Output isn't correct |
6 |
Incorrect |
16 ms |
19032 KB |
Output isn't correct |
7 |
Incorrect |
24 ms |
19188 KB |
Output isn't correct |
8 |
Incorrect |
26 ms |
19288 KB |
Output isn't correct |
9 |
Incorrect |
65 ms |
20056 KB |
Output isn't correct |
10 |
Incorrect |
68 ms |
20760 KB |
Output isn't correct |
11 |
Incorrect |
154 ms |
21632 KB |
Output isn't correct |
12 |
Incorrect |
289 ms |
22812 KB |
Output isn't correct |
13 |
Incorrect |
150 ms |
23332 KB |
Output isn't correct |
14 |
Incorrect |
336 ms |
24312 KB |
Output isn't correct |
15 |
Incorrect |
1265 ms |
29908 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2601 ms |
43484 KB |
Output isn't correct |
2 |
Incorrect |
802 ms |
49368 KB |
Output isn't correct |
3 |
Correct |
1142 ms |
62440 KB |
Output is correct |
4 |
Incorrect |
418 ms |
24880 KB |
Output isn't correct |
5 |
Incorrect |
839 ms |
27320 KB |
Output isn't correct |
6 |
Incorrect |
477 ms |
35812 KB |
Output isn't correct |
7 |
Incorrect |
1529 ms |
39772 KB |
Output isn't correct |
8 |
Incorrect |
2801 ms |
55156 KB |
Output isn't correct |
9 |
Incorrect |
5748 ms |
61808 KB |
Output isn't correct |
10 |
Incorrect |
7795 ms |
98504 KB |
Output isn't correct |
11 |
Incorrect |
5014 ms |
119360 KB |
Output isn't correct |
12 |
Incorrect |
4887 ms |
87608 KB |
Output isn't correct |
13 |
Incorrect |
5395 ms |
89296 KB |
Output isn't correct |
14 |
Incorrect |
5407 ms |
63668 KB |
Output isn't correct |
15 |
Execution timed out |
8070 ms |
65908 KB |
Time limit exceeded |
16 |
Incorrect |
4074 ms |
126840 KB |
Output isn't correct |
17 |
Execution timed out |
8083 ms |
78672 KB |
Time limit exceeded |