2594: [Wc2006]水管局长数据加强版
Time Limit: 25 Sec Memory Limit: 128 MB
Submit: 934 Solved: 291
[Submit][Status]
Description
Input
Output
Sample Input
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4
Sample Output
3
【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【C/C++选手注意事项】
由于此题输入规模较大(最大的测试点约20MB),因此即使使用scanf读入数据也会花费较多的时间。为了节省读入耗时,建议使用以下函数读入正整数(返回值为输入文件中下一个正整数):
int getint()
{
char ch = getchar();
for ( ; ch > '9' || ch < '0'; ch = getchar());
int tmp = 0;
for ( ; '0' <= ch && ch <= '9'; ch = getchar())
tmp = tmp * 10 + int(ch) - 48;
return tmp;
}
动态树的常数优化根本搞不懂,这道题我是全加inline,register,26000ms水过的(bzoj真神奇)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
#define MAXM 1100000
#define MAXN 110000
#define MAXQ 110000
#define MAXT MAXM+MAXN
inline int nextInt()
{
register char ch;
register int x=;
while (ch=getchar(),ch<'' || ch>'');
do
x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x;
}
int n,m,q;
int ch[MAXT][],pnt[MAXT];
int mx[MAXT],mt[MAXT];
int val[MAXT],siz[MAXT],pos[MAXT];
bool rev[MAXT];
int stack[MAXT],tops=-;
inline bool is_root(int now)
{
return !pnt[now] || (ch[pnt[now]][]!=now && ch[pnt[now]][]!=now);
}
inline void update(int now)
{
siz[now]=siz[ch[now][]]+siz[ch[now][]]+;
if (mx[ch[now][]]>mx[ch[now][]])
{
mx[now]=mx[ch[now][]];
mt[now]=mt[ch[now][]];
}else
{
mx[now]=mx[ch[now][]];
mt[now]=mt[ch[now][]];
}
if (mx[now]<val[now])
{
mx[now]=val[now];
mt[now]=now;
}
}
inline void reverse(int now)
{
if (!now)return ;
swap(ch[now][],ch[now][]);
rev[now]^=;
}
inline void down(int now)
{
if (rev[now])
{
reverse(ch[now][]);
reverse(ch[now][]);
rev[now]=;
}
}
inline void rotate(int now)
{
register int p=pnt[now],anc=pnt[p];
register int dir=ch[p][]==now;
if (!is_root(pnt[now]))
ch[anc][ch[anc][]==p]=now;
pnt[now]=anc;
if (ch[now][dir])
pnt[ch[now][dir]]=p;
ch[p][-dir]=ch[now][dir];
pnt[p]=now;
ch[now][dir]=p;
update(p);
update(now);
}
void splay(int now)
{
int x=now;
stack[++tops]=x;
while (!is_root(x))
{
x=pnt[x];
stack[++tops]=x;
}
while (~tops)
down(stack[tops--]);
while (!is_root(now))
{
int p=pnt[now],anc=pnt[p];
if (is_root(p))
rotate(now);
else if ((ch[anc][]==p) == (ch[p][]==now))
rotate(p),rotate(now);
else
rotate(now),rotate(now);
}
}
int access(int now)
{
int son=;
while (now)
{
splay(now);
ch[now][]=son;
son=now;
update(now);
now=pnt[now];
}
return son;
}
inline void make_root(int now)
{
access(now);
splay(now);
reverse(now);
}
struct aaa
{
int x,y,d,id;
}el[MAXM],e[MAXM];
struct bbb
{
int x,y,d,t,id;
}qur[MAXQ];
bool operator <(aaa a1,aaa a2)
{
if (a1.x==a2.x)return a1.y<a2.y;
return a1.x<a2.x;
}
set<aaa> S;
bool cmp_d(aaa a1,aaa a2)
{
return a1.d<a2.d;
}
int uf[MAXN];
int get_fa(int now)
{
return now==uf[now] ? now : uf[now]=get_fa(uf[now]);
}
bool comb(int x,int y)
{
x=get_fa(x);
y=get_fa(y);
if (x==y)return false;
uf[x]=y;
return true;
}
void add_edge(int x,int y)
{
//cout<<"Add:"<<x<<" "<<y<<endl;
make_root(x);
make_root(y);
ch[x][]=y;
pnt[y]=x;
update(x);
}
void erase_edge(int x,int y)
{
//cout<<"Del:"<<x<<" "<<y<<endl;
make_root(x);
access(y);
splay(x);
if (ch[x][]==y)
{
splay(y);
ch[y][]=pnt[x]=;
update(y);
}else if (ch[x][]==y)
{
ch[x][]=pnt[y]=;//注意清空pnt[]
update(x);
}else throw ;
}
vector<int> ans;
pair<int,int> Qry_path(int x,int y)
{
make_root(x);
int t=access(y);
return make_pair(mx[t],mt[t]-n-);
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k;
scanf("%d%d%d",&n,&m,&q);
aaa at;
for (i=;i<=n;i++)uf[i]=i;
for (i=;i<m;i++)
{
at.x=nextInt();at.y=nextInt();at.d=nextInt();
//scanf("%d%d%d",&at.x,&at.y,&at.d);
val[n+i+]=at.d;
at.id=i;
if (at.x>at.y)
swap(at.x,at.y);
S.insert(at);
el[i]=at;
}
set<aaa>::iterator it1;
for (i=;i<q;i++)
{
qur[i].t=nextInt();qur[i].x=nextInt();qur[i].y=nextInt();
//scanf("%d%d%d",&qur[i].t,&qur[i].x,&qur[i].y);
if (qur[i].t==)
{
if (qur[i].x>qur[i].y)
swap(qur[i].x,qur[i].y);
at.x=qur[i].x,at.y=qur[i].y;
it1=S.find(at);
qur[i].d=it1->d;
qur[i].id=it1->id;
S.erase(it1);
}
}
m=;
for (it1=S.begin();it1!=S.end();it1++)
{
e[m++]=*it1;
}
sort(e,e+m,cmp_d);
for (i=;i<m;i++)
{
if (comb(e[i].x,e[i].y))
{
add_edge(e[i].x,n+e[i].id+);
add_edge(e[i].y,n+e[i].id+);
}
}
for (i=q-;i>=;i--)
{
if (qur[i].t==)
{
pair<int,int> pr;
pr=Qry_path(qur[i].x,qur[i].y);
if (pr.first<=qur[i].d)continue;
erase_edge(el[pr.second].x,pr.second++n);
erase_edge(el[pr.second].y,pr.second++n);
add_edge(qur[i].x,qur[i].id+n+);
add_edge(qur[i].y,qur[i].id+n+);
}else
{
pair<int,int> pr;
pr=Qry_path(qur[i].x,qur[i].y);
ans.push_back(pr.first);
//printf("%d\n",pr.first);
}
}
while (ans.size())
{
printf("%d\n",ans[ans.size()-]);
ans.pop_back();
}
}