QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#264963#7749. A Simple MST Problemucup-team022#TL 735ms199636kbC++141.9kb2023-11-25 16:12:502023-11-25 16:12:50

Judging History

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

  • [2023-11-25 16:12:50]
  • 评测
  • 测评结果:TL
  • 用时:735ms
  • 内存:199636kb
  • [2023-11-25 16:12:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
vector<pair<int,int> >vec[30];
int T,l,r,pri[N],tot,mn[N],w[N],fa[N],ans,trans[N],rec[N];
bool vis[N];
inline int find(int x){while(x!=fa[x])x=fa[x]=fa[fa[x]];return x;}
vector<int>fac[N];
void sieve(int n){
	for(int i=2;i<=n;++i){
		if(!vis[i])pri[++tot]=i,mn[i]=i;
		for(int j=1;j<=tot&&i*pri[j]<=n;++j){
			vis[i*pri[j]]=1;
			mn[i*pri[j]]=min(mn[i],pri[j]);
			if(i%pri[j]==0)break;
		}
	}
	w[1]=0;
	for(int i=2;i<=n;++i)
		if(i/mn[i]%mn[i]==0)w[i]=w[i/mn[i]];
		else w[i]=w[i/mn[i]]+1;
	trans[1]=1;
	for(int i=2;i<=n;++i){
		int res=1,x=i;
		while(x>1){
			int t=mn[x];
			res*=t;
			while(x%t==0)x/=t;
		}
		trans[i]=res;
	}
	for(int i=2;i<=n;++i)if(trans[i]==i)
		for(int j=i+i;j<=n;j+=i)if(trans[j]==j)
			fac[j].emplace_back(i);
}
inline int getw(int x,int y){
	int res=0;
	x=trans[x],y=trans[y];
	while(x>1&&y>1){
		++res;
		if(mn[x]==mn[y])x/=mn[x],y/=mn[y];
		else if(mn[x]<mn[y])x/=mn[x];else y/=mn[y];
	}
	return res+w[x]+w[y];
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	sieve(1000000);
	for(cin>>T;T--;){
		cin>>l>>r;
		ans=0;
		for(int i=0;i<30;++i)vec[i].clear();
		for(int i=l;i<=r;++i){
			fa[i]=i;
			if(!rec[trans[i]])rec[trans[i]]=i;
		}
		int t=l;
		for(int i=l+1;i<=r;++i)if(w[i]<w[t])t=i;
		for(int i=l;i<=r;++i){
			for(int j=i+1;j<=r&&j-i<=30;++j)vec[getw(i,j)].emplace_back(i,j);
//			continue;
			if(i!=t)vec[getw(i,t)].emplace_back(i,t);
			if(rec[trans[i]]&&rec[trans[i]]!=i)vec[w[i]].emplace_back(i,rec[trans[i]]);
			for(int x:fac[trans[i]])
				if(rec[x])vec[w[i]].emplace_back(i,rec[x]);
		}
		int e=0;
		for(int v=0;v<30;++v)
			for(auto it:vec[v]){
				int x=find(it.first),y=find(it.second);
				if(x!=y)++e,fa[y]=x,ans+=v;//,cerr<<it.first<<' '<<it.second<<endl;
			}
		assert(e==r-l);
		cout<<ans<<'\n';
		for(int i=l;i<=r;++i)rec[trans[i]]=0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 280ms
memory: 73512kb

input:

5
1 1
4 5
1 4
1 9
19 810

output:

0
2
3
9
1812

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 378ms
memory: 102720kb

input:

2
27 30
183704 252609

output:

8
223092

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 364ms
memory: 103628kb

input:

1
183704 252609

output:

223092

result:

ok single line: '223092'

Test #4:

score: 0
Accepted
time: 718ms
memory: 199636kb

input:

2
639898 942309
30927 34660

output:

983228
11512

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 735ms
memory: 192868kb

input:

3
21731 33468
46192 370315
1712 3753

output:

34608
948775
5299

result:

ok 3 lines

Test #6:

score: 0
Accepted
time: 465ms
memory: 100676kb

input:

4
15237 67700
10918 86104
98287 116980
17432 23592

output:

148811
210927
60893
18687

result:

ok 4 lines

Test #7:

score: -100
Time Limit Exceeded

input:

5
5594 70302
19202 69588
5634 7877
59821 469300
97439 261121

output:


result: