BZOJ2001 HNOI2010城市建设(线段树分治+LCT)

一个很显然的思路是把边按时间段拆开线段树分治一下,用lct维护MST。理论上复杂度是O((M+Q)logNlogQ),实际常数爆炸T成狗。正解写不动了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 20010
#define M 50010
#define lson tree[k].ch[0]
#define rson tree[k].ch[1]
#define lself tree[tree[k].fa].ch[0]
#define rself tree[tree[k].fa].ch[1]
int n,m,q,f[M],L[M<<],R[M<<],cnt;
long long tot=,ans[M];
struct edge{int x,y,z;
}e[M],a[N+M<<];
struct data{int i,x;};
vector<data> Q[M];
vector<int> tree[M<<];
stack<int> undo[M<<];
namespace lct
{
struct data{int ch[],fa,rev,x;}tree[N+M<<];
void up(int k)
{
tree[k].x=k;
if (a[tree[lson].x].z>a[tree[k].x].z) tree[k].x=tree[lson].x;
if (a[tree[rson].x].z>a[tree[k].x].z) tree[k].x=tree[rson].x;
}
void rev(int k){if (k) swap(lson,rson),tree[k].rev^=;}
void down(int k){if (tree[k].rev) rev(lson),rev(rson),tree[k].rev=;}
bool isroot(int k){return lself!=k&&rself!=k;}
int whichson(int k){return rself==k;}
void push(int k){if (!isroot(k)) push(tree[k].fa);down(k);}
void move(int k)
{
int fa=tree[k].fa,gf=tree[fa].fa,p=whichson(k);
if (!isroot(fa)) tree[gf].ch[whichson(fa)]=k;tree[k].fa=gf;
tree[fa].ch[p]=tree[k].ch[!p],tree[tree[k].ch[!p]].fa=fa;
tree[fa].fa=k,tree[k].ch[!p]=fa;
up(fa),up(k);
}
void splay(int k)
{
push(k);
while (!isroot(k))
{
int fa=tree[k].fa;
if (!isroot(fa))
if (whichson(fa)^whichson(k)) move(k);
else move(fa);
move(k);
}
}
void access(int k){for (int t=;k;t=k,k=tree[k].fa) splay(k),tree[k].ch[]=t,up(k);}
void makeroot(int k){access(k),splay(k),rev(k);}
int findroot(int k){access(k),splay(k);for (;lson;k=lson) down(k);splay(k);return k;}
void link(int x,int y){makeroot(x);tree[x].fa=y;}
void cut(int x,int y){makeroot(x),access(y),splay(y);tree[y].ch[]=tree[x].fa=;up(y);}
int query(int x,int y){makeroot(x),access(y),splay(y);return tree[y].x;}
void newpoint(int k){tot+=a[k].z;link(k,a[k].x),link(k,a[k].y);}
void delpoint(int k){tot-=a[k].z;cut(k,a[k].x),cut(k,a[k].y);}
}
void build(int k,int l,int r)
{
L[k]=l,R[k]=r;
if (l==r) return;
int mid=l+r>>;
build(k<<,l,mid);
build(k<<|,mid+,r);
}
void add(int k,int l,int r,int e)
{
if (L[k]==l&&R[k]==r) {tree[k].push_back(e);return;}
int mid=L[k]+R[k]>>;
if (r<=mid) add(k<<,l,r,e);
else if (l>mid) add(k<<|,l,r,e);
else add(k<<,l,mid,e),add(k<<|,mid+,r,e);
}
void solve(int k)
{
int s=tree[k].size();
for (int i=;i<s;i++)
if (lct::findroot(a[tree[k][i]].x)!=lct::findroot(a[tree[k][i]].y))
lct::newpoint(tree[k][i]),undo[k].push(tree[k][i]);
else
{
int t=lct::query(a[tree[k][i]].x,a[tree[k][i]].y);
if (a[t].z>a[tree[k][i]].z)
{
lct::delpoint(t),lct::newpoint(tree[k][i]);
undo[k].push(-t),undo[k].push(tree[k][i]);
}
}
if (L[k]==R[k]) ans[L[k]]=tot;
else solve(k<<),solve(k<<|);
while (!undo[k].empty())
{
if (undo[k].top()<) lct::newpoint(-undo[k].top());
else lct::delpoint(undo[k].top());
undo[k].pop();
}
}
int main()
{
freopen("bzoj2001.in","r",stdin);
freopen("bzoj2001.out","w",stdout);
n=read(),m=read(),q=read();
for (int i=;i<=m;i++)
e[i].x=read(),e[i].y=read(),e[i].z=read();
for (int i=;i<=q;i++)
{
int x=read(),y=read();
Q[x].push_back((data){i,y});
}
build(,,q);
cnt=n;
for (int i=;i<=m;i++)
{
int s=Q[i].size();
if (s==) a[++cnt]=e[i],add(,,q,cnt);
else
{
if (Q[i][].i>) a[++cnt]=e[i],add(,,Q[i][].i-,cnt);
for (int j=;j<s-;j++)
a[++cnt]=(edge){e[i].x,e[i].y,Q[i][j].x},
add(,Q[i][j].i,Q[i][j+].i-,cnt);
a[++cnt]=(edge){e[i].x,e[i].y,Q[i][s-].x},
add(,Q[i][s-].i,q,cnt);
}
}
solve();
for (int i=;i<=q;i++) printf("%lld\n",ans[i]);
fclose(stdin);fclose(stdout);
return ;
}
上一篇:关于MySql 数据库InnoDB存储引擎介绍


下一篇:mysql innodb存储引擎介绍