QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#270372#6520. Classic ProblemC1942huangjiaxuWA 1391ms31332kbC++142.0kb2023-11-30 19:58:402023-11-30 19:58:41

Judging History

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

  • [2023-11-30 19:58:41]
  • 评测
  • 测评结果:WA
  • 用时:1391ms
  • 内存:31332kb
  • [2023-11-30 19:58:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
typedef long long ll;
ll ans;
int T,n,m,a[N],ca,fa[N],pl[N],pr[N],cp,va[N],iv[N],id[N],cnt,p1[N],p2[N],f1[N],f2[N];
map<pair<int,int>,int>mp;
struct edge{
	int x,y,z;
}e[N];
int gf(int *fa,int x){
	return fa[x]==x?fa[x]:fa[x]=gf(fa,fa[x]);
}
void solve(){
	scanf("%d%d",&n,&m),ca=0;
	for(int i=1,x,y,z;i<=m;++i){
		scanf("%d%d%d",&x,&y,&z);
		a[++ca]=x,a[++ca]=y;
		e[i]={x,y,z};
	}
	sort(a+1,a+ca+1);
	ca=unique(a+1,a+ca+1)-a-1,cp=ans=0;
	for(int i=1;i<=ca;++i){
		if(a[i]!=a[i-1]+1)++cp,pl[cp]=a[i-1]+1,pr[cp]=a[i]-1;
		++cp,pl[cp]=pr[cp]=a[i],id[i]=cp;
	}
	mp.clear();
	for(int i=1;i<=m;++i){
		e[i].x=id[lower_bound(a+1,a+ca+1,e[i].x)-a];
		e[i].y=id[lower_bound(a+1,a+ca+1,e[i].y)-a];
		mp[make_pair(e[i].x,e[i].y)]=e[i].z;
	}
	if(a[ca]!=n)++cp,pl[cp]=a[ca]+1,pr[cp]=n;
	for(int i=1;i<=cp;++i)fa[i]=i,ans+=pr[i]-pl[i],p1[i]=i-1,p2[i]=i+1,f1[i]=i,f2[i]=i;
	cnt=cp;
	while(cnt>1){
		for(int i=1;i<=cp;++i)va[i]=1e9+7,iv[i]=0;
		for(int i=1;i<=cp;++i){
			int u=gf(fa,i);
			while(p1[i]&&(gf(fa,p1[i])==u||mp.count(make_pair(p1[i],i)))){
				--p1[i];
				if(p1[i])p1[i]=gf(f1,p1[i]);
			}
			if(p1[i]&&va[u]>pl[i]-pr[p1[i]]){
				va[u]=pl[i]-pr[p1[i]];
				iv[u]=gf(fa,p1[i]);
			}
			while(p2[i]<=cp&&(gf(fa,p2[i])==u||mp.count(make_pair(i,p2[i])))){
				++p2[i];
				if(p2[i]<=cp)p2[i]=gf(f2,p2[i]);
			}
			if(p2[i]<=cp&&va[u]>pl[p2[i]]-pr[i]){
				va[u]=pl[p2[i]]-pr[i];
				iv[u]=gf(fa,p2[i]);
			}
		}
		for(int i=1;i<=m;++i){
			int u=gf(fa,e[i].x),v=gf(fa,e[i].y);
			if(u==v)continue;
			if(va[u]>e[i].z)va[u]=e[i].z,iv[u]=v;
			if(va[v]>e[i].z)va[v]=e[i].z,iv[v]=u;
		}
		for(int i=1;i<=cp;++i)if(fa[i]==i)fa[gf(fa,iv[i])]=i,ans+=va[i];
		for(int i=1;i<=cp;++i){
			if(i>1&&f1[i]==i&&gf(fa,i-1)==gf(fa,i))f1[i]=f1[i-1];
			if(i<cp&&f2[i]==i&&gf(fa,i+1)==gf(fa,i))f2[i]=f2[i+1];
		}
		cnt=0;
		for(int i=1;i<=cp;++i)if(fa[i]==i)++cnt;
	}
	printf("%lld\n",ans);
}
int main(){
	scanf("%d",&T);
	while(T--)solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3704kb

input:

3
5 3
1 2 5
2 3 4
1 5 0
5 0
5 4
1 2 1000000000
1 3 1000000000
1 4 1000000000
1 5 1000000000

output:

4
4
1000000003

result:

ok 3 number(s): "4 4 1000000003"

Test #2:

score: 0
Accepted
time: 1391ms
memory: 26640kb

input:

16
1000000000 0
447 99681
1 2 1000000000
1 3 1000000000
2 3 1000000000
1 4 1000000000
2 4 1000000000
3 4 1000000000
1 5 1000000000
2 5 1000000000
3 5 1000000000
4 5 1000000000
1 6 1000000000
2 6 1000000000
3 6 1000000000
4 6 1000000000
5 6 1000000000
1 7 1000000000
2 7 1000000000
3 7 1000000000
4 7 ...

output:

999999999
446000000000
732256441
999999680
999899999
999966830
127
4
2186
1562
1176
5100
5
503
679
4

result:

ok 16 numbers

Test #3:

score: 0
Accepted
time: 545ms
memory: 31332kb

input:

5
1000000000 100000
158260500 877914550 822889311
24979400 861648750 548830120
433933400 476190600 342858071
211047200 971407750 480845266
731963950 822804750 449656269
430302150 982631900 545923679
880895700 923078500 816372580
189330700 910286900 135599877
303238500 404539650 605997004
492686550 7...

output:

999999999
999999999
999999999
999999999
999999999

result:

ok 5 number(s): "999999999 999999999 999999999 999999999 999999999"

Test #4:

score: 0
Accepted
time: 655ms
memory: 24564kb

input:

5
2000 100000
1873 1874 822889311
1290 1291 548830120
1162 1163 342858071
814 815 480845266
742 743 449656269
1609 1610 545923679
1431 1432 816372580
1143 1144 135599877
414 415 605997004
1117 1118 921749002
121 122 786119025
1672 1673 655702582
38 39 211428413
1639 1640 393671555
922 923 501983447
...

output:

4097
20020
198635
2099999
1000099999

result:

ok 5 number(s): "4097 20020 198635 2099999 1000099999"

Test #5:

score: 0
Accepted
time: 450ms
memory: 21848kb

input:

5
100000 100000
36426 60522 446755913
60522 90081 181424399
3497 60522 625711486
60522 94325 647639160
60522 68417 160714711
35902 60522 61020590
23857 60522 626006012
29211 60522 547865075
60522 63340 561330066
60016 60522 650693494
24593 60522 849028504
60522 90310 285323416
11431 60522 990607689
...

output:

100024
150003
200003
250000
999999999

result:

ok 5 number(s): "100024 150003 200003 250000 999999999"

Test #6:

score: -100
Wrong Answer
time: 428ms
memory: 12552kb

input:

5
4000 100000
694 696 619136615
1611 1614 829153748
2551 2552 370724298
3034 3037 49559541
98 105 443754249
822 827 735959328
878 885 201346483
2729 2731 304071225
3961 3965 557890416
1631 1637 430215474
3195 3205 212882505
503 507 937363346
3141 3150 47574456
577 583 727402691
3673 3675 279029853
3...

output:

43935
7037
4340
18651
12683

result:

wrong answer 2nd numbers differ - expected: '6913', found: '7037'