UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#203604#157. 路径排序yizhiming10011154ms24988kbC++115.5kb2024-02-28 10:57:542024-02-28 12:16:25

answer

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
int read(){
	int x=0,f=1;char ch = getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int N = 3e5+5;
struct poi{
	int x[2];int id;
}p[N];
bool cmp2(poi a,poi b){
	return a.x[0]<b.x[0];
}
bool cmp3(poi a,poi b){
	return a.x[1]<b.x[1];
}
struct aa{
	int nxt,to;
}edge[N*2];
int head[N],tote,in[N];
void link(int u,int v){
	if(!u||!v){
		return;
	}
	edge[++tote] = (aa){head[u],v};head[u] = tote;
}
queue<int>q;
struct KDT{
	int tot;
	struct aa{
		int lc,rc,L[2],R[2];//0/1表示横纵坐标 LR 是最小最大 
		poi P;
		int mi,tag,val;
	}node[N*3];
	void pushup(int u){
		for(int i=0;i<=1;i++){
			node[u].L[i] = node[u].P.x[i];node[u].R[i] = node[u].P.x[i];
			if(node[u].lc){
				node[u].L[i] = min(node[u].L[i],node[node[u].lc].L[i]);
				node[u].R[i] = max(node[u].R[i],node[node[u].lc].R[i]);
			}
			if(node[u].rc){
				node[u].L[i] = min(node[u].L[i],node[node[u].rc].L[i]);
				node[u].R[i] = max(node[u].R[i],node[node[u].rc].R[i]);
			}
		}
		node[u].mi = min(node[u].val,min(node[node[u].lc].mi,node[node[u].rc].mi));
	}
	void build(int &u,int l,int r,int k){
		if(l>r){
			return;
		}
		if(!u){
			u = ++tot;
		}
		int mid = (l+r)/2;
		if(k==0){
			nth_element(p+l,p+mid,p+r+1,cmp2);
		}else{
			nth_element(p+l,p+mid,p+r+1,cmp3);
		}
		build(node[u].lc,l,mid-1,k^1);
		build(node[u].rc,mid+1,r,k^1);
		
		node[u].P = p[mid];
		node[u].val = in[p[mid].id];
		pushup(u);
//		cout<<"U:"<<u<<" "<<l<<" "<<r<<" "<<node[u].mi<<" "<<p[mid].id<<" "<<node[u].val<<"\n";
	}
	void lazy_tag(int u,int x){
		if(!u){
			return;
		}
		node[u].mi+=x;
		node[u].val+=x;
		node[u].tag+=x;
	}
	void pushdown(int u){
		if(node[u].tag){
			lazy_tag(node[u].lc,node[u].tag);
			lazy_tag(node[u].rc,node[u].tag);
		}
		node[u].tag = 0;
	}
	void upd(int u,int a0,int b0,int a1,int b1,int x){
		if(!u){
			return;
		}
		pushdown(u);
		if(node[u].L[0]>=a0&&node[u].R[0]<=b0&&node[u].L[1]>=a1&&node[u].R[1]<=b1){
//			sta[++top] = u;
			lazy_tag(u,x);
//			cout<<"2:"<<u<<" "<<node[u].val<<" "<<node[u].mi<<"\n";
			return;
		}
		if(b0<node[u].L[0]||a0>node[u].R[0]||b1<node[u].L[1]||a1>node[u].R[1]){
			return;
		}
		poi v = node[u].P;
		if(v.x[0]>=a0&&v.x[0]<=b0&&v.x[1]>=a1&&v.x[1]<=b1){
//			sta[++top] = node[u].P.id;
//			lazy_tag(u,x);
			node[u].val+=x;
//			cout<<"1:"<<u<<" "<<node[u].val<<"\n";
		}
		upd(node[u].lc,a0,b0,a1,b1,x);
		upd(node[u].rc,a0,b0,a1,b1,x);
		
		pushup(u);
//		cout<<"V:"<<u<<" "<<node[u].mi<<" "<<node[u].val<<"\n";
	}
	void ask(int u){
//		cout<<"U:"<<u<<" "<<node[u].val<<" "<<node[u].mi<<"\n";
		if(!u||node[u].mi>0){
			return;
		}
		
		pushdown(u);
		if(node[u].val==0){
			q.push(node[u].P.id);
			node[u].val = 1e9;
		}
		ask(node[u].lc);
		ask(node[u].rc);
		pushup(u);
	}
	void print(int u){
		if(!u){
			return;
		}
		pushdown(u);
//		cout<<"P:"<<u<<" "<<node[u].val<<" "<<node[u].mi<<"\n";
		print(node[u].lc);print(node[u].rc);
	}
}T;
int rt;
int n,m,k;
vector<int>ed[N];
int dep[N],fa[N],tp[N],dfn[N],siz[N],son[N],ct;
void dfs1(int u,int f){
	siz[u] = 1;
	dfn[u] = ++ct;
	for(auto x:ed[u]){
		if(x==f){
			continue;
		}
		dep[x] = dep[u]+1;
		fa[x] = u;
		dfs1(x,u);
		siz[u]+=siz[x];
		if(siz[x]>siz[son[u]]){
			son[u] = x;
		}
	}
}
void dfs2(int u,int t){
	tp[u] = t;
	if(!son[u]){
		return;
	}
	dfs2(son[u],t);
	for(auto x:ed[u]){
		if(x==fa[u]||x==son[u]){
			continue;
		}
		dfs2(x,x);
	}
}
int Lca(int u,int v){
	while(tp[u]!=tp[v]){
		if(dep[tp[u]]<dep[tp[v]]){
			swap(u,v);
		}
		u = fa[tp[u]];
	}
	if(dep[u]<dep[v]){
		swap(u,v); 
	}
	return v;
}
int U[N],V[N];
int getrt(int u,int t){
	while(tp[u]!=tp[t]){
		if(fa[tp[u]]==t){
			return tp[u];
		}
		u = fa[tp[u]];
	}
	return son[t];
} 
void ask(int l1,int r1,int l2,int r2,int x){
	if(l1>r1||l2>r2){
		return;
	}
	if(l1>l2){
		swap(l1,l2);swap(r1,r2);
	}
//	cout<<"LR:"<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<"\n";
	T.upd(rt,l1,r1,l2,r2,x);
}
void Link(int i,int x){
	int u=U[i],v=V[i];
	int L = Lca(u,v);
	if(L==u){
		int R = getrt(v,u);
//		cout<<"R:"<<R<<" "<<v<<" "<<u<<"\n";
		ask(1,dfn[R]-1,dfn[v],dfn[v]+siz[v]-1,x);
		ask(dfn[R]+siz[R],n,dfn[v],dfn[v]+siz[v]-1,x);
	}else{
		ask(dfn[u],dfn[u]+siz[u]-1,dfn[v],dfn[v]+siz[v]-1,x);
	}
}
int sta[N],top;
signed main(){
	n = read();m = read();k = read();
	T.tot = m;T.node[0].mi = 1e9;
	for(int i=1;i<n;i++){
		int u,v;
		u = read();v = read();
		ed[u].push_back(v);ed[v].push_back(u);
	}
	dfs1(1,0);
	dfs2(1,1);
	
	for(int i=1;i<=m;i++){
		in[i]--;
		U[i] = read();V[i] = read();
		if(dfn[U[i]]>dfn[V[i]]){
			swap(U[i],V[i]);
		}
		p[i].x[0] = dfn[U[i]];p[i].x[1] = dfn[V[i]];p[i].id = i;
	}
	for(int i=1;i<=k;i++){
		int u,v;
		u = read();v = read();
		link(v,u);in[u]++;
	}
	T.build(rt,1,m,0);
	for(int i=1;i<=m;i++){
//		cout<<"i:"<<i<<"\n";
		Link(i,1);
		
//		T.print(rt);
	}
//	cout<<"hrer:"<<T.node[rt].mi<<"\n";
	T.ask(rt);
	while(!q.empty()){
		int u = q.front();
		q.pop();
//		cout<<"U:"<<u<<"\n";
		sta[++top] = u;
//		cout<<u<<" ";
		Link(u,-1);
		for(int i=head[u];i;i=edge[i].nxt){
			int now = edge[i].to;
			T.upd(rt,dfn[U[now]],dfn[U[now]],dfn[V[now]],dfn[V[now]],-1);
		}
		T.ask(rt);
	}
	for(int i=top;i>=1;i--){
		cout<<sta[i]<<" ";
	}
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 34ms
memory: 9152kb

input:

250 300 100000
129 130
143 86
36 114
86 192
114 129
244 11
44 21
120 71
120 28
156 203
44 119
120 23...

output:

59 184 190 275 182 176 173 43 167 46 165 163 51 149 55 56 262 143 137 64 135 68 133 129 73 127 125 1...

result:

ok Correct

Test #2:

score: 10
Accepted
time: 36ms
memory: 9156kb

input:

300 300 100000
100 148
155 187
257 70
49 159
103 227
160 155
103 89
256 77
50 45
294 15
72 99
136 16...

output:

232 185 248 25 136 122 29 51 233 97 129 195 47 46 215 212 42 208 261 3 158 293 167 153 280 65 152 94...

result:

ok Correct

Test #3:

score: 10
Accepted
time: 62ms
memory: 15968kb

input:

100000 300 100000
28200 27666
6110 87883
27985 69354
78366 29719
83603 82953
48479 5767
23170 19451
...

output:

249 187 62 200 165 256 101 69 260 195 270 151 192 118 146 185 246 137 295 218 216 294 292 86 11 180 ...

result:

ok Correct

Test #4:

score: 10
Accepted
time: 61ms
memory: 17288kb

input:

100000 500 99995
63125 64162
49442 43375
40926 86108
90303 50407
11302 52972
95607 27407
84863 33754...

output:

101 402 100 383 386 384 389 90 89 278 83 399 283 417 416 266 68 425 192 427 259 49 440 125 155 145 3...

result:

ok Correct

Test #5:

score: 10
Accepted
time: 80ms
memory: 17280kb

input:

100000 1000 100000
92328 45707
79216 23071
92328 62657
98306 53771
50571 77995
92328 8687
98029 9316...

output:

684 433 673 655 423 632 633 92 645 628 642 627 687 623 658 448 714 402 80 625 411 383 377 738 756 70...

result:

ok Correct

Test #6:

score: 10
Accepted
time: 2038ms
memory: 23004kb

input:

99999 100000 0
50744 39915
17764 54852
26187 49572
38788 51141
26187 43701
17764 62544
26187 54877
1...

output:

15164 88795 49449 52038 47841 85630 88843 80094 60093 71165 80105 1710 22501 74783 84085 88772 85109...

result:

ok Correct

Test #7:

score: 10
Accepted
time: 2103ms
memory: 22588kb

input:

80000 100000 100000
7356 73211
12823 29294
18001 19716
5444 55580
17246 10745
28150 19481
46616 2976...

output:

60071 48944 21919 99135 20392 97330 32002 8212 43977 90352 16690 86605 80199 41333 90668 79063 48492...

result:

ok Correct

Test #8:

score: 10
Accepted
time: 2196ms
memory: 24932kb

input:

100000 100000 100000
6788 96104
43211 44103
23512 56251
44334 62618
45036 62410
7232 1638
79535 6536...

output:

19816 33009 92214 24854 21167 50887 32695 40396 43497 78294 55436 11877 29442 49718 91220 75267 6877...

result:

ok Correct

Test #9:

score: 10
Accepted
time: 2358ms
memory: 24988kb

input:

100000 100000 100000
8800 45985
87438 90509
78707 15831
43187 91426
54182 88739
52013 43902
84467 27...

output:

14492 74195 22589 44000 12875 2351 49970 39337 71016 10186 23921 56387 47396 31358 83896 70957 9262 ...

result:

ok Correct

Test #10:

score: 10
Accepted
time: 2186ms
memory: 24944kb

input:

100000 100000 100000
68995 81957
4508 80704
81211 57724
77918 34365
72042 25746
72042 45309
10233 86...

output:

75454 68398 9545 40339 56686 68426 82639 54887 46091 91186 82950 7113 17198 45776 41166 64636 23159 ...

result:

ok Correct

Extra Test:

score: 0
Extra Test Passed