[JZOJ5433] FIG.

description

A n points A + B no connected graph of edges, a variable x, the weight of each edge is a simple polynomial with respect to x, wherein A has the right sides is a value of k + x, B additionally weights of edges is kx, if only to retain the weight of the form k + edge x, then this figure remains a connected graph, if only to retain the weight of the form kx edge, this figure also remains a connected graph.
Given query q groups, each challenge value x is given, no Q at this time is the minimum weight spanning tree how to communicate with the FIG.


analysis

  • First, FIG easy to know when any of the \ (the MST \) only of (A \) \ collection or \ (B \) edges within the set consisting of

  • When \ (X \) from \ (- ∞ \) gradually becomes \ (∞ + \) , \ (the MST \) will be made only from the \ (A \) constituting the sides only by becoming \ (B \) the sides forming

  • Here \ (MST \) will change (n-1 \) \ times, then use \ (LCT \) Maintenance \ (MST \) Tim edge Erase

  • Of course, the first \ (Kruskal \) to \ (A \) and \ (B \) each with \ (n-1 \) edges engage them, the \ (A \) frontier to \ (the LCT \) above

  • Ascending order \ (B \) is added to a section of the edge \ (the LCT \) inside and out of the new ring in place of \ (K \) maximum \ (A \) side

  • With the current \ (B \) side \ (KX \) is subtracted by deleting the \ (A \) side \ (K + X \) , will be \ (k'-2x \) a class of formula

  • Note the edge of the ring if there is a query \ (B \) side, since only replace \ (A \) side disregard \ (B \) side, so even \ (B \) side when not even the edge node

  • If multiple \ (B \) side, due to the ascending added \ (LCT \) , so \ (k \) smaller \ (B \) side will contribute early

  • Requirements for the final answers, asking the sort, and has been \ (n-1 \) Article \ (k-2x \) expression of such

  • The formula then \ (k \) ordering, ask to see how many of each side will be used before replacement (that is, how many former formula)

  • This sweeping over the heap formula, as long as the \ (k-2 * \) of the current query number \ (<= 0 \) , indicating that the edge is replaced contribute to continue to sweep down

  • Note that the answer should count each \ (k \) and the rest have not been replaced \ (A \) side in the \ (+ x \)


code

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 500005
#define ha 1926081719491001
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)

using namespace std;

ll tr[MAXN][2],fa[MAXN],pf[MAXN],st[MAXN],fat[MAXN],val[MAXN],answer[MAXN];
ll n,m,A,B,q,tot,cnt,ans;
struct quiry{ll x,y;}inquiry[MAXN];
struct edge{ll x,y,z;}f[2][MAXN],g[MAXN];
struct node{ll mx,val,size;bool rev;}a[MAXN];
inline ll read(){ll x=0,f=1;char ch=getchar();while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
inline void swap(ll &x,ll &y){ll z=x;x=y,y=z;}
inline bool cmp(edge a,edge b){return a.z<b.z;}
inline bool cmpp(quiry a,quiry b){return a.x<b.x;}
inline ll getfa(ll x){return !fat[x]?x:fat[x]=getfa(fat[x]);}
inline void update(ll x){if (!x)return;a[x].mx=x;if (a[a[tr[x][0]].mx].val>a[a[x].mx].val)a[x].mx=a[tr[x][0]].mx;if (a[a[tr[x][1]].mx].val>a[a[x].mx].val)a[x].mx=a[tr[x][1]].mx;a[x].size=a[tr[x][0]].size+a[tr[x][1]].size+1;}
inline void reverse(ll x){if (x)swap(tr[x][0],tr[x][1]),a[x].rev^=1;}
inline void down(ll x){if (a[x].rev)reverse(tr[x][0]),reverse(tr[x][1]),a[x].rev=0;}
inline void downdata(ll x){while (x)st[++st[0]]=x,x=fa[x];while (st[0])down(st[st[0]--]);}
inline ll lr(ll x){return tr[fa[x]][1]==x;}
inline void rotate(ll x){ll y=fa[x],k=lr(x);tr[y][k]=tr[x][!k];if (tr[x][!k])fa[tr[x][!k]]=y;fa[x]=fa[y];if (fa[y])tr[fa[y]][lr(y)]=x;tr[x][!k]=y,fa[y]=x,pf[x]=pf[y],update(y),update(x);}
inline void splay(ll x,ll y){downdata(x);while (fa[x]!=y){if (fa[fa[x]]!=y)rotate(lr(fa[x])==lr(x)?fa[x]:x);rotate(x);}}
inline void access(ll x){for (ll y=0;x;update(x),y=x,x=pf[x])splay(x,0),fa[tr[x][1]]=0,pf[tr[x][1]]=x,tr[x][1]=y,fa[y]=x,pf[y]=0;}
inline void makeroot(ll x){access(x),splay(x,0),reverse(x);}
inline void link(ll x,ll y){makeroot(x),pf[x]=y;}
inline void cut(ll x,ll y){makeroot(x),access(y),splay(x,0),tr[x][1]=fa[y]=pf[y]=0,update(x);}
inline ll query(ll x,ll y){makeroot(x),access(y),splay(y,0);return a[y].mx;}
int main()
{
    freopen("T3.in","r",stdin);
    //freopen("graph.in","r",stdin);
    //freopen("graph.out","w",stdout);
    n=read(),A=read(),B=read(),q=read();
    fo(i,1,A)f[0][i].x=read(),f[0][i].y=read(),f[0][i].z=read();
    fo(i,1,B)f[1][i].x=read(),f[1][i].y=read(),f[1][i].z=read();
    sort(f[0]+1,f[0]+A+1,cmp),sort(f[1]+1,f[1]+B+1,cmp);
    fo(i,0,n)a[i].val=-ha;
    fo(i,1,A)
    {
        ll x=f[0][i].x,y=f[0][i].y,z=f[0][i].z;
        if (getfa(x)!=getfa(y))fat[getfa(x)]=getfa(y),ans+=z,
        a[n+i].val=z,a[n+i].mx=n+i,link(x,n+i),link(n+i,y);
    }
    memset(fat,0,sizeof(fat));
    fo(i,1,B)
    {
        ll x=f[1][i].x,y=f[1][i].y,z=f[1][i].z;
        if (getfa(x)!=getfa(y))fat[getfa(x)]=getfa(y),g[++tot]=f[1][i];
    }
    fo(i,1,tot)
    {
        ll x=g[i].x,y=g[i].y,z=g[i].z,tmp=query(x,y);
        if (tmp<=n)continue;
        cut(f[0][tmp-n].x,tmp),cut(tmp,f[0][tmp-n].y);
        link(x,y),val[++cnt]=z-a[tmp].val;
    }
    fo(i,1,q)inquiry[i].x=read(),inquiry[i].y=i;
    sort(inquiry+1,inquiry+q+1,cmpp),sort(val+1,val+cnt+1);
    fo(i,1,q)
    {
        while (m<tot && val[m+1]<=inquiry[i].x*2)ans+=val[++m];
        answer[inquiry[i].y]=ans+inquiry[i].x*(n-m*2-1);
    }
    fo(i,1,q)printf("%lld\n",answer[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/horizonwd/p/11600131.html