QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#268444#7749. A Simple MST Problemqkm66666TL 986ms209628kbC++174.2kb2023-11-28 17:34:092023-11-28 17:34:10

Judging History

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

  • [2023-11-28 17:34:10]
  • 评测
  • 测评结果:TL
  • 用时:986ms
  • 内存:209628kb
  • [2023-11-28 17:34:09]
  • 提交

answer

#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 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++)
    {
        ll s=1;
        for(auto j:d[i])s*=j;
        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--)
    {
        ll s=1;
        for(auto j:d[i])s*=j;
        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();
        if(l==1)
        {
            int ans=0;
            for(int i=2;i<=r;i++)
            ans+=d[i].size();
            printf("%d\n",ans);
            continue;
        }
        int ok=0;
        for(int i=l;i<=r;i++)
        if(isp[i])ok=i;
        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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 820ms
memory: 189476kb

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: 774ms
memory: 191820kb

input:

2
27 30
183704 252609

output:

8
223092

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 815ms
memory: 191752kb

input:

1
183704 252609

output:

223092

result:

ok single line: '223092'

Test #4:

score: 0
Accepted
time: 896ms
memory: 200196kb

input:

2
639898 942309
30927 34660

output:

983228
11512

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 911ms
memory: 202848kb

input:

3
21731 33468
46192 370315
1712 3753

output:

34608
948775
5299

result:

ok 3 lines

Test #6:

score: 0
Accepted
time: 831ms
memory: 195152kb

input:

4
15237 67700
10918 86104
98287 116980
17432 23592

output:

148811
210927
60893
18687

result:

ok 4 lines

Test #7:

score: 0
Accepted
time: 961ms
memory: 205976kb

input:

5
5594 70302
19202 69588
5634 7877
59821 469300
97439 261121

output:

179735
145706
6565
1203684
488669

result:

ok 5 lines

Test #8:

score: 0
Accepted
time: 945ms
memory: 202324kb

input:

6
43477 229639
188276 269887
72424 150178
9009 36918
137421 180271
14666 124530

output:

541705
255651
232054
77284
135277
313203

result:

ok 6 lines

Test #9:

score: 0
Accepted
time: 890ms
memory: 197208kb

input:

7
461436 501798
98856 148334
20953 119408
910 5027
762 14117
10959 46088
96149 130311

output:

139017
151124
281536
10504
34818
98004
108375

result:

ok 7 lines

Test #10:

score: 0
Accepted
time: 986ms
memory: 209628kb

input:

8
6448 11162
33691 94044
137536 140277
106719 267437
13494 38185
3185 4173
4835 299526
25162 43201

output:

13177
175485
9711
480992
69059
2808
840950
53422

result:

ok 8 lines

Test #11:

score: 0
Accepted
time: 937ms
memory: 196848kb

input:

9
4136 54985
38425 155322
107602 157683
115533 137627
13677 44903
43432 69310
70004 129314
43033 55373
76424 147123

output:

139668
339337
153266
68520
87592
76612
183238
39109
211339

result:

ok 9 lines

Test #12:

score: 0
Accepted
time: 936ms
memory: 202872kb

input:

10
21662 27103
55634 196143
20466 44902
87028 202799
127745 151602
1598 1679
95299 126981
13367 134183
52307 66285
10136 38071

output:

17117
410126
71979
344673
74754
263
100586
342577
41870
77522

result:

ok 10 lines

Test #13:

score: 0
Accepted
time: 942ms
memory: 199784kb

input:

20
14973 29525
12674 35281
27500 64458
67431 109122
12468 19466
4606 9884
38731 44161
3212 89277
23213 37134
4392 40769
5378 7211
22586 29237
56331 81369
43924 59554
31787 34990
19949 21972
47309 65085
5666 48185
99 2714
7969 131566

output:

40882
63073
107649
129480
19975
14563
17562
238237
41056
99346
5358
20747
73854
48244
9911
6517
54866
117382
6374
347901

result:

ok 20 lines

Test #14:

score: 0
Accepted
time: 916ms
memory: 193824kb

input:

30
11101 53557
6079 6241
869 14433
6164 10602
73432 82272
15845 17496
18885 49518
12127 35037
5812 14898
12225 15757
19736 36027
34506 69210
12204 37099
642 1256
11875 12382
169453 211949
20884 26173
8302 26634
75302 79147
13938 16896
11229 13736
4753 7575
2742 17752
4443 5021
2988 5533
1042 1364
19...

output:

118619
538
35473
12392
28768
4915
88426
63728
25217
10666
47893
102086
69488
1584
1669
135374
16581
50701
12720
8517
7762
8170
40235
1798
7014
936
78143
29
32461
19423

result:

ok 30 lines

Test #15:

score: 0
Accepted
time: 980ms
memory: 193172kb

input:

40
1022 1378
14032 55415
12506 15792
3919 16767
12870 32763
10624 12091
1144 29449
5184 9133
4178 8021
7785 13966
3880 26749
15390 34240
2582 11246
431 4695
7020 28894
14092 27156
52666 55295
4068 22068
7392 11533
18273 31481
18854 47481
7786 39812
7341 24968
22021 54790
3221 10332
4930 37633
4407 1...

output:

959
116367
9946
34920
55460
4626
75707
10961
11069
17829
62128
52987
23362
10769
60198
36594
9093
48818
11976
39528
82591
88609
48598
96601
19475
89551
19435
27135
16967
139445
1063
181709
57729
6335
4114
5586
5189
5669
59793
8451

result:

ok 40 lines

Test #16:

score: -100
Time Limit Exceeded

input:

50
15626 23807
5585 6016
6101 8103
9127 21310
36189 45079
14069 28358
4793 6034
4029 8938
35309 95923
43860 65991
3472 11896
13230 36032
11979 20636
2432 24320
1680 8915
2612 5242
9797 10319
2232 9531
24754 30470
41799 71066
9681 10551
434 2733
8898 28848
2033 35179
8338 13387
1547 5093
1840 3757
47...

output:

23466
1386
5889
33690
28343
40048
3754
13493
176378
65680
23137
63701
24080
59083
18974
7221
1739
19409
17905
86553
2807
5709
55079
89488
15241
9203
4985
12067
5112
815
36773
6032
21936
48301
14796
43205
40728
1000
10389
346
16233
134
5168
54990
5999
25607
7930
9444
25040
30739

result: