QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#109795#3029. The Great Drone Showc20230507AC ✓17618ms945168kbC++144.0kb2023-05-30 17:16:172023-05-30 17:16:20

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-30 17:16:20]
  • 评测
  • 测评结果:AC
  • 用时:17618ms
  • 内存:945168kb
  • [2023-05-30 17:16:17]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1.5e6+10;
const int INF=0x3f3f3f3f;
int t,n,m,K,q,X[maxn],Y[maxn],sid,times[maxn],dp[maxn][20];
int in[maxn],vis[maxn],H[maxn],fa[maxn],dep[maxn];
vector<pair<int,int>>G[maxn],Fa[maxn];
set<pair<int,int> >L[maxn],R[maxn];
set<pair<int,int> >S;
map<pair<int,int>,int>mp;
vector<int>g[maxn];//Kruskal 
struct node{
	int k,x,y;
}sta[maxn];
int tot;
void init(){
	for(int i=1;i<=n;i++){
		L[i].clear(),R[i].clear(),G[i].clear(),Fa[i].clear();
		in[i]=vis[i]=H[i]=0;
	}
	for(int i=1;i<=2*n;i++){
		g[i].clear();fa[i]=i,dep[i]=0;times[i]=0;
	}
	S.clear();mp.clear();tot=0;
}
int P(int x){
	int y=sqrt(x);
	while(y*y>x)y--;
	while((y+1)*(y+1)<=x)y++;
	return y;
}
int solve(int x,int y,int w){
	int val=w*w-(X[x]-X[y])*(X[x]-X[y])-(Y[x]-Y[y])*(Y[x]-Y[y]);
	return P(val);
}
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void work(int v,int x,int y){
	int l=find(x),r=find(y);
	if(l==r)return ;
	sid++,times[sid]=v;
	fa[l]=fa[r]=sid;
	g[sid].push_back(l),g[sid].push_back(r);
}
void dfs(int x,int f){
	dep[x]=dep[f]+1;
	dp[x][0]=f;
//	cout<<x<<" "<<f<<" "<<dep[x]<<" "<<dp[x][0]<<endl;
	for(auto p:g[x]){
		if(p==f)continue;
		dfs(p,x);
	}
}
int LCA(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	for(int i=19;i>=0;i--)if(dep[dp[x][i]]>=dep[y])x=dp[x][i];
	if(x==y)return x;
	for(int i=19;i>=0;i--)if(dp[x][i]!=dp[y][i])x=dp[x][i],y=dp[y][i];
	return dp[x][0];
}
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++)cin>>X[i]>>Y[i];
		init();
		cin>>m;
		for(int i=1;i<=m;i++){
			int x,y,val;cin>>x>>y>>val;
			G[x].push_back({y,val});
			G[y].push_back({x,val});
			in[x]++,in[y]++;
		}
		for(int i=1;i<=n;i++)S.insert({in[i],i});
		//定向
		for(int i=1;i<=n;i++){
			auto it=S.begin();
			int x=it->second;
			S.erase(it),vis[x]=1;
			for(auto p:G[x]){
				int y=p.first,w=p.second;
				if(vis[y])continue;
				Fa[x].push_back(p);
				L[y].insert({-solve(x,y,w),x});
				R[y].insert({solve(x,y,w),x});
				S.erase({in[y],y});
				in[y]--;
				S.insert({in[y],y});
			}
		}
		cin>>K;
		for(int i=1;i<=K;i++){
			int x,val;cin>>x>>val;
			H[x]+=val;
			//将父亲处理
			for(auto p:Fa[x]){
				int v=p.first,w=p.second;
				if(mp[{x,v}])continue;
				L[v].erase({H[x]-val-solve(x,v,w),x});
				R[v].erase({H[x]-val+solve(x,v,w),x});
				int pos=(X[x]-X[v])*(X[x]-X[v])+(Y[x]-Y[v])*(Y[x]-Y[v])+(H[x]-H[v])*(H[x]-H[v]);
				if(pos>w*w){
					sta[++tot]={i,x,v};
					mp[{x,v}]=mp[{v,x}]=1;
					continue;
				}
				L[v].insert({H[x]-solve(x,v,w),x});
				R[v].insert({H[x]+solve(x,v,w),x});
			}
			
			while(L[x].size()&&((--L[x].end())->first)>H[x]){//超出下界 
				auto it=(--L[x].end());
				if(mp[{x,it->second}]){
					L[x].erase(it);
					continue;
				}
				sta[++tot]={i,x,it->second};
				mp[{x,it->second}]=mp[{it->second,x}]=1;
				L[x].erase(it);
			}
			
			while(R[x].size()&&((R[x].begin())->first)<H[x]){//小于上界 
				auto it=R[x].begin();
				if(mp[{x,it->second}]){
					R[x].erase(it);
					continue;
				}
				sta[++tot]={i,x,it->second};
				mp[{x,it->second}]=mp[{it->second,x}]=1;
				R[x].erase(it);
			}
		}
		
		sid=n;
//		cout<<tot<<endl;
		for(int i=1;i<=n;i++){
			for(auto p:G[i]){
				if(mp[{i,p.first}])continue;
				//未被删除
				work(K+1,i,p.first); 
			}
		}
		for(int i=tot;i>=1;i--){//本质是求最小值最大 
			work(sta[i].k,sta[i].x,sta[i].y);
		}
		for(int i=1;i<=n;i++){
			int x=find(i);
			if(!dep[x])dfs(x,0);//所有连通块 
		}
		for(int i=1;i<=19;i++){
			for(int j=1;j<=sid;j++){
				dp[j][i]=dp[dp[j][i-1]][i-1];
			}
		}
		cin>>q;
		for(int i=1;i<=q;i++){
			int x,y;cin>>x>>y;
			int lca=LCA(x,y);
			if(times[lca]>K)cout<<-1<<"\n";
			else cout<<times[lca]<<"\n";
		}
	}
	return 0;
}
//1
//4
//0 0
//0 12
//0 24
//0 25
//3
//1 2 13
//2 3 13
//3 4 1
//4
//3 1
//2 6
//3 1
//2 -6
//4
//1 2
//2 3
//3 4
//1 4

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 495ms
memory: 272056kb

input:

389
686
10357458 -45685083
-13525616 -46849548
13245371 -52959727
1533241 -47731445
29767995 -57009848
8709026 -49429890
-10596975 -37749761
-2292572 -57107130
-9505231 -38312854
-61987063 -5380598
31126084 -56985867
2632716 -48068309
17172532 -54294658
-9557851 -38286352
12761654 -53122994
16653770...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 139675 lines

Test #2:

score: 0
Accepted
time: 17618ms
memory: 792012kb

input:

2
500000
14564495 88542502
14369974 88737023
14295451 88811546
14576134 88530863
14275155 88831842
14376399 88730598
14495717 88611280
14627448 88479549
14409262 88697735
14197920 88909077
14514752 88592245
14230630 88876367
14593804 88513193
14254688 88852309
14446490 88660507
14601040 88505957
143...

output:

499999
499999
499999
362592
499999
499999
499999
499999
499999
499999
500000
499999
499999
499999
500000
499999
499999
500000
500000
500000
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
499999
500000...

result:

ok 1000000 lines

Test #3:

score: 0
Accepted
time: 15144ms
memory: 945168kb

input:

2
500000
-19378149 -1495147
-63216117 1581244
-19385210 -1493025
-19413444 -1485172
-19378687 -1494837
-19385262 -1493222
-19416893 -1484193
-19382610 -1493888
-19394475 -1490854
-19389700 -1492007
-19368978 -1497516
-19411475 -1485807
-19409427 -1486310
-19410593 -1486127
-19398571 -1489556
-194185...

output:

-1
-1
-1
-1
-1
-1
287736
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
438438
-1
-1
-1
426237
434139
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
343746
-1
-1
-1
228939
-1
-1
263585
-1
-1
-1
-1
-1
-1
491977
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
108087
-1
-1
-1
...

result:

ok 700000 lines

Test #4:

score: 0
Accepted
time: 15901ms
memory: 831620kb

input:

2
500000
-14328778 23720400
-14328778 23760511
-14328778 23608513
-14328778 23472063
-14328778 23478531
-14328778 23831736
-14328778 23498966
-14328778 23429182
-14328778 23466244
-14328778 23663462
-14328778 23798751
-14328778 23731882
-14328778 23476840
-14328778 23514441
-14328778 23747035
-14328...

output:

451240
397300
-1
331036
363448
166109
-1
352060
-1
424519
-1
-1
-1
250539
-1
152767
-1
474347
300896
306646
185236
417574
493445
-1
442324
414082
379274
414954
-1
178698
120502
-1
120599
396406
-1
325710
350531
304703
485967
498734
165205
406031
-1
397300
335709
-1
323677
386421
399220
393304
23021
...

result:

ok 1000000 lines