QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#845356 | #9904. 最小生成树 | nullptr_qwq | 100 ✓ | 2426ms | 59636kb | C++14 | 2.9kb | 2025-01-06 16:11:39 | 2025-01-06 16:11:40 |
Judging History
answer
/*
考虑直接使用 kruskal 算法,对 $a$ 进行排序,这要求我们对 $k=u+(k-u)$ 的所有边 $(u,v)$ 尽量连边。那么我们需要支持插入该边权所有边以及查询这类边会有多少条在边在 MST 中。然后可以考虑经典套路,对于每一次拓展二分找到对于当前 $k$ 最小的一对不在同一个连通块中的边。那么就需要判定 $V=[l,r]\cup[k-r,k-l]$ 是否满足每个对应的边都不是同一个连通块。我们考虑对于同一种连通块的颜色,给这个点赋上这个颜色对应的权值,这个权值是你给出的两两不同的任意实数,可以用 $[0,2^{64}-1]$ 中随机正整数代替,那么我们要求找出首个不是同个连通块的,也就是说两者权值不同。我们要求极长保留的,要求 $i,k-i$ 的颜色相同,这相当于判定是否构成回文串。线段树维护一下正反串的哈希值,然后对于每次加边即合并连通块,使用启发式合并修改小集合中每个点的颜色为大集合的颜色。时间复杂度 $\mathcal O(n\log^2n)$。
懒得写了。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=4e5+3;
ll n,ans,a[N],id[N],f[N],leaf[N];
vector<int>ve[N];
ull bas=998244853,pw[N],col[N];
mt19937 rd(654321);
int F(int x){return f[x]==x?x:f[x]=F(f[x]);}
struct Info{ull v;int len;};
Info operator +(Info A,Info B){return {A.v*pw[B.len]+B.v,A.len+B.len};}
#define mi ((l+r)>>1)
struct SGT
{
#define ls (p<<1)
#define rs (p<<1|1)
#define lson ls,l,mi
#define rson rs,mi+1,r
Info tr[(1<<19)+3];
void Up(int p){tr[p]=tr[ls]+tr[rs];}
void Build(int op,int p=1,int l=1,int r=n)
{
if(l==r){tr[p]={!op?col[l]:col[n-l+1],1};leaf[l]=p;return;}
Build(op,lson);Build(op,rson);Up(p);
}
void Mdf(int p,ull v)
{
p=leaf[p];tr[p]={v,1};
while(p>1)Up(p>>=1);
}
Info Ask(int L,int R,int p=1,int l=1,int r=n)
{
if(L<=l&&r<=R)return tr[p];
if(R<=mi)return Ask(L,R,lson);
if(L>mi)return Ask(L,R,rson);
return Ask(L,R,lson)+Ask(L,R,rson);
}
}T0,T1;
bool Chk(int l,int r,int len){return T0.Ask(l,l+len-1).v==T1.Ask(n-r+1,n-r+len).v;}
void Mer(int x,int y)
{
x=F(x);y=F(y);
if(ve[x].size()<ve[y].size())swap(x,y);
for(int v:ve[y])ve[x].push_back(v),col[v]=col[x],T0.Mdf(v,col[v]),T1.Mdf(n-v+1,col[v]);
ve[y].clear();f[F(y)]=F(x);
}
int main()
{
cin>>n;pw[0]=1;
for(int i=1;i<N;i++)pw[i]=pw[i-1]*bas;
for(int i=3;i<2*n;i++)cin>>a[i],id[i]=i;
sort(id+3,id+2*n,[](int X,int Y){return a[X]<a[Y];});
for(int i=1;i<=n;i++)col[i]=rd()%(bas-1)+1,f[i]=i,ve[i].push_back(i);
T0.Build(0);T1.Build(1);
for(int t=3;t<2*n;t++)
{
int k=id[t],L=max(k-n,1ll),R=k-L;
while(1)
{
int l=1,r=(k-1)/2-L+1,s=0;
while(l<=r)!Chk(L,R,mi)?(s=mi,r=mi-1):l=mi+1;
if(!s)break;
Mer(L+s-1,R-s+1);ans+=a[k];
}
}
cout<<ans;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 28292kb
input:
1000 345 432 641 771 49 37 930 572 1083 733 1309 1495 818 883 1510 1079 1718 1873 1795 1903 1850 2255 2309 2022 2503 2619 2633 2628 3089 3263 2021 3361 2718 3104 3509 2714 3708 3994 3716 3830 3429 4128 4272 4464 4137 4719 4383 4775 3639 4700 5203 4170 4797 5033 4930 5201 5254 5705 5682 5753 5327 611...
output:
23841384
result:
ok 1 number(s): "23841384"
Test #2:
score: 10
Accepted
time: 3ms
memory: 28260kb
input:
1000 7907859 8299401 2595272 8647244 4654300 116556 9058074 9457856 3976958 11518861 1620710 10332687 19015127 15766719 18789607 17873140 21089373 14170673 12490174 24529096 25019840 25561985 98735 23117075 25603838 24631554 24173505 19647554 31525691 31244267 27972151 24041201 29476743 32211677 381...
output:
245822716951
result:
ok 1 number(s): "245822716951"
Test #3:
score: 10
Accepted
time: 42ms
memory: 31224kb
input:
10000 340635 376047 136654 618815 159696 710061 835001 815618 256229 497032 1343755 1320451 1132380 1539973 1623473 1693936 742042 1524274 1360190 2316919 2412274 1845055 1512219 1892430 2508203 2868404 2749708 2440259 3056384 3057456 3265524 2966678 3189137 3378488 3643366 3679507 3257152 3353380 3...
output:
2486173886938
result:
ok 1 number(s): "2486173886938"
Test #4:
score: 10
Accepted
time: 2150ms
memory: 56624kb
input:
199999 573770324 414266051 954722842 267823478 158683708 565067390 925135266 174993733 436964444 553816220 604600347 779139383 174739126 949957679 854403239 570026878 788469644 953674374 876965030 454165131 744511837 657999668 214085562 499964686 298322155 359355913 174185410 539605497 272107319 335...
output:
1369136336
result:
ok 1 number(s): "1369136336"
Test #5:
score: 10
Accepted
time: 2426ms
memory: 59636kb
input:
199995 915098216 2961244 759529641 482072770 96198925 737065230 449814468 129834835 621735650 54036166 218513119 558467906 677940504 13599685 995819084 510352113 707972383 120352652 589903002 16908544 762512553 739109339 372437660 222576208 513404326 217333429 185120609 391592963 508209737 783114329...
output:
1028748453
result:
ok 1 number(s): "1028748453"
Test #6:
score: 10
Accepted
time: 1387ms
memory: 55072kb
input:
190000 12647 1015 25075 32877 14131 35973 34224 413 44756 46889 54548 54872 63138 61206 71980 85368 66953 33960 95604 43956 100468 110045 110651 72002 125677 123806 133907 140928 103006 130963 143904 169665 170636 165176 183128 175044 182777 187911 196733 199113 207536 212192 226285 232248 234909 21...
output:
42711887507849
result:
ok 1 number(s): "42711887507849"
Test #7:
score: 10
Accepted
time: 1440ms
memory: 55496kb
input:
200000 8109 14228 14749 32990 56681 15919 53325 47174 74650 67783 86459 63058 88143 36203 104344 35793 26382 110478 59803 128301 99948 86098 111738 64341 86847 72244 140370 82851 137577 177720 131119 154285 147726 149828 200941 221507 192221 224332 229763 181710 202066 252618 208158 257523 235035 26...
output:
45006690224362
result:
ok 1 number(s): "45006690224362"
Test #8:
score: 10
Accepted
time: 1452ms
memory: 55476kb
input:
200000 41774 48021 118119 29186 108563 102700 86530 135784 76652 29845 41180 3354 87818 104774 155088 70654 152906 70557 171701 185768 3583 5786 145566 29866 215765 51785 226476 148960 212212 10833 250086 183769 173223 143125 136002 151292 278423 156833 262389 113210 283038 103297 278088 183899 3260...
output:
49872168142307
result:
ok 1 number(s): "49872168142307"
Test #9:
score: 10
Accepted
time: 1441ms
memory: 55476kb
input:
199995 21953 33999 14594 52551 36028 54566 56411 22575 110079 80430 106838 65069 106309 6700 140637 96336 15178 37399 147814 148267 143572 133807 33223 196777 152621 187806 141393 206582 219417 202758 224038 219972 226860 188342 204760 226982 226678 131378 276188 192400 16500 296583 235472 308907 28...
output:
50034940148604
result:
ok 1 number(s): "50034940148604"
Test #10:
score: 10
Accepted
time: 1425ms
memory: 55472kb
input:
199992 15109 10774 19215 25971 19703 9215 31839 29367 42010 30812 43875 31508 40758 64566 22408 68451 60879 61748 29806 78007 76364 88577 52117 73605 106032 86763 127486 99171 117902 139055 167133 87789 107406 157602 160466 182539 183096 172886 187343 195740 175495 199166 125460 167310 209709 203205...
output:
50087500724413
result:
ok 1 number(s): "50087500724413"
Extra Test:
score: 0
Extra Test Passed