# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair <int, int>
#define int long long
using namespace std;
const int N = 2e5 + 5, B = 400;
int tin,in[N],out[N],r1,r2,fer,n,le,ri,up,hea[N],lig[N],col[N],r,q,p[N],idx[N];
multiset <pii> :: iterator it;
int mpp[N];
map <int,int>ans[N];
vector <int> heavy,light;
vector <int> v[N],v1[N];
multiset <pii> ms[N];
vector <pii> vv;
multiset <int> mss;
void dfs(int a, int p) {
tin++;
in[a] = tin;
vv.pb({in[a],a});
for (int i = 0; i < v[a].size(); i++) {
int to = v[a][i];
if (to == p) continue;
dfs(to,a);
}
out[a] = tin;
}
void dfs1(int a, int p) {
if (hea[col[a]]) {
mpp[col[a]]++;
}
for (int j = 0; j < heavy.size(); j++) {
fer = heavy[j];
//if (fer == 1 && col[a] == 3)
//cout<<fer<<" "<<a<<" "<<col[a]<<endl;
if (mpp[fer]) ans[fer][col[a]]+= mpp[fer];
}
for (int i = 0; i < v[a].size(); i++) {
int to = v[a][i];
if (to == p) continue;
dfs1(to,a);
}
if (hea[col[a]]) mpp[col[a]]--;
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>r>>q;
for (int i = 1; i <= n; i++) {
if (i != 1) cin>>p[i],v[p[i]].pb(i),v[i].pb(p[i]);
cin>>col[i];
v1[col[i]].pb(i);
}
for (int i = 1; i <= r; i++) {
if (v1[i].size() >= B) heavy.pb(i), hea[i] = 1;
else light.pb(i),lig[i] = 1;
}
dfs(1,0);
sort(vv.begin(),vv.end());
for (int i = 0; i < vv.size(); i++) {
idx[col[vv[i].s]]++;
ms[col[vv[i].s]].insert({vv[i].f,idx[col[vv[i].s]]});
}
dfs1(1,0);
while(q--) {
cin>>r1>>r2;
if (lig[r1]) {
int anss = 0;
for (int i = 0; i < v1[r1].size(); i++) {
up = v1[r1][i];
it = ms[r2].lower_bound({in[up],0});
if (it == ms[r2].end()) continue;
le = (*it).s;
it = ms[r2].upper_bound({out[up],1e9});
if (it == ms[r2].end()) ri = ms[r2].size();
else ri = (*it).s - 1;
anss += (ri - le + 1);
}
cout<<anss<<"\n";
continue;
}
cout<<ans[r1][r2]<<"\n";
}
}
Compilation message
regions.cpp: In function 'void dfs(long long int, long long int)':
regions.cpp:22:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
regions.cpp: In function 'void dfs1(long long int, long long int)':
regions.cpp:33:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int j = 0; j < heavy.size(); j++) {
| ~~^~~~~~~~~~~~~~
regions.cpp:39:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
regions.cpp: At global scope:
regions.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
46 | main() {
| ^~~~
regions.cpp: In function 'int main()':
regions.cpp:60:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < vv.size(); i++) {
| ~~^~~~~~~~~~~
regions.cpp:69:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < v1[r1].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
13 ms |
28488 KB |
Time limit exceeded (wall clock) |
2 |
Execution timed out |
12 ms |
28488 KB |
Time limit exceeded (wall clock) |
3 |
Execution timed out |
13 ms |
28488 KB |
Time limit exceeded (wall clock) |
4 |
Execution timed out |
12 ms |
28488 KB |
Time limit exceeded (wall clock) |
5 |
Execution timed out |
14 ms |
28616 KB |
Time limit exceeded (wall clock) |
6 |
Execution timed out |
13 ms |
28744 KB |
Time limit exceeded (wall clock) |
7 |
Execution timed out |
13 ms |
28744 KB |
Time limit exceeded (wall clock) |
8 |
Execution timed out |
14 ms |
28872 KB |
Time limit exceeded (wall clock) |
9 |
Execution timed out |
15 ms |
29768 KB |
Time limit exceeded (wall clock) |
10 |
Execution timed out |
17 ms |
30016 KB |
Time limit exceeded (wall clock) |
11 |
Execution timed out |
19 ms |
30796 KB |
Time limit exceeded (wall clock) |
12 |
Execution timed out |
22 ms |
31936 KB |
Time limit exceeded (wall clock) |
13 |
Execution timed out |
25 ms |
32264 KB |
Time limit exceeded (wall clock) |
14 |
Execution timed out |
32 ms |
33216 KB |
Time limit exceeded (wall clock) |
15 |
Execution timed out |
36 ms |
38324 KB |
Time limit exceeded (wall clock) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
105 ms |
41456 KB |
Time limit exceeded (wall clock) |
2 |
Execution timed out |
96 ms |
41364 KB |
Time limit exceeded (wall clock) |
3 |
Execution timed out |
91 ms |
45496 KB |
Time limit exceeded (wall clock) |
4 |
Execution timed out |
31 ms |
33500 KB |
Time limit exceeded (wall clock) |
5 |
Execution timed out |
31 ms |
36660 KB |
Time limit exceeded (wall clock) |
6 |
Execution timed out |
461 ms |
60156 KB |
Time limit exceeded (wall clock) |
7 |
Execution timed out |
442 ms |
57136 KB |
Time limit exceeded (wall clock) |
8 |
Execution timed out |
1825 ms |
113068 KB |
Time limit exceeded (wall clock) |
9 |
Execution timed out |
124 ms |
53956 KB |
Time limit exceeded (wall clock) |
10 |
Runtime error |
1656 ms |
131076 KB |
Execution killed with signal 9 |
11 |
Execution timed out |
157 ms |
61020 KB |
Time limit exceeded (wall clock) |
12 |
Execution timed out |
204 ms |
59820 KB |
Time limit exceeded (wall clock) |
13 |
Execution timed out |
184 ms |
61480 KB |
Time limit exceeded (wall clock) |
14 |
Execution timed out |
864 ms |
84200 KB |
Time limit exceeded (wall clock) |
15 |
Execution timed out |
204 ms |
69236 KB |
Time limit exceeded (wall clock) |
16 |
Execution timed out |
201 ms |
80204 KB |
Time limit exceeded (wall clock) |
17 |
Execution timed out |
806 ms |
100120 KB |
Time limit exceeded (wall clock) |