BZOJ1453: [WC2005]Dface双面棋盘
离线LCT维护MST,和3082的方法一样。然而比较码农,适合颓废的时候写。
PS:线段树分治要好写得多,LCT比较自娱自乐。
#include<bits/stdc++.h>
using namespace std;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
const int inf=1e9;
int n;
int cal1(int x1,int y1,int x2,int y2){
int i=(min(x1,x2)*n+min(y1,y2))*2;
return x1==x2?i:i+1;
}
struct node;
typedef node*ptr;
struct vec{
int v;ptr i,s,t;
vec(){v=inf;}
};
bool operator<(vec a,vec b){
return a.v<b.v;
}
struct node{
ptr i,j,p;bool r;vec v,s;
node();
ptr up(){
s=min(min(v,i->s),j->s);
return this;
}
void down(){
if(r)
i->r^=1,j->r^=1,swap(i,j),r=0;
}
}e[120000];
node::node(){i=j=p=e;}
bool root(ptr o){
return o!=o->p->i&&o!=o->p->j;
}
void turn(ptr o){
ptr s=o->p,t=s->p;
if(!root(s))(s==t->i?t->i:t->j)=o;
o->p=t,s->p=o;
if(o==s->i)
s->i=o->j,o->j->p=s,o->j=s->up();
else
s->j=o->i,o->i->p=s,o->i=s->up();
}
void push(ptr o){
if(!root(o))push(o->p);
o->down();
}
ptr splay(ptr o){
push(o);
while(!root(o)){
if(!root(o->p))turn(o==o->p->i^o->p==o->p->p->i?o:o->p);
turn(o);
}
return o->up();
}
ptr exp(ptr o){
ptr s=e;
while(o!=e)splay(o)->j=s,s=o->up(),o=o->p;
return s;
}
ptr bel(ptr o){
o=exp(o);
while(o->i!=e)o=o->i,o->down();
return o;
}
bool jud(ptr s,ptr t){
exp(s)->r=1;
return bel(t)==s;
}
void link(ptr s,ptr t){
exp(s)->r=1,splay(s)->p=t;
}
bool cut(ptr s,ptr t){
exp(s)->r=1,exp(t);
splay(t);
if(s==t->i&&s->j==e)
return t->i=s->p=e;
return 0;
}
ptr cal2(int i){
return e+n*n+i+1;
}
bool link(int x1,int y1,int x2,int y2){
ptr s=e+x1*n+y1+1;
ptr t=e+x2*n+y2+1;
int i=cal1(x1,y1,x2,y2);
if(!jud(s,t)){
link(s,cal2(i));
link(t,cal2(i));
return 1;
}
exp(s)->r=1;
vec v=exp(t)->s;
if(v<cal2(i)->v){
cut(v.s,v.i);
cut(v.t,v.i);
link(s,cal2(i));
link(t,cal2(i));
}
return 0;
}
bool cut(int x1,int y1,int x2,int y2){
ptr s=e+x1*n+y1+1;
ptr t=e+x2*n+y2+1;
int i=cal1(x1,y1,x2,y2);
if(cut(s,cal2(i)))
return cut(t,cal2(i));
return 0;
}
int m,t,c6[2],c1[200][200];
queue<int>c3[80000];
void pre(int x1,int y1,int x2,int y2){
if(!~x1||x1==n||!~y1||y1==n||!~x2||x2==n||!~y2||y2==n)
return;
int i=cal1(x1,y1,x2,y2);
if(c1[x1][y1]==c1[x2][y2])
c3[i].push(t);
}
void ins(int x1,int y1,int x2,int y2){
if(!~x1||x1==n||!~y1||y1==n||!~x2||x2==n||!~y2||y2==n)
return;
int i=cal1(x1,y1,x2,y2);
if(c1[x1][y1]==c1[x2][y2]){
cal2(i)->v.i=cal2(i);
cal2(i)->v.s=e+x1*n+y1+1;
cal2(i)->v.t=e+x2*n+y2+1;
if(c3[i].empty())
cal2(i)->v.v=inf;
else{
cal2(i)->v.v=c3[i].front();
c3[i].pop();
}
if(link(x1,y1,x2,y2))
--c6[c1[x1][y1]];
}
}
void del(int x1,int y1,int x2,int y2){
if(0<=x1&&x1<n&&0<=y1&&y1<n&&0<=x2&&x2<n&&0<=y2&&y2<n&&c1[x1][y1]==c1[x2][y2]&&cut(x1,y1,x2,y2))
++c6[c1[x1][y1]];
}
struct que{int x,y;};
que c2[10000];
int main(){
struct{
operator int(){
int x=0,c=getchar();
while(c<48)c=getchar();
while(c>32)
x=x*10+c-48,c=getchar();
return x;
}
}it;
n=it;
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
++c6[c1[i][j]=it];
m=it;
for(int&i=t=0;i<m;++i){
int x=it-1,y=it-1;
c2[i].x=x,c2[i].y=y;
for(int j=0;j<4;++j)
pre(x,y,x+dx[j],y+dy[j]);
c1[x][y]^=1;
}
for(int i=m-1;~i;--i)
c1[c2[i].x][c2[i].y]^=1;
for(int i=0;i<n;++i)
for(int j=0;j<n;++j){
ins(i,j,i,j+1);
ins(i,j,i+1,j);
}
for(int i=0;i<m;++i){
int x=c2[i].x,y=c2[i].y;
for(int j=0;j<4;++j)
del(x,y,x+dx[j],y+dy[j]);
--c6[c1[x][y]];
++c6[c1[x][y]^=1];
for(int j=0;j<4;++j)
ins(x,y,x+dx[j],y+dy[j]);
printf("%d %d\n",c6[1],c6[0]);
}
}
BZOJ1453: [WC2005]Dface双面棋盘的更多相关文章
- BZOJ1453: [Wc]Dface双面棋盘
Description Input Output Sample Input Sample Output HINT 线段树套并查集应该是比较好写的做法,时间复杂度为O(N^3+M*NlogN). #in ...
- 【BZOJ1453】[Wc]Dface双面棋盘 线段树+并查集
[BZOJ1453][Wc]Dface双面棋盘 Description Input Output Sample Input Sample Output HINT 题解:话说看到题的第一反应其实是LCT ...
- bzoj 1453: [Wc]Dface双面棋盘
1453: [Wc]Dface双面棋盘 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 617 Solved: 317[Submit][Status][ ...
- 【BZOJ1453】[WC] Dface双面棋盘(LCT维护联通块个数)
点此看题面 大致题意: 给你一个\(n*n\)的黑白棋盘,每次将一个格子翻转,分别求黑色连通块和白色连通块的个数. \(LCT\)动态维护图连通性 关于这一部分内容,可以参考这道例题:[BZOJ402 ...
- BZOJ1453:[WC]Dface双面棋盘
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...
- [BZOJ1453]Dface双面棋盘
Description Input Output Sample Input Sample Output HINT 线段树+并查集,暴力记录和更新一些信息,详情见代码注解. #include<cm ...
- [Wc]Dface双面棋盘()
题解: 一道维护奇怪信息的线段树... 我刚开始看了标签想的是删去图上一个点后求连通性 发现不会 于是退化成一般图支持删除 插入 维护连通性 发现有2两种做法 1.lct维护 按照结束顺序先后排序,给 ...
- 【刷题】BZOJ 1453 [Wc]Dface双面棋盘
Description Input Output Sample Input Sample Output HINT Solution 不强制在线的动态图问题,那就LCT了 类似二分图那道题目 对于四个方 ...
- P4121 [WC2005]双面棋盘
题目 P4121 [WC2005]双面棋盘 貌似是刘汝佳出的题目?? 做法 线段树维护并查集 线段树分治\(1\)~\(n\)行,我们要考虑维护的肯定是黑.白各自的联通块数量 考虑区间合并,其实就与中 ...
随机推荐
- P1111 修复公路 洛谷
https://www.luogu.org/problem/show?pid=1111 题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地 ...
- Java对象的死亡
在堆里面存放着Java世界中几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还“存活”着,哪些已经“死去”(即不可能再被任何途径使用的对象). 一,引用计数算法 给 ...
- Jetty插件实现热部署(开发时修改文件自动重启Jetty)
在pom.xml文件中配置Jetty插件的参数:scanIntervalSeconds <plugin> <groupId>org.mortbay.jetty</grou ...
- excel转换html
利用POI解析excel,转换成html,支持各种版本的excel.支持自定义样式.支持行列合并 需要用到的jar public class Excel2Html { /** * 读取Excel并转换 ...
- PPAPI+Skia实现的涂鸦板
在PPAPI插件中使用Skia画图介绍了怎样在PPAPI中使用Skia,文末说回头要提供一个简单的涂鸦板插件,这次我来兑现承诺了. foruok原创,关注微信订阅号"程序视界"可联 ...
- UITabBarController ---- 标签视图控制器
直接上代码: // // AppDelegate.m // // #import "AppDelegate.h" #import "RootViewController. ...
- HTML5已定稿:将彻底颠覆原生应用
2007年W3C(万维网联盟)立项HTML5,直至2014年10月底.这个长达八年的规范最终正式封稿. 过去这些年.HTML5颠覆了PC互联网的格局,优化了移动互联网的体验,接下来.HTML5将颠覆原 ...
- 分析Cocos2d-x横版ACT手游源码 1、公共
直接上代码 不说什么 这一款源码 凝视及多 PublicDef.h 公共头文件 #define NF_PLATFORM 1 //当前版本号(默觉得普通版) //版本号列表 #define NF_PLA ...
- SQL 通配符及其使用
Sql Server中通配符的使用 通配符_ "_"号表示任意单个字符,该符号只能匹配一个字符."_"可以放在查询条件的任意位置,且只能代表一个字符.一个汉字只 ...
- delete from inner join
Update Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,这个在表间关联来做更新和删除操作 ...