提交时间:2024-11-14 14:28:13

运行 ID: 34743

#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define pi pair<int,int> #define p1 first #define p2 second #define m_p make_pair #define p_b push_back using namespace std; typedef long long ll; const int maxn=1e6+10,mod=998244353; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; } int n,q,fa[3005]; ll px[3005],py[3005]; struct nd { int x,y;ll w; }d[int(4.5e6)+1]; bool operator<(nd a,nd b){return a.w<b.w;} int fd(int x){if(x==fa[x])return x;return fa[x]=fd(fa[x]);} void KR1(int m,vector<nd> &E){ up(i,1,n)fa[i]=i; sort(d+1,d+m+1); up(i,1,m){ int a=fd(d[i].x),b=fd(d[i].y); if(a!=b)E.p_b(d[i]),fa[a]=b; } } ll KR2(vector<nd> &E){ up(i,1,n)fa[i]=i; ll res=0; for(auto it:E){ int a=fd(it.x),b=fd(it.y); if(a!=b)res+=it.w,fa[a]=b; }return res; } ll pw(ll a){if(a<0)a=-a;return a*a*a;} void slv(){ n=read(),q=read(); up(i,1,n)px[i]=read(),py[i]=read(); int m=0;up(i,1,n)up(j,i+1,n)d[++m]=(nd){i,j,pw(px[i]-px[j])+pw(py[i]-py[j])}; vector<nd>E;KR1(m,E); printf("%lld\n",KR2(E)); while(q--){ int x=read(),y=read();ll w=read(); //E.p_b((nd){x,y,w}); nd f=(nd){x,y,w}; E.insert(lower_bound(E.begin(),E.end(),f),f); printf("%lld\n",KR2(E)); } } int main(){ //freopen("logistics.in","r",stdin); //freopen("logistics.out","w",stdout); slv(); fclose(stdin); fclose(stdout); return 0; }