cogs1885 [WC2006]水管局长数据加强版
BZOJ卡不过灰常蛋疼(毕竟人蠢自带巨大常数
这和动态维护最小生成树很像,但加边变成了删边,似乎没法做了。
然后根据之前的套路离线做,删边变成加边,就可以做了orz
二分查找的:(慢
// It is made by XZZ
#include<cstdio>
#include<algorithm>
#include<map>
#include<stack>
#define il inline
#define rg register
#define vd void
#define sta static
using namespace std;
il int gi(){
rg int x=0,f=1;rg char ch=getchar();
while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int maxn=100001;
stack<int>stk;
namespace LCT{
typedef const int& fast;
int ch[maxn*2][2],fa[maxn*2],mx[maxn*2],w[maxn*2],index,A[maxn*2],B[maxn*2];bool rev[maxn*2];
il vd upd(fast x){
mx[x]=x;
if(w[mx[ch[x][0]]]>w[mx[x]])mx[x]=mx[ch[x][0]];
if(w[mx[ch[x][1]]]>w[mx[x]])mx[x]=mx[ch[x][1]];
}
il vd Rev(fast x){if(x)rev[x]^=1,swap(ch[x][0],ch[x][1]);}
il vd down(fast x){if(rev[x])Rev(ch[x][0]),Rev(ch[x][1]),rev[x]=0;}
il bool isrt(fast x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
il vd rotate(fast x){
sta int y,z,o;y=fa[x],z=fa[y],o=x==ch[y][1];
if(!isrt(y))ch[z][y==ch[z][1]]=x;fa[x]=z;
ch[y][o]=ch[x][!o],fa[ch[x][!o]]=y;
ch[x][!o]=y,fa[y]=x;
upd(y);
}
il vd splay(fast x){
sta int y,z,stk[maxn],top;stk[top=1]=x;
for(rg int i=x;!isrt(i);i=fa[i])stk[++top]=fa[i];
while(top)down(stk[top--]);
for(y=fa[x],z=fa[y];!isrt(x);rotate(x),y=fa[x],z=fa[y])
if(!isrt(y))rotate(((x==ch[y][0])^(y==ch[z][0]))?y:x);
upd(x);
}
il vd access(int x){for(rg int y=0;x;x=fa[y=x])splay(x),ch[x][1]=y,upd(x);}
il vd makert(fast x){access(x),splay(x),Rev(x);}
il vd link(fast x,fast y){makert(x),fa[x]=y;}
il vd cut(fast x,fast y){makert(x),access(y),splay(y);fa[x]=ch[y][0]=0;}
il int Query(fast x,fast y){makert(x),access(y),splay(y);return mx[y];}
il int find(int x){access(x),splay(x);while(ch[x][0])x=ch[x][0];return x;}
il vd Link(fast x,fast y,fast _w){
if(find(x)^find(y)){w[++index]=_w,link(A[index]=x,index),link(B[index]=y,index);return;}
sta int z;z=Query(x,y);
if(_w>=w[z])return;
cut(A[z],z),cut(B[z],z);
w[z]=_w,link(A[z]=x,z),link(B[z]=y,z);
}
};
namespace GGMap{
struct gzy{int x,y,*k;}s[maxn];
il bool cmp(const gzy&a,const gzy&b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int tot=0;
il vd insert(int a,int b,int*c){++tot,s[tot].x=a,s[tot].y=b,s[tot].k=c;}
il vd prepare(){sort(s+1,s+tot+1,cmp);}
il int* find(int a,int b){
rg int l=1,r=tot,mid;
while(l<r){
mid=(l+r)>>1;
if(s[mid].x<a||(s[mid].x==a&&s[mid].y<b))l=mid+1;
else r=mid;
}
if(s[l].x==a&&s[l].y==b)return s[l].k;
else return NULL;
}
}
int opt[maxn],A[maxn],B[maxn],C[maxn];
int X[10000001],Y[10000001],Z[10000001];
int main(){
freopen("tube_strong.in","r",stdin);
freopen("tube_strong.out","w",stdout);
int n=gi(),m=gi(),q=gi();
LCT::index=n;
for(rg int i=1;i<=m;++i){X[i]=gi(),Y[i]=gi(),Z[i]=gi();if(X[i]>Y[i])swap(X[i],Y[i]);}
for(rg int i=1;i<=q;++i){
opt[i]=gi(),A[i]=gi(),B[i]=gi();if(A[i]>B[i])swap(A[i],B[i]);
if(opt[i]==2)GGMap::insert(A[i],B[i],&C[i]);
}
GGMap::prepare();
for(rg int i=1;i<=m;++i){
int *it=GGMap::find(X[i],Y[i]);
if(it==NULL)LCT::Link(X[i],Y[i],Z[i]);
else *it=Z[i];
}
while(q){
if(opt[q]==1)stk.push(LCT::w[LCT::Query(A[q],B[q])]);
else LCT::Link(A[q],B[q],C[q]);
--q;
}
while(!stk.empty())printf("%d\n",stk.top()),stk.pop();
return 0;
}
map+一丁点哈希优化:(较上面快,但还是慢
// It is made by XZZ
#include<cstdio>
#include<algorithm>
#include<map>
#include<stack>
#define il inline
#define rg register
#define vd void
#define sta static
using namespace std;
il int gi(){
rg int x=0,f=1;rg char ch=getchar();
while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int maxn=100001;
map<pair<int,int>,int>yyb[32768];stack<int>stk;
namespace LCT{
typedef const int& fast;
int ch[maxn*2][2],fa[maxn*2],mx[maxn*2],w[maxn*2],index,A[maxn*2],B[maxn*2];bool rev[maxn*2];
il vd upd(fast x){
mx[x]=x;
if(w[mx[ch[x][0]]]>w[mx[x]])mx[x]=mx[ch[x][0]];
if(w[mx[ch[x][1]]]>w[mx[x]])mx[x]=mx[ch[x][1]];
}
il vd Rev(fast x){if(x)rev[x]^=1,swap(ch[x][0],ch[x][1]);}
il vd down(fast x){if(rev[x])Rev(ch[x][0]),Rev(ch[x][1]),rev[x]=0;}
il bool isrt(fast x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
il vd rotate(fast x){
sta int y,z,o;y=fa[x],z=fa[y],o=x==ch[y][1];
if(!isrt(y))ch[z][y==ch[z][1]]=x;fa[x]=z;
ch[y][o]=ch[x][!o],fa[ch[x][!o]]=y;
ch[x][!o]=y,fa[y]=x;
upd(y);
}
il vd splay(fast x){
sta int y,z,stk[maxn],top;stk[top=1]=x;
for(rg int i=x;!isrt(i);i=fa[i])stk[++top]=fa[i];
while(top)down(stk[top--]);
for(y=fa[x],z=fa[y];!isrt(x);rotate(x),y=fa[x],z=fa[y])
if(!isrt(y))rotate(((x==ch[y][0])^(y==ch[z][0]))?y:x);
upd(x);
}
il vd access(int x){for(rg int y=0;x;x=fa[y=x])splay(x),ch[x][1]=y,upd(x);}
il vd makert(fast x){access(x),splay(x),Rev(x);}
il vd link(fast x,fast y){makert(x),fa[x]=y;}
il vd cut(fast x,fast y){makert(x),access(y),splay(y);fa[x]=ch[y][0]=0;}
il int Query(fast x,fast y){makert(x),access(y),splay(y);return mx[y];}
il int find(int x){access(x),splay(x);while(ch[x][0])x=ch[x][0];return x;}
il vd Link(fast x,fast y,fast _w){
if(find(x)^find(y)){w[++index]=_w,link(A[index]=x,index),link(B[index]=y,index);return;}
sta int z;z=Query(x,y);
if(_w>=w[z])return;
cut(A[z],z),cut(B[z],z);
w[z]=_w,link(A[z]=x,z),link(B[z]=y,z);
}
};
int opt[maxn],A[maxn],B[maxn],C[maxn];
int X[10000001],Y[10000001],Z[10000001];
int main(){
freopen("tube_strong.in","r",stdin);
freopen("tube_strong.out","w",stdout);
int n=gi(),m=gi(),q=gi(),k;
LCT::index=n;
for(rg int i=1;i<=m;++i){X[i]=gi(),Y[i]=gi(),Z[i]=gi();if(X[i]>Y[i])swap(X[i],Y[i]);}
for(rg int i=1;i<=q;++i){
opt[i]=gi(),A[i]=gi(),B[i]=gi();if(A[i]>B[i])swap(A[i],B[i]);
if(opt[i]==2)yyb[(A[i]+B[i]*23333ll)&32767][make_pair(A[i],B[i])]=-1;
}
for(rg int i=1;i<=m;++i){
sta map<pair<int,int>,int>::iterator it;
k=(X[i]+Y[i]*23333ll)&32767;
it=yyb[k].find(make_pair(X[i],Y[i]));
if(it==yyb[k].end())LCT::Link(X[i],Y[i],Z[i]);
else it->second=Z[i];
}
while(q){
if(opt[q]==1)stk.push(LCT::w[LCT::Query(A[q],B[q])]);
else
LCT::Link(A[q],B[q],yyb[(A[q]+B[q]*23333ll)&32767][make_pair(A[q],B[q])]);
--q;
}
while(!stk.empty())printf("%d\n",stk.top()),stk.pop();
return 0;
}
//orz各位巨犇
cogs1885 [WC2006]水管局长数据加强版的更多相关文章
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- BZOJ_2594_[Wc2006]水管局长数据加强版_LCT
BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...
- [WC2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- 【刷题】BZOJ 2594 [Wc2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- 2594. [WC2006]水管局长数据加强版【LCT+最小生成树】
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
随机推荐
- 微信自定义菜单的emoji图标
微信公众号自定义菜单添加emoji表情图标 第一步:打开微信公众平台接口调试工具,点击前往接口调试工具: 第二步:把这段代码 {"button":[{"sub_butt ...
- Linux wc命令详解
wc常见命令参数 wc -l : 统计行 wc -c: 统计字节数 wc -m:统计字符数,不能与-c同时使用 wc -w:统计字数 wc -L:打印最长长度 注意: wc 可以直接后面跟文件使用,但 ...
- python---九九乘法表代码
#_*_ coding:utf-8 _*_# author choco ''' #while循环num1=0while num1<9: num1+=1 num2=1 while num2< ...
- 个人技术博客二之apk反编译与加密
根据原文郭霖大神的博客Android安全攻防战,反编译与混淆技术完全解析 本人亲测反编译真的没有什么卵用,个人纯属好奇就去搜了一下,偷窃有罪,抄袭可耻. 1.手机上的apk都是打包好的,直接安装使用. ...
- 函数式编程的类型系统:typeclass--Functor的解释--构造类型
函数式编程的类型系统:typeclass Typeclass是带有关联构造类型的抽象接口,抽象接口的行为用于约束构造类型. 构造类型实现了抽象接口的行为约束,就称这个实现为这个构造类型的函子. 要素: ...
- JavaScript组合继承的一点思考
今天看<JavaScript高级程序设计>一书中关于组合继承模式时.书上有这么一个Demo程序: <html> <head> </head> <b ...
- PHP eval() 函数
定义和用法 eval() 函数把字符串按照 PHP 代码来计算. 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串中调用 return 语句,则返回 NULL.如果代码中存 ...
- 【转】numpy.random.randn()与rand()的区别
转自: https://blog.csdn.net/u010758410/article/details/71799142 numpy中有一些常用的用来产生随机数的函数,randn()和rand()就 ...
- JDBC通过配置文件(properites)读取数据库配置信息
扫盲: Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中.与普通程序不同的是.Java程序(class文件)并不是本地的可执行程序.当运行Java程序时,首先运行JVM( ...
- USB耳机声卡-音频输入/输出控制器:DP108替代兼容CM108
DP108是一款完全替代CM108的高度集成的单芯片USB音频解决方案芯片.方便的USB即插即用的兼容性,用户可以快速创建易用性,高质量和便携式USB音频产品基于高度集成的单芯片解决方案.所有重要的模 ...