提出 #44160682


ソースコード 拡げる

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define Ed for(int i=h[x];~i;i=ne[i])
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
#define Wh while
namespace fstIO{
	const char _fg='\n';
	int _len=0;
	char ibuf[(1<<20)+1],*iS,*iT,_out[(1<<25)+1],_ar[50];
	#define _gh()\
	(iS==iT?iT=(iS=ibuf)+fread(ibuf,1,(1<<20)+1,stdin),\
	(iS==iT?EOF:*iS++):*iS++)
	#define putc(ch) _out[_len++]=ch
	void read(){}
	template<typename Type,typename...Types>
	void read(Type&x,Types&...xs){
		x=0;
		char ch=_gh();
		char t=0;
		while(ch<'0'||ch>'9')t|=ch=='-',ch=_gh();
		while(ch>='0'&&ch<='9')x=x*10+(ch^48),ch=_gh();
		x=t?-x:x;
		read(xs...);
	}
	template<typename Type>
	void write(Type x){
		int tot=0;
		if(!x)putc('0');
		if(x<0)putc('-'),x=-x;
		while(x)_ar[++tot]=x%10+'0',x/=10;
		for(int i=tot;i;--i)putc(_ar[i]);
		putc(_fg);
	}
	void flush(){
		fwrite(_out,1,_len,stdout);
		_len=0;
	}
}
using namespace fstIO;
const int N=200010,M=2*N;
int h[N],e[M],ne[M],idx,l[N],r[N],dfn,n,m;//don't forget memset h!
vector<int>g[N];
void add(int a,int b){
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int x,int fa,int d){
	l[x]=++dfn,g[d].push_back(dfn);
	int tot=1;
	Ed{
		int j=e[i];
		if(j==fa)continue;
		tot+=dfs(j, x, d+1);
	}
	r[x]=dfn;
	return tot;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    #endif
    read(n);
	memset(h,-1,n*4+4);
	Le(i, 2, n){
		int p;
		read(p);
		add(p,i);
	}
	dfs(1, -1, 0);
	read(m);
	W(m){
		int u,de;
		read(u,de);
		write(upper_bound(g[de].begin(),g[de].end(),r[u])
		-lower_bound(g[de].begin(),g[de].end(),l[u]));
	}
	flush();
    return 0;
}

提出情報

提出日時
問題 E - Count Descendants
ユーザ WUSICHENG
言語 C++ (GCC 9.2.1)
得点 500
コード長 1937 Byte
結果 AC
実行時間 66 ms
メモリ 34880 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 1
AC × 30
セット名 テストケース
Sample sample_00
All binary_00, binary_01, binary_02, binary_03, binary_04, binary_05, binary_06, binary_07, binary_08, binary_09, bound_00, bound_01, bound_02, bound_03, broomlike_00, broomlike_01, broomlike_02, broomlike_03, broomlike_04, random_00, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, sample_00
ケース名 結果 実行時間 メモリ
binary_00 AC 45 ms 12072 KiB
binary_01 AC 46 ms 11072 KiB
binary_02 AC 36 ms 11760 KiB
binary_03 AC 32 ms 12164 KiB
binary_04 AC 41 ms 11876 KiB
binary_05 AC 27 ms 11180 KiB
binary_06 AC 24 ms 11644 KiB
binary_07 AC 16 ms 9656 KiB
binary_08 AC 41 ms 11460 KiB
binary_09 AC 59 ms 13364 KiB
bound_00 AC 66 ms 34880 KiB
bound_01 AC 63 ms 34652 KiB
bound_02 AC 14 ms 8656 KiB
bound_03 AC 19 ms 8800 KiB
broomlike_00 AC 52 ms 21744 KiB
broomlike_01 AC 47 ms 23268 KiB
broomlike_02 AC 62 ms 22536 KiB
broomlike_03 AC 55 ms 18892 KiB
broomlike_04 AC 50 ms 23028 KiB
random_00 AC 21 ms 9816 KiB
random_01 AC 39 ms 12792 KiB
random_02 AC 29 ms 9612 KiB
random_03 AC 43 ms 10308 KiB
random_04 AC 52 ms 13404 KiB
random_05 AC 19 ms 8792 KiB
random_06 AC 46 ms 11992 KiB
random_07 AC 46 ms 11508 KiB
random_08 AC 29 ms 12832 KiB
random_09 AC 41 ms 13500 KiB
sample_00 AC 10 ms 7740 KiB