线段树+并查集维护连通性。

好像 \(700ms\) 的时限把我的常数超级大的做法卡掉了, 必须要开 \(O_2\) 才行。

对于线段树的每一个结点都开左边的并查集,右边的并查集,然后合并。

\(Code\ Below:\)

#include <bits/stdc++.h>
#define lson (rt<<1)
#define rson (rt<<1|1)
using namespace std;
const int maxn=200+10;
int n,m,a[maxn][maxn],f[maxn<<2],tmp[maxn<<2],wsum[maxn<<2],bsum[maxn<<2],lfa[maxn<<2][maxn],rfa[maxn<<2][maxn]; inline int read(){
register int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return (f==1)?x:-x;
} int find(int x,int *f){
return (x==f[x])?x:f[x]=find(f[x],f);
} inline void pushup(int rt,int mid){
wsum[rt]=wsum[lson]+wsum[rson];
bsum[rt]=bsum[lson]+bsum[rson];
for(int i=1;i<=n;i++){
f[i]=lfa[lson][i];f[i+n]=rfa[lson][i];
f[i+2*n]=lfa[rson][i]+2*n;f[i+3*n]=rfa[rson][i]+2*n;
}
for(int i=1;i<=n;i++)
if(a[mid][i]==a[mid+1][i]){
if(find(i+n,f)!=find(i+2*n,f)){
f[find(i+n,f)]=f[find(i+2*n,f)];
if(a[mid][i]==0) wsum[rt]--;
else bsum[rt]--;
}
}
for(int i=1;i<=n;i++) tmp[find(i,f)]=i;
for(int i=3*n+1;i<=4*n;i++) tmp[find(i,f)]=i-2*n;
for(int i=1;i<=n;i++){
lfa[rt][i]=tmp[find(i,f)];
rfa[rt][i]=tmp[find(i+3*n,f)];
}
} void build(int l,int r,int rt){
if(l == r){
for(int i=1;i<=n;i++) lfa[rt][i]=i;
for(int i=1;i<=n;i++){
if(a[l][i]==0) wsum[rt]++;
else bsum[rt]++;
if(a[l][i-1]==a[l][i]){
lfa[rt][find(i-1,lfa[rt])]=i;
if(a[l][i]==0) wsum[rt]--;
else bsum[rt]--;
}
rfa[rt][i]=lfa[rt][i];
}
return ;
}
int mid=(l+r)>>1;
build(l,mid,lson);
build(mid+1,r,rson);
pushup(rt,mid);
} void modify(int x,int l,int r,int rt){
if(l == r){
wsum[rt]=bsum[rt]=0;
for(int i=1;i<=n;i++) lfa[rt][i]=i;
for(int i=1;i<=n;i++){
if(a[l][i]==0) wsum[rt]++;
else bsum[rt]++;
if(a[l][i-1]==a[l][i]){
lfa[rt][find(i-1,lfa[rt])]=i;
if(a[l][i]==0) wsum[rt]--;
else bsum[rt]--;
}
rfa[rt][i]=lfa[rt][i];
}
return ;
}
int mid=(l+r)>>1;
if(x <= mid) modify(x,l,mid,lson);
else modify(x,mid+1,r,rson);
pushup(rt,mid);
} int main()
{
n=read();
for(int i=1;i<=n;i++) a[i][0]=-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) a[i][j]=read();
build(1,n,1);
m=read();
int x,y;
while(m--){
x=read(),y=read();
a[x][y]^=1;modify(x,1,n,1);
printf("%d %d\n",bsum[1],wsum[1]);
}
return 0;
}

[WC2005]双面棋盘(线段树+并查集)的更多相关文章

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

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

  2. 2022.02.27 CF811E Vladik and Entertaining Flags(线段树+并查集)

    2022.02.27 CF811E Vladik and Entertaining Flags(线段树+并查集) https://www.luogu.com.cn/problem/CF811E Ste ...

  3. codeforces 811E Vladik and Entertaining Flags(线段树+并查集)

    codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...

  4. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  5. 【XSY2707】snow 线段树 并查集

    题目描述 有\(n\)个人和一条长度为\(t\)的线段,每个人还有一个工作范围(是一个区间).最开始整条线段都是白的.定义每个人的工作长度是这个人的工作范围中白色部分的长度(会随着线段改变而改变).每 ...

  6. bzoj 2054: 疯狂的馒头(线段树||并查集)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2054 线段树写法: 点的颜色只取决于最后一次染的颜色,所以我们可以倒着维护,如果当前区间之前 ...

  7. 【CF687D】Dividing Kingdom II 线段树+并查集

    [CF687D]Dividing Kingdom II 题意:给你一张n个点m条边的无向图,边有边权$w_i$.有q个询问,每次给出l r,问你:如果只保留编号在[l,r]中的边,你需要将所有点分成两 ...

  8. 【Codeforces811E】Vladik and Entertaining Flags [线段树][并查集]

    Vladik and Entertaining Flags Time Limit: 20 Sec  Memory Limit: 512 MB Description n * m的矩形,每个格子上有一个 ...

  9. 【BZOJ 4662】 4662: Snow (线段树+并查集)

    4662: Snow Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 136  Solved: 47 Description 2333年的某一天,临冬突 ...

随机推荐

  1. @Transactional注解使用心得

    配置基于注解的声明式事务: ...配置tx,aop的命名空间 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:a ...

  2. 使用adb shell 模拟点击事件

    针对问题[appium无法点击到某一内容,需要使用adb去执行点击事件] 需要命令: adb shell adb devices input [input可以实现的功能]: 输入文本信息:input ...

  3. idea创建spring boot+mybatis(oracle)+themeleaf项目

    1.新建项目 选择idea已经有的spring initializr next,然后填写项目命名,包名 然后next,选择所需要的依赖 然后一路next,finish,项目新建成功,然后可以删除下面的 ...

  4. 系统当前时间system.currenttimemillis与new Date().getTime() 区别

    system.currenttimemillis //取到毫秒数,并且执行效率高 new Date().getTime()没他精确

  5. (10)The secret to great opportunities? The person you haven't met yet

    https://www.ted.com/talks/tanya_menon_the_secret_to_great_opportunities_the_person_you_haven_t_met_y ...

  6. VB连接MYSQL数据的方法

    原文链接:http://hanbaohong.iteye.com/blog/704800 第一步:上网http://dev.mysql.com/downloads/connector/odbc/下载m ...

  7. ArcMap等值面

    先说一下题目,ArcMap中没有由栅格直接生成等值面的功能,但由栅格直接生成等值线的功能存在,可通过如下方式得到等值面: 1.提取等值线 由dem直接提取等值线:Spatial Analyst Too ...

  8. FastDFS分布文件系统Java客户端集成

    参考博客:http://blog.csdn.net/xyang81/article/details/52847311 官网Java客户端源代码: https://github.com/happyfis ...

  9. 通过pip安装套件

    pip3 install requests pip3 install BeautifulSoup4   还需要使用jupyter: pip3 install jupyter 打开jupyterbook ...

  10. JAVA所属公司与非盈利组织

    版权     现在是Oracle公司的   Apache     负责Java发展的,重要的非盈利组织,主要产品包括Struts.Tomcat