离线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双面棋盘的更多相关文章

  1. BZOJ1453: [Wc]Dface双面棋盘

    Description Input Output Sample Input Sample Output HINT 线段树套并查集应该是比较好写的做法,时间复杂度为O(N^3+M*NlogN). #in ...

  2. 【BZOJ1453】[Wc]Dface双面棋盘 线段树+并查集

    [BZOJ1453][Wc]Dface双面棋盘 Description Input Output Sample Input Sample Output HINT 题解:话说看到题的第一反应其实是LCT ...

  3. bzoj 1453: [Wc]Dface双面棋盘

    1453: [Wc]Dface双面棋盘 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 617  Solved: 317[Submit][Status][ ...

  4. 【BZOJ1453】[WC] Dface双面棋盘(LCT维护联通块个数)

    点此看题面 大致题意: 给你一个\(n*n\)的黑白棋盘,每次将一个格子翻转,分别求黑色连通块和白色连通块的个数. \(LCT\)动态维护图连通性 关于这一部分内容,可以参考这道例题:[BZOJ402 ...

  5. BZOJ1453:[WC]Dface双面棋盘

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  6. [BZOJ1453]Dface双面棋盘

    Description Input Output Sample Input Sample Output HINT 线段树+并查集,暴力记录和更新一些信息,详情见代码注解. #include<cm ...

  7. [Wc]Dface双面棋盘()

    题解: 一道维护奇怪信息的线段树... 我刚开始看了标签想的是删去图上一个点后求连通性 发现不会 于是退化成一般图支持删除 插入 维护连通性 发现有2两种做法 1.lct维护 按照结束顺序先后排序,给 ...

  8. 【刷题】BZOJ 1453 [Wc]Dface双面棋盘

    Description Input Output Sample Input Sample Output HINT Solution 不强制在线的动态图问题,那就LCT了 类似二分图那道题目 对于四个方 ...

  9. P4121 [WC2005]双面棋盘

    题目 P4121 [WC2005]双面棋盘 貌似是刘汝佳出的题目?? 做法 线段树维护并查集 线段树分治\(1\)~\(n\)行,我们要考虑维护的肯定是黑.白各自的联通块数量 考虑区间合并,其实就与中 ...

随机推荐

  1. Mybatis详解

    SqlSession(SqlSessionDaoSupport类) SqlSessionDaoSupportSqlSessionDaoSupport是一个抽象的支持类,用来为你提供SqlSession ...

  2. Nginx阻止DDoS攻击的教程收集(转)(待实践)

    DDoS估计是一个非常头痛的问题. 分布式拒绝服务攻击(DDoS)指的是通过多台机器向一个服务或者网站发送大量看似合法的数据包使其网络阻塞.资源耗尽从而不能为正常用户提供正常服务的攻击手段.随着互联网 ...

  3. string 复制给char[] 即:c_str() 用法:

    语法: const char *c_str();c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过str ...

  4. java平台利用jsoup开发包,抓取优酷视频播放地址与图片地址等信息。

    /********************************************************************************************  * aut ...

  5. cocos2dx3.0 2048多功能版

    1.2048项目描写叙述 1.1.2048功能描写叙述 实现手机上2048的功能,同一时候具备能够删除随意一个方块的功能,悔棋功能,退出自己主动保存,启动自己主动载入功能. 1.2.2048所需技术 ...

  6. bzoj 1030: [JSOI2007]文本生成器 (ac自己主动机上的dp)

    1030: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 2635  Solved: 1090 [id=1030&qu ...

  7. Animated progress view with CAGradientLayer(带翻译)

    Animated progress view with CAGradientLayer(带翻译)  Modern software design is getting flatter and thin ...

  8. CodeVS2492 上帝造题的七分钟2(树状数组+并查集)

    传送门 树状数组模板题.注意优化,假设某个数的值已经是1了的话.那么我们以后就不用对他进行操作了,这个能够用并查集实现. 这道题还有个坑的地方,给出查询区间端点的a,b,有可能a>b. #inc ...

  9. Arcgis Engine(ae)接口详解(5):IGeometry几何高级操作

    IPoint point = new PointClass(); point.PutCoords(, ); //ITopologicalOperator接口用于几何对象的几何操作 ITopologic ...

  10. [网页游戏开发]Morn组件赋值

    在讲解List之前,我们先介绍一下Morn组件赋值功能 默认属性赋值 界面逻辑开发过程中,经常会涉及到动态更改UI属性,比如: 界面有一个按钮,一个多选框和一个文本,分别命名为myButton,myC ...