QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#153556#6520. Classic ProblemqzezRE 381ms29336kbC++141.9kb2023-08-30 11:25:032023-08-30 11:25:04

Judging History

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

  • [2023-08-30 11:25:04]
  • 评测
  • 测评结果:RE
  • 用时:381ms
  • 内存:29336kb
  • [2023-08-30 11:25:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=2e5+10,E=2e7+10;
int T,n,m,k;
struct edges{
	int u,v,w;
}e[E];
int p[N],cnt;
ll ans;
vector<int>to[N];
int fa[N];
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return 0;
	return fa[x]=y,1;
}
int vis[N];
void get(){
	scanf("%d%d",&m,&k);
	for(int i=1;i<=k;i++){
		scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		p[++n]=e[i].u,p[++n]=e[i].v;
	}
	sort(p+1,p+1+n),n=unique(p+1,p+1+n)-p-1;
	if(p[n]!=m)p[++n]=m;
	auto trs=[&](int &x){
		x=lower_bound(p+1,p+1+n,x)-p;
	};
	for(int i=1;i<=k;i++){
		trs(e[i].u),trs(e[i].v);
		to[e[i].v].push_back(e[i].u);
	}
	int cnt=n;
	for(int i=1;i<=n;i++){
		ans+=p[i]-p[i-1]-1;
		cnt-=p[i]-p[i-1]-1;
	}
	int lim=sqrt(k*2)+10;
	for(int i=1;i<=n;i++){
		for(int v:to[i])vis[v]=1;
		int t=p[i-1]+1;
		for(int l=i-1;l>=1;l--){
			int d=-1;
			if(!vis[l])d=t-p[l];
			else if(p[i]>t)d=t-p[l];
			else if(p[l-1]+1<p[l])d=t-p[l]+1;
			else continue;
			if(d>lim)break;
			e[++k]={l,i,d};
		}
		for(int v:to[i])vis[v]=0;
	}
	iota(fa,fa+1+n,0);
	sort(e+1,e+1+k,[](edges x,edges y){return x.w<y.w;});
	for(int i=1;i<=k;i++){
		if(merge(e[i].u,e[i].v))ans+=e[i].w;
	}
	printf("%lld\n",ans);
}
void clr(){
	for(int i=1;i<=n;i++)to[i].clear();
	n=ans=0;
}
int main(){
	for(scanf("%d",&T);T--;clr())get();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 10116kb

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: 381ms
memory: 29336kb

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: 317ms
memory: 22712kb

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: -100
Runtime Error

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:


result: