QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#268442 | #7749. A Simple MST Problem | qkm66666 | TL | 994ms | 207808kb | C++17 | 4.1kb | 2023-11-28 17:29:24 | 2023-11-28 17:29:26 |
Judging History
answer
#pragma GCC optimize(3)
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<cstdio>
#include<cctype>
#include<vector>
#include<bitset>
#include<random>
#include<ctime>
#include<queue>
#include<cmath>
#include<list>
#include<map>
#include<set>
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<long long,long long>
#define FF fflush(stdout)
#define inf 0x3f3f3f3f
#define endl "\n"
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
//char buf[1<<20],*p1,*p2;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
inline int read()
{
int s=0,f=1;
char x=getchar();
while(!isdigit(x))f=(x=='-'?-1:1),x=getchar();
while(isdigit(x))s=s*10+x-'0',x=getchar();
return s*f;
}
// const int p=1e9+7;
//ll ksm(int a,int b){ll ans=1,bs=a;while(b){if(b&1)ans=ans*bs%p;bs=bs*bs%p;b>>=1;}return ans;}
mt19937 rd(time(0));
#define reaD read
vector<int> d[1000005];
int dd[1000005];
vector<int> df[1000005];
int mx[1000005],mi[1000005];
bool isp[1000005];
int f[1000005];
int fd(int x)
{
return x==f[x]?x:f[x]=fd(f[x]);
}
int mg(int x,int y)
{
x=fd(x),y=fd(y);
if(x==y)return 0;
if(d[y].size()>d[x].size())
f[y]=x;
else f[x]=y;
return 1;
}
bool occ[1000005];
int cal(int x,int y)
{
int ans=d[x].size();
for(auto i:d[x])
occ[i]=1;
for(auto i:d[y])
if(!occ[i])ans++;
for(auto i:d[x])
occ[i]=0;
return ans;
}
void bf(int l,int r)
{
vector<pair<int,pii> > e;
for(int i=l;i<=r;i++)
for(int j=i+1;j<=r;j++)
{
e.pb(mp(cal(i,j),mp(i,j)));
}
sort(e.begin(),e.end());
int ans=0;
for(auto i:e)
{
int v=i.fi,x=i.se.fi,y=i.se.se;
if(mg(x,y))ans+=v;
}
printf("%d\n",ans);
return ;
// return ans;
}
//map<vector<int>,int> M;
int id[1000005];
int pri[1000005];
int tot=0;
void dfs(int x,int n,int s)
{
// if(s>mx[x])return ;
if(n==d[x].size())
{
if(s<x)mi[x]=max(mi[x],s);
if(s>x)mx[x]=min(mx[x],s);
return ;
}
ll now=1;
for(int i=0;;i++)
{
if(s*now>1000000||s*now>mx[x])break;
dfs(x,n+1,s*now);
now*=d[x][n];
}
}
int pp[1000005];
int main()
{
for(int i=2;i<=1000000;i++)
if(!d[i].size())
{
isp[i]=1;
pri[++tot]=i;
for(int j=i;j<=1000000;j+=i)
d[j].pb(i);
}
for(int i=1;i<=1000000;i++)
for(int j=i;j<=1000000;j+=i)
df[j].pb(i);
for(int i=1;i<=1000000;i++)
{
int s=1;
for(auto j:d[i])s*=j;
pp[i]=s;
for(auto j:df[s])
mi[i]=max(mi[i],dd[j]);
dd[s]=i;
}
for(int i=1;i<=1000000;i++)
dd[i]=1e9;
for(int i=1000000;i>=1;i--)
{
int s=pp[i];
mx[i]=1e9;
for(auto j:df[s])
mx[i]=min(mx[i],dd[j]);
dd[s]=i;
}
// for(int i=1;i<=1000000;i++)
{
// mx[i]=1000000;
// if(i>1)mx[i]=min(1000000,i*d[i][0]);
// dfs(i,0,1);//cout<<mx[i]<<" ";
}
int T=reaD();
while(T--)
{
int l=reaD(),r=reaD();
int ok=0;
for(int i=l;i<=r;i++)
if(isp[i])
{
ok=i;
break;
}
for(int i=l;i<=r;i++)
f[i]=i;
if(!ok)
{
bf(l,r);
continue;
}
vector<pair<int,pii> > e;
int ans=0;
for(int i=l;i<=r;i++)
{
if(mi[i]>=l)e.pb(mp(d[i].size(),mp(mi[i],i)));
if(mx[i]<=r)e.pb(mp(d[i].size(),mp(i,mx[i])));
}
for(int i=l;i<=r;i++)
if(i!=ok)
{
e.pb(mp(d[i].size()+1,mp(i,ok)));
}
sort(e.begin(),e.end());
for(auto i:e)
{
int v=i.fi,x=i.se.fi,y=i.se.se;
if(mg(x,y))ans+=v;
}
printf("%d\n",ans);
}
// system("pause");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 930ms
memory: 193224kb
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: 947ms
memory: 196796kb
input:
2 27 30 183704 252609
output:
8 223092
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 939ms
memory: 197188kb
input:
1 183704 252609
output:
223092
result:
ok single line: '223092'
Test #4:
score: 0
Accepted
time: 994ms
memory: 205172kb
input:
2 639898 942309 30927 34660
output:
983228 11512
result:
ok 2 lines
Test #5:
score: 0
Accepted
time: 987ms
memory: 207808kb
input:
3 21731 33468 46192 370315 1712 3753
output:
34608 948775 5299
result:
ok 3 lines
Test #6:
score: 0
Accepted
time: 909ms
memory: 198780kb
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:
179735 145706 6565 1203684 488669