#include <bits/stdc++.h>
#define MAXN 205000
#define REGIOES 25100
#define INI MAXN-5
#define SQRTN 450
int cores[MAXN],array[MAXN],primeiro[MAXN],size[MAXN],cur;
std::vector<int> con[MAXN];
std::vector<int> indices[REGIOES];
int N,R,Q;
int dfs(int pos,int prev){
array[cur]=cores[pos];
primeiro[pos]=cur;
++cur;
int res=1;
for(auto&x:con[pos]){
if(x==prev)continue;
res+=dfs(x,pos);
}
return size[pos]=res;
}
int tamanho[REGIOES];
bool grandes[REGIOES];
int indicegrandeza[REGIOES];
const int threshold = 300;///Mudar para SQRTN!!!!!!
int curg=0;
namespace big {
int bucket[670][REGIOES];
int tab[MAXN*4];
void query(int l,int r,int la=0,int ra=INI,int pos=1){
if(ra<l||la>r)return;
if(la>=l&&ra<=r){
++tab[pos];
return;
}
int m=(la+ra)/2;
query(l,r,la,m,pos*2);
query(l,r,m+1,ra,(pos*2)+1);
}
void build(int k,int la=0,int ra=INI,int pos=1){
if(tab[pos]){
for(int i=la;i!=ra+1;++i){
bucket[k][array[i]]+=tab[pos];
}
tab[pos]=0;
}
if(la==ra)return;
int m=(la+ra)/2;
build(k,la,m,pos*2);
build(k,m+1,ra,(pos*2)+1);
}
void preparar(void){
for(int i=0;i!=REGIOES;++i){
if(grandes[i]){
for(auto&x:indices[i]){
query(primeiro[x],primeiro[x]+size[x]-1);
}
build(indicegrandeza[i]);
}
}
}
int consulta(int x,int y){
return bucket[indicegrandeza[x]][y];
}
}
typedef std::pair<int,int> pii;
namespace small {
std::vector<pii> tab[MAXN*4];
int query(int l,int r,int k,int la=0,int ra=INI,int pos=1){
if(ra<l||la>r)return 0;
if(la>=l&&ra<=r){
int l=0,r=tab[pos].size()-1;
while(l<r){
int m = (l+r)/2;
if(tab[pos][m].first>=k){
r=m;
}else l=m+1;
}
if(tab[pos][l].first==k)return tab[pos][l].second;return 0;
}
int m=(la+ra)/2;
return query(l,r,k,la,m,pos*2) +
query(l,r,k,m+1,ra,(pos*2)+1);
}
void build(int la=0,int ra=INI,int pos=1){
for(int i=la;i!=ra+1;++i){
tab[pos].push_back({array[i],1});
}
std::sort(tab[pos].begin(),tab[pos].end());
{
std::vector<pii> novo_vetor;
for(auto&x:tab[pos]){
if(!novo_vetor.size()||novo_vetor.back().first!=x.first){
novo_vetor.push_back(x);
}else ++novo_vetor.back().second;
}
tab[pos]=novo_vetor;
}
if(la==ra)return;
int m=(la+ra)/2;
build(la,m,pos*2);
build(m+1,ra,(pos*2)+1);
}
int consulta(int a,int b){
int res=0;
for(auto&x:indices[a]){
res+=query(primeiro[x],primeiro[x]+size[x]-1,b);
}
return res;
}
};
int main()
{
std::ios::sync_with_stdio(false);
std::cin>>N>>R>>Q;
std::cin>>cores[0];
indices[cores[0]].push_back(0);
for(int i=1;i!=N;++i){
int a,b;std::cin>>a>>b;--a;
cores[i]=b;
indices[b].push_back(i);
tamanho[b]++;
con[a].push_back(i);con[i].push_back(a);
}
dfs(0,0);
for(int i=0;i!=REGIOES;++i){
if(tamanho[i]>threshold){
indicegrandeza[i]=curg;
++curg;
grandes[i]=true;
}
}
big::preparar();
small::build();
for(int i=0;i!=Q;++i){
int a,b;
std::cin>>a>>b;
if(grandes[a]){
std::cout<<big::consulta(a,b)<<std::endl;
}else {
std::cout<<small::consulta(a,b)<<std::endl;
}
}
}
Compilation message
regions.cpp: In function 'int small::query(int, int, int, int, int, int)':
regions.cpp:78:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
78 | if(tab[pos][l].first==k)return tab[pos][l].second;return 0;
| ^~
regions.cpp:78:63: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
78 | if(tab[pos][l].first==k)return tab[pos][l].second;return 0;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
68996 KB |
Output is correct |
2 |
Correct |
230 ms |
68916 KB |
Output is correct |
3 |
Correct |
214 ms |
68900 KB |
Output is correct |
4 |
Correct |
215 ms |
68912 KB |
Output is correct |
5 |
Correct |
229 ms |
68832 KB |
Output is correct |
6 |
Correct |
236 ms |
68964 KB |
Output is correct |
7 |
Correct |
296 ms |
68896 KB |
Output is correct |
8 |
Correct |
244 ms |
68940 KB |
Output is correct |
9 |
Correct |
322 ms |
69392 KB |
Output is correct |
10 |
Correct |
386 ms |
69452 KB |
Output is correct |
11 |
Correct |
558 ms |
69772 KB |
Output is correct |
12 |
Correct |
715 ms |
70236 KB |
Output is correct |
13 |
Correct |
777 ms |
70220 KB |
Output is correct |
14 |
Correct |
1281 ms |
70504 KB |
Output is correct |
15 |
Correct |
2904 ms |
73540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
8009 ms |
74540 KB |
Time limit exceeded |
2 |
Execution timed out |
8026 ms |
74200 KB |
Time limit exceeded |
3 |
Execution timed out |
8036 ms |
76740 KB |
Time limit exceeded |
4 |
Correct |
1356 ms |
70760 KB |
Output is correct |
5 |
Correct |
1984 ms |
72476 KB |
Output is correct |
6 |
Correct |
1004 ms |
74564 KB |
Output is correct |
7 |
Correct |
4815 ms |
75884 KB |
Output is correct |
8 |
Correct |
5220 ms |
84004 KB |
Output is correct |
9 |
Execution timed out |
8086 ms |
83040 KB |
Time limit exceeded |
10 |
Execution timed out |
8013 ms |
95736 KB |
Time limit exceeded |
11 |
Execution timed out |
8045 ms |
92176 KB |
Time limit exceeded |
12 |
Execution timed out |
8055 ms |
81316 KB |
Time limit exceeded |
13 |
Execution timed out |
8026 ms |
82416 KB |
Time limit exceeded |
14 |
Execution timed out |
8083 ms |
84080 KB |
Time limit exceeded |
15 |
Execution timed out |
8031 ms |
86456 KB |
Time limit exceeded |
16 |
Execution timed out |
8060 ms |
94000 KB |
Time limit exceeded |
17 |
Execution timed out |
8021 ms |
94000 KB |
Time limit exceeded |