이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
template<typename T>
void pr(T a){std::cerr<<a<<std::endl;}
template<typename T,typename... Args>
void pr(T a, Args... args) {std::cerr<<a<<' ',pr(args...);}
#else
template<typename... Args>
void pr(Args... args){}
#endif
using namespace std;
const int MM = 2e5+5, NN = 25001, SQ = 450;
int n, m, q;
int val[MM];
vector<int> adj[MM];
int in[MM], out[MM], t;
map<int, int> cnt[MM], parcnt;
vector<int> nodes[MM], pos[MM];
int bid[MM];
vector<int> big;
int ans1[SQ][NN];
int ans2[NN][SQ]; //swap order if still not fast enough
void dfs(int cur, int pre){
in[cur] = ++t;
pos[val[cur]].emplace_back(t);
if(size(nodes[val[cur]]) < SQ){
//do big to small only
for(int i = 0; i < size(big); i++)
ans1[i][val[cur]] += parcnt[big[i]];
}
parcnt[val[cur]]++;
int id = 0;
for(auto u: adj[cur]){
if(u != pre){
dfs(u, cur);
if(size(cnt[u]) > size(cnt[id]))
id = u;
}
}
cnt[cur] = move(cnt[id]);
for(auto u: adj[cur]){
if(u != pre and u != id){
for(auto [a, c]: cnt[u]){
cnt[cur][a] += c;
}
cnt[u].clear();
}
}
cnt[cur][val[cur]]++;
//do small to big and big to big
for(int i = 0; i < size(big); i++)
ans2[val[cur]][i] += cnt[cur][big[i]];
if(!--parcnt[val[cur]]){
parcnt.erase(val[cur]);
}
out[cur] = t;
}
int main(){
cin>>n>>m>>q;
cin>>val[1];
nodes[val[1]].emplace_back(1);
for(int i = 2,p; i <= n; i++){
cin>>p>>val[i];
adj[p].emplace_back(i);
nodes[val[i]].emplace_back(i);
}
for(int i = 1; i <= m; i++){
if(size(nodes[i]) >= SQ){
bid[i] = size(big);
big.emplace_back(i);
}
pos[i].reserve(size(nodes[i]));
}
dfs(1, 0);
for(int i = 0,a,b; i < q; i++){
cin>>a>>b;
if(size(nodes[b]) >= SQ){
cout<<ans2[a][bid[b]]<<'\n';
}
else if(size(nodes[a]) >= SQ){
cout<<ans1[bid[a]][b]<<'\n';
}
else{
int ret = 0;
for(int j: nodes[a]){
ret += upper_bound(all(pos[b]), out[j]) - lower_bound(all(pos[b]), in[j]);
}
cout<<ret<<'\n';
}
cout<<flush;
}
}
/*
precompute big-small, small-small, small-big
brute force small-small
*/
컴파일 시 표준 에러 (stderr) 메시지
regions.cpp: In function 'void dfs(int, int)':
regions.cpp:34:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0; i < size(big); i++)
| ~~^~~~~~~~~~~
regions.cpp:59:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 0; i < size(big); i++)
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |