QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#454751#8547. Whose Land?masterhuangTL 4ms11800kbC++202.6kb2024-06-25 13:11:012024-06-25 13:11:02

Judging History

你现在查看的是最新测评结果

  • [2024-06-25 13:11:02]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:11800kb
  • [2024-06-25 13:11:01]
  • 提交

answer

#include<bits/stdc++.h>
#define P pair<int,int>
#define fi first
#define se second
#define LL long long
#define vec vector<P>
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
namespace IO
{
	const int _Pu=2e7+5,_d=32;
	char buf[_Pu],obuf[_Pu],*p1=buf+_Pu,*p2=buf+_Pu,*p3=obuf,*p4=obuf+_Pu-_d;
	inline void fin()
	{
		memmove(buf,p1,p2-p1);
		int rlen=fread(buf+(p2-p1),1,p1-buf,stdin);
		if(p1-rlen>buf) buf[p2-p1+rlen]=EOF;p1=buf;
	}
	inline void fout(){fwrite(obuf,p3-obuf,1,stdout),p3=obuf;}
	inline int rd()
	{
		if(p1+_d>p2) fin();int isne=0,x=0;
		for(;!isdigit(*p1);++p1) isne=(*p1=='-');x=(*p1++-'0');
	    for(;isdigit(*p1);++p1) x=x*10+(*p1-'0');
		if(isne) x=-x;return x;
	}
	inline void wr(int x,char end='\n')
	{
		if(!x) return *p3++='0',*p3++=end,void();
		if(x<0) *p3++='-',x=-x;
		char sta[20],*top=sta;
		do{*top++=(x%10)+'0';x/=10;}while(x);
		do{*p3++=*--top;}while(top!=sta);(*p3++)=end;
	}
}using IO::rd;using IO::wr;
const int N=1e5+5;
int T,n,k,q,tot,f[N],d[N],c[N][25];
basic_string<int>g[N];
vec G[19][N];bool v[N][25];
void dfs(int x,int fa){f[x]=fa;for(int i:g[x]) if(i^fa) dfs(i,x);}
inline void upd(int i,int x,int y){G[0][i].push_back({x,y});}
inline vec hb(vec &A,vec &B)
{
	vec z;
	for(auto [u,d]:B) v[u][d]=1;
	for(auto [u,d]:A)
	{
		bool o=1;
		for(int j=0,x=u;j<=k-d&&x;j++,x=f[x])
			if(j&&v[x][d+j]){o=0;break;}
		if(o) z.push_back({u,d});
	}
	for(auto [u,d]:B) v[u][d]=0;
	swap(A,B);
	for(auto [u,d]:B) v[u][d]=1;
	for(auto [u,d]:A)
	{
		bool o=1;
		for(int j=0,x=u;j<=k-d&&x;j++,x=f[x])
			if(j&&v[x][d+j]){o=0;break;}
		if(o) z.push_back({u,d});
	}
	for(auto [u,d]:B) v[u][d]=0;swap(A,B);return z;
}
int main()
{
	T=rd();
	while(T--)
	{
		n=rd(),k=rd(),q=rd();
		for(int i=1,u,v;i<n;i++) u=rd(),v=rd(),g[u]+=v,g[v]+=u;dfs(1,0);
		for(int i=1;i<=n;i++) for(int j=0,x=i;j<=k&&x;j++,x=f[x]) c[x][j]++;
		for(int i=1;i<=n;i++) for(int x=i,y=k;x&&~y;x=f[x],y--)
		{
			if(f[x]&&y>1) upd(i,x,y),upd(i,x,y-1);
			else for(int j=0;j<=y;j++) upd(i,x,j);
		}
		for(int i=1;i<=18;i++) for(int j=1;j+(1<<i)-1<=n;j++)
			G[i][j]=hb(G[i-1][j],G[i-1][j+(1<<(i-1))]);
		while(q--)
		{
			int l=rd(),r=rd(),t=__lg(r-l+1);
			vec z=hb(G[t][l],G[t][r-(1<<t)+1]);
			sort(z.begin(),z.end());
			z.erase(unique(z.begin(),z.end()),z.end());
			int ans=0;
			for(auto [u,d]:z) ans+=c[u][d];wr(ans);
		}
		for(int i=0;i<=18;i++) for(int j=1;j<=n;j++) G[i][j].clear();
		for(int i=0;i<=k;i++) for(int j=1;j<=n;j++) c[j][i]=0;
		for(int i=1;i<=n;i++) g[i].clear();
	}
	return IO::fout(),0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 11800kb

input:

2
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3
8 2 3
1 2
1 3
2 4
2 5
4 6
5 7
7 8
2 2
2 5
3 4

output:

4
5
7
8
6

result:

ok 5 number(s): "4 5 7 8 6"

Test #2:

score: -100
Time Limit Exceeded

input:

1000
500 1 500
291 2
406 9
207 13
71 15
351 17
442 18
496 19
104 20
208 23
448 34
80 42
187 44
352 45
290 46
116 47
318 50
226 52
129 55
83 59
100 60
54 61
73 65
63 66
454 67
43 71
26 74
68 26
320 75
388 76
425 79
170 81
350 83
48 85
153 86
221 90
290 95
130 96
82 98
124 82
36 99
213 100
121 101
132...

output:


result: