QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#116137 | #6520. Classic Problem | lxhcr | TL | 3463ms | 70904kb | C++14 | 4.3kb | 2023-06-28 10:44:42 | 2023-06-28 10:44:45 |
Judging History
你现在查看的是最新测评结果
- [2023-12-06 00:08:28]
- hack成功,自动添加数据
- (//qoj.ac/hack/481)
- [2023-08-10 23:21:45]
- System Update: QOJ starts to keep a history of the judgings of all the submissions.
- [2023-06-28 10:44:42]
- 提交
answer
#include <bits/stdc++.h>
typedef long long ll;typedef unsigned long long ull; typedef double db;typedef long double ldb;
#define fre(x) freopen(#x ".in","r",stdin),freopen(#x ".out","w",stdout)
#define Rep(i,a,b) for(int i=a;i<=b;++i)
#define Dwn(i,a,b) for(int i=a;i>=b;--i)
#define pii pair<int,int>
#define mair make_pair
#define fir first
#define sec second
using namespace std;
const int maxn=5e5+10;
int n,m;
unordered_map<int,int>MapId,UpId;
vector<pii>E[maxn];
int Id(int x){ if(MapId.count(x))return MapId[x]; MapId[x]=MapId.size()+1;E[MapId[x]].resize(0);return MapId[x]; }
vector<int>vec;
pii a[maxn],val[maxn];int tot;
ll ans;
int pre[maxn];bool vis[maxn];
struct UN{
int f[maxn];
void Init(int n){ Rep(i,1,n)f[i]=i; }
int Find(int x){ return f[x]==x ? x : f[x]=Find(f[x]); }
int Merge(int x,int y){
x=Find(x),y=Find(y);if(x==y)return 0;
f[y]=x;return 1;
}
}S;
void solve(){
cin>>n>>m;unordered_map<int,int>().swap(MapId);unordered_map<int,int>().swap(UpId);vec.resize(0);tot=0;ans=0;
Rep(i,1,m){
int u,v,w;cin>>u>>v>>w;E[Id(u)].emplace_back(v,w),E[Id(v)].emplace_back(u,w);
vec.emplace_back(u),vec.emplace_back(v);
}sort(vec.begin(),vec.end()),vec.erase(unique(vec.begin(),vec.end()),vec.end());
int last=0;
for(auto it : vec){
if(last+1<it)a[++tot]=pii(last+1,it-1),ans+=it-last-2;
last=it,a[++tot]=pii(it,it);
}if(last<n)a[++tot]=pii(last+1,n),ans+=n-last-1;
S.Init(tot);int Ecnt=0;Rep(i,1,tot)UpId[a[i].fir]=UpId[a[i].sec]=i;
while(Ecnt+1<tot){
Rep(i,1,tot)val[i]=pii(INT_MAX,0),pre[i]=0,S.Find(i),pre[i]=0;
pii lasp[2];
Rep(i,1,tot){
pre[i]=(lasp[0].sec==S.f[i] ? UpId[lasp[1].fir] : UpId[lasp[0].fir]);
if(a[i].sec>a[i].fir || (!MapId.count(a[i].fir))){
if(lasp[0].sec && lasp[0].sec!=S.f[i]){
val[lasp[0].sec]=min(val[lasp[0].sec],pii(a[i].fir-lasp[0].fir,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(a[i].fir-lasp[0].fir,lasp[0].sec));
}else if(lasp[1].sec && lasp[1].sec!=S.f[i]){
val[lasp[1].sec]=min(val[lasp[1].sec],pii(a[i].fir-lasp[1].fir,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(a[i].fir-lasp[1].fir,lasp[1].sec));
}
}else{
int u=MapId[a[i].fir],x=S.f[i],y;vector<int>pp;
for(auto [v,w] : E[u]){
y=S.Find(UpId[v]);
if(x!=y){ val[x]=min(val[x],pii(w,y)); val[y]=min(val[y],pii(w,x));vis[UpId[v]]=true; }
}
int now=pre[i];
while(now){
if(vis[now]){ --now;continue; }
if(S.f[now]==S.f[i]){now=pre[now];}
else{
val[S.f[now]]=min(val[S.f[now]],pii(a[i].fir-a[now].sec,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(a[i].fir-a[now].sec,S.f[i]));
break;
}
}
for(auto [v,w] : E[u])vis[UpId[v]]=false;
}
if(S.f[i]!=lasp[0].sec){
lasp[1]=lasp[0];lasp[0]=pii(a[i].sec,S.f[i]);
}else lasp[0].fir=a[i].sec;
}lasp[0]=lasp[1]=pii(0,0);
Dwn(i,tot,1){
pre[i]=(lasp[0].sec==S.f[i] ? UpId[lasp[1].fir] : UpId[lasp[0].fir]);
if(a[i].sec>a[i].fir || (!MapId.count(a[i].fir))){
if(lasp[0].sec && lasp[0].sec!=S.f[i]){
val[lasp[0].sec]=min(val[lasp[0].sec],pii(lasp[0].fir-a[i].sec,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(lasp[0].fir-a[i].sec,lasp[0].sec));
}else if(lasp[1].sec && lasp[1].sec!=S.f[i]){
val[lasp[1].sec]=min(val[lasp[1].sec],pii(lasp[1].fir-a[i].sec,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(lasp[1].fir-a[i].sec,lasp[1].sec));
}
}else{
int u=MapId[a[i].fir],x=S.f[i],y;vector<int>pp;
for(auto [v,w] : E[u]){
y=S.Find(UpId[v]);
if(x!=y){ val[x]=min(val[x],pii(w,y)); val[y]=min(val[y],pii(w,x));vis[UpId[v]]=true; }
}
int now=pre[i];
while(now && now<=tot){
if(vis[now]){ ++now;continue; }
if(S.f[now]==S.f[i]){ now=pre[now]; }
else{
val[S.f[now]]=min(val[S.f[now]],pii(a[now].fir-a[i].sec,S.f[i]));
val[S.f[i]]=min(val[S.f[i]],pii(a[now].fir-a[i].sec,S.f[i]));
break;
}
}
for(auto [v,w] : E[u])vis[UpId[v]]=false;
}
if(S.f[i]!=lasp[0].sec){
lasp[1]=lasp[0];lasp[0]=pii(a[i].fir,S.f[i]);
}else lasp[0].fir=a[i].fir;
}
Rep(i,1,tot)if(S.Find(i)==i && val[i].fir!=INT_MAX){
if(S.Merge(i,val[i].sec)){ ++Ecnt;ans+=val[i].fir; }
}
}
cout<<ans<<"\n";
}
int tex;
int main (){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);cin>>tex;while(tex--)solve();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 22148kb
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: 3463ms
memory: 70904kb
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: 1292ms
memory: 69452kb
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: 1299ms
memory: 66164kb
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: 587ms
memory: 50924kb
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: 0
Accepted
time: 727ms
memory: 28196kb
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 6913 4336 18350 12678
result:
ok 5 number(s): "43935 6913 4336 18350 12678"
Test #7:
score: -100
Time Limit Exceeded
input:
5 100000 100000 73979 73980 5250107 1946 1947 757506029 87433 87434 443673136 64352 64354 338125488 69103 69104 235256717 60843 60845 20769731 23601 23602 214534313 92417 92419 669411181 57364 57365 519766962 42029 42031 827237806 98524 98525 593643533 71482 71483 662414293 6709 6710 745687608 36460...