QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#456395#8547. Whose Land?kkkgjyismine4WA 147ms20276kbC++235.9kb2024-06-27 20:50:442024-06-27 20:50:45

Judging History

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

  • [2024-06-27 20:50:45]
  • 评测
  • 测评结果:WA
  • 用时:147ms
  • 内存:20276kb
  • [2024-06-27 20:50:44]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r>>1)
#define vi vector<int>
#define mp make_pair
using namespace std;
const ll mod=998244353;
ll val[100050];int N,K,Q,mark[100050];
vector<int>Vis[100050];
vector<pii>d[100050][21],Vec[100050];
vector<int>road[100050];
int sz[100500],hson[100050],dfn[100050],low[100050],tot,Top[100050],dep[100050],fa[100050];
void dfs(int u,int f){
	sz[u]=1,hson[u]=-1;
	for(int v:road[u]){
		if(v==f)continue;
		dep[v]=dep[u]+1,dfs(v,u),fa[v]=u,sz[u]+=sz[v];
		if(hson[u]==-1||sz[v]>sz[hson[u]])hson[u]=v;
	}
}
void dfs1(int u,int g){
	Top[u]=g;if(hson[u]!=-1)dfs1(hson[u],g);
	for(int v:road[u])if(dep[v]>dep[u]&&v!=hson[u])dfs1(v,v);
}
int tt2,bl[100050];
void getid(int u){
	if(!mark[u])dfn[u]=++tot;
	else{
		bl[u]=++tt2;
		if(dep[u]<=K+1)Vis[1].pb(u);
		else{
			int p=u,ct=K;while(ct--)p=fa[p];
			Vis[p].pb(u);
		}
	}
	if(hson[u]!=-1)getid(hson[u]);
	for(int v:road[u])if(dep[v]>dep[u]&&v!=hson[u])getid(v);
}
bool cmp(int a,int b){
	if(dep[a]==dep[b])return bl[a]<bl[b];
	return dep[a]<dep[b];
}
pii c[100500];int tt;
void merge(vector<pii> &a,vector<pii> &b){
    int n1=0,n2=0;tt=0;
    while(n1<a.size()&&n2<b.size()){
    	if(a[n1].fi<b[n2].fi)c[++tt]=a[n1++];
    	else c[++tt]=b[n2++];
	}
	while(n1<a.size())c[++tt]=a[n1++];
	while(n2<b.size())c[++tt]=b[n2++];a.clear();
	for(int i=1;i<=tt;){
		int R=i;while(R<tt&&c[R+1].fi==c[R].se+1)++R;
		a.pb(make_pair(c[i].fi,c[R].se));i=R+1;
	}
}
int Mx=0,Tot;
void init(int u){
    Vec[u].pb(mp(dfn[u],dfn[u]));d[u][0].pb(mp(dfn[u],dfn[u]));
    for(int v:road[u]){
    	if(dep[v]<dep[u])continue;
		init(v);merge(Vec[u],Vec[v]);
    	for(int i=0;i<K;++i)if(d[v][i].size())merge(d[u][i+1],d[v][i]);
	}
	for(int i=0;i<=K;++i){
		int Ct=0;
		for(auto v:d[u][i])if(v.fi<=Tot)++Ct;
		Mx=max(Mx,Ct);
	}
}
queue<int>q;
struct BIT{
	int ct[100005];
	void init(){
		for(int i=0;i<=N;++i)ct[i]=0;
	}
	void add(int p,int o){
		if(!p){ct[p]+=o;return;}
		for(int i=p;i<=N;i+=(i&-i))ct[i]+=o;
	}
	int qry(int p){
		int ans=0;
		for(int i=p;i;i&=(i-1))ans+=ct[i];
		return ans;
	}
}fwk;
typedef unsigned long long ull;
ull *A[4];
void Init(){
	A[0]=new ull[1<<18];
	A[1]=new ull[1<<12];
	A[2]=new ull[1<<6];
	A[3]=new ull [1];
}
void ins(int x){
	for(int i=0;i<4;i++,x>>=6){
		if(A[i][x>>6]&(1ull<<(x&63)))return;
		A[i][x>>6]|=1ull<<(x&63);
	}
}
void del(int x){
	if(A[0][x>>6]&(1ull<<(x&63))==0)return;
	for(int i=0;i<4;i++,x>>=6){
		A[i][x>>6]^=1ull<<(x&63);
		if(A[i][x>>6]!=0)return;
	}
}
int pre(int x){
	++x;
	int i,pre;
	for(i=0;;i++,x>>=6){
		if(i==4)return -1;
		ull s=A[i][x>>6]&((1ull<<(x&63))-1);
		if(!s)continue;
		pre=63-__builtin_clzll(s);
		pre=x&(~63)|pre;
		break;
	}
	for(;i;i--)pre=pre<<6|63-__builtin_clzll(A[i-1][pre]);
	return pre;
}
int nxt(int x){
	--x;
	int i,nxt;
	for(i=0;;i++,x>>=6){
		if(i==4)return -1;
		if((x&63)==63)continue;
		ull s=A[i][x>>6]>>((x&63)+1);
		if(!s)continue;
		nxt=x+1+__builtin_ctzll(s);
		nxt=x&(~63)|nxt;
		break;
	}
	for(;i;i--)nxt=nxt<<6|__builtin_ctzll(A[i-1][nxt]);
	return nxt;
}
int col[100005],stk[100005],tail,st[100005],tl;
void cv(int l,int r,int c){
	tl=0;
	for(int p=nxt(l);p<=r;p=nxt(p+1))st[++tl]=p;
	int Rg=nxt(r+1),lf,rg;
	if(l>1){
		rg=nxt(l),lf=pre(l-1);
		fwk.add(col[lf],-(min(rg,r+1)-l));
	}
	ins(r+1);
	if(Rg==r+1);
	else if(tl&&Rg>r+1)col[r+1]=col[st[tl]];
	else if(l>1)col[r+1]=col[lf];
	else col[r+1]=0;
	int lst=r;
	while(tl)fwk.add(col[st[tl]],-(lst-st[tl]+1)),del(st[tl]),lst=st[tl]-1,--tl;
	col[l]=c,ins(l),fwk.add(c,r-l+1);
}
void Add(int p){
	int c=p;
	tail=0;stk[0]=p;int nk=K;
	while(p!=1&&nk){p=fa[p];stk[++tail]=p;--nk;}
	for(int i=0;i<=tail;++i){
	    for(auto v:d[stk[i]][K-i])cv(v.fi,v.se,c);
		if(K-i-1>=0)for(auto v:d[stk[i]][K-i-1])cv(v.fi,v.se,c);
		if(i==tail)
		  for(int j=K-i-2;j>=0;--j)
		    for(auto v:d[stk[i]][j])cv(v.fi,v.se,c);
	}
}
int ql[500005],Ans[500050];
#define pb push_back
vector<int>que[100005];
#define getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++
#define putchar(x) (p3 - obuf < 1000000) ? (*p3++ = x) : (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf, *p3++ = x)
static char buf[2999999], *p1 = buf, *p2 = buf, obuf[2999999], *p3 = obuf;
template <typename item>
inline void read (register item &x) {
    x = 0;
	register char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
static char cc[15];
template <typename item>
inline void print (register item x) {
	register int len = 0;
	do cc[len++] = x % 10 + '0', x /= 10; while (x);
	while (len--) putchar(cc[len]);
}
void solve(){
	read(N),read(K),read(Q),fwk.init();
	for(int i=1;i<=N;++i){
		que[i].clear(),road[i].clear(),Vis[i].clear(),fa[i]=0,Vec[i].clear(),col[i]=0,dfn[i]=dep[i]=mark[i]=0,hson[i]=-1;
		for(int j=0;j<=K;++j)d[i][j].clear();
	}
	tot=Tot=0;
    for(int i=1;i<N;++i){int u,v;read(u),read(v);road[u].pb(v),road[v].pb(u);}
    dep[1]=1;dfs(1,-1),dfs1(1,1);
    for(int i=1;i<=N;++i)if(dep[i]-dep[Top[i]]<=K)mark[i]=1;
	getid(1);Tot=tot;
	q.push(1);
	while(!q.empty()){
		int u=q.front();q.pop();
		if(u==1)sort(Vis[u].begin(),Vis[u].end(),cmp);
		for(auto v:Vis[u])dfn[v]=++tot;
		if(hson[u]!=-1)q.push(hson[u]);
		for(int v:road[u])if(dep[v]>dep[u]&&v!=hson[u])q.push(v);
	}
	init(1),ins(1),ins(N+1),fwk.ct[0]=N;
	for(int i=1,r;i<=Q;++i)read(ql[i]),read(r),que[r].pb(i);
	for(int i=1;i<=N;++i){
		Add(i);
		for(auto v:que[i])Ans[v]=N-fwk.qry(ql[v]-1)-fwk.ct[0];
	}
	for(int i=1;i<=N+1;++i)del(i);
	for(int i=1;i<=Q;++i)print(Ans[i]),putchar('\n');
}
int main(){
	Init();
    int T;read(T);
    while(T--)solve();
    fwrite(obuf,p3-obuf,1,stdout);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 20276kb

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
Wrong Answer
time: 147ms
memory: 18812kb

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:

255
386
356
124
315
330
437
8
335
423
398
338
180
242
352
500
145
44
342
261
92
326
38
291
259
71
137
456
171
24
162
453
283
325
250
319
478
460
77
354
56
393
372
217
395
265
188
256
134
68
205
429
436
346
300
462
324
170
291
406
207
480
198
182
489
61
476
127
289
204
282
374
114
406
488
366
121
190...

result:

wrong answer 30507th numbers differ - expected: '500', found: '668'