QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#849845#9904. 最小生成树qwezky80 1871ms59376kbC++143.8kb2025-01-09 18:38:572025-01-09 18:38:59

Judging History

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

  • [2025-01-09 18:38:59]
  • 评测
  • 测评结果:80
  • 用时:1871ms
  • 内存:59376kb
  • [2025-01-09 18:38:57]
  • 提交

answer

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
#include <deque>
#include <map>
#include <vector>
#include <list>
#include <functional>
#include <stack>
#include <unordered_map>
#include <set>
#include <cctype>
#include <cstring>
#include <tuple>
#include <bitset>
#include <unordered_set>
#include <iomanip>
#include <utility>
#include <ctime>
#include <iterator>
#include <numeric>
#include <random>
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
using namespace std;
typedef unsigned short us;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef unsigned __int128 ulll;
typedef unsigned int uint;
typedef unsigned char uc;
typedef unsigned short us;
typedef long double ld;
const int M=1e9+9,N=2e5+5,LOGN=20,YI=1,LING=0,MOD=1e9+7; const ll LM=1e18+18,P=133377773331ll;
int n,logn;
ll ans;
struct ai
{
	int a,i;
}
a[N*2];
ull xx[N],pp[N];
struct hh
{
	ull a;
	int len;
	hh():len(1){}
	hh(const ull x,const int l):a(x),len(l){}
	hh operator + (const hh &x) {return hh(a*pp[x.len]+x.a,len+x.len);}
	bool operator == (const hh &x) {return a==x.a&&len==x.len;}
};
namespace xds
{
	struct node
	{
		int l,r,mid;
		hh hl,hr;
	}
	tree[N*4];

	int l,r,x,k;

	#define li i*2
	#define ri li+1
	#define p tree[i]
	#define lz tree[li]
	#define rz tree[ri]
	#define bao (p.l>=l&&p.r<=r)
	#define wu (p.l>r||p.r<l)
	#define up p.hl=lz.hl+rz.hl,p.hr=rz.hr+lz.hr;

	void build(const int i,const int l,const int r)
	{
		p.l=l,p.r=r,p.mid=(l+r)>>1;
		if(l==r) p.hl.a=p.hr.a=xx[l];
		else build(li,l,p.mid),build(ri,p.mid+1,r),up;
	}

	void change(const int i=1)
	{
		if(wu) return ;
		if(bao) p.hl.a=p.hr.a=xx[k];
		else change(li),change(ri),up;
	}

	hh ask_l(const int i=1)
	{
		if(bao) return p.hl;
		if(p.mid>=r) return ask_l(li);
		if(p.mid< l) return ask_l(ri);
		return ask_l(li)+ask_l(ri);
	}

	hh ask_r(const int i=1)
	{
		if(bao) return p.hr;
		if(p.mid>=r) return ask_r(li);
		if(p.mid< l) return ask_r(ri);
		return ask_r(ri)+ask_r(li);
	}
}
#define change(cx,ck) xds::l=xds::r=cx,xds::k=ck,xds::change()
inline hh ask_l(const int cl,const int cr) {xds::l=cl,xds::r=cr; return xds::ask_l();}
inline hh ask_r(const int cl,const int cr) {xds::l=cl,xds::r=cr; return xds::ask_r();}
namespace bcj
{
	list<int> z[N];
	int fu[N];
	void lian(int x,int y)
	{
		x=fu[x],y=fu[y];
		if(z[x].size()<z[y].size()) swap(x,y);
		for(const int &i:z[y]) change(i,x),fu[i]=x;
		z[x].splice(z[x].end(),z[y]);
	}
}
#define p2(x) (1<<x)
int l,r,mid;
bool solve(const int a)
{
	if(a-1<=n)
	{
		l=0,r=(a+1)/2;
		while(l+1<r)
		{
			mid=(l+r)>>1;
			if(ask_l(1,mid)==ask_r(a-mid,a-1)) l=mid;
			else r=mid;
		}
		if(l==(a-1)/2) return 0;
		else
		{
			bcj::lian(l+1,a-l-1);
			return 1;
		}
	}
	else
	{
		l=0,r=(n*2-a+1)/2+1;
		while(l+1<r)
		{
			mid=(l+r)>>1;
			if(ask_l(a-n,a-n+mid-1)==ask_r(n-mid+1,n)) l=mid;
			else r=mid;
		}
		if(l==(n*2-a+1)/2) return 0;
		else
		{
			bcj::lian(a-n+l,n-l);
			return 1;
		}
	}

}
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	//freopen("test.in","r",stdin),freopen("qwe.out","w",stdout);
	cin>>n; logn=log2(n);
	mt19937 rng(time(nullptr));
	uniform_int_distribution<ll> r_x(1,M);
	pp[0]=1;
	for(int i=1;i<=n;i++) xx[i]=r_x(rng),bcj::fu[i]=i,pp[i]=pp[i-1]*P;
	for(int i=1;i<=n;i++) bcj::z[i].push_back(i);
	xds::build(1,1,n);
	for(int i=3;i<=n*2-1;i++) cin>>a[i].a,a[i].i=i;
	sort(a+3,a+n*2,[&](const ai &a,const ai &b){return a.a<b.a;});
	for(int i=3;i<=n*2-1;i++)
		while(solve(a[i].i))
			ans+=a[i].a;
	cout<<ans;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 3ms
memory: 49540kb

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: 51060kb

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: 55ms
memory: 49704kb

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: 0
Time Limit Exceeded

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:


result:


Test #5:

score: 0
Time Limit Exceeded

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:


result:


Test #6:

score: 10
Accepted
time: 1751ms
memory: 58772kb

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: 1843ms
memory: 59376kb

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: 1871ms
memory: 59188kb

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: 1844ms
memory: 59284kb

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: 1837ms
memory: 59192kb

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"