2022.02.27 CF811E Vladik and Entertaining Flags

https://www.luogu.com.cn/problem/CF811E

Step 1 题意

在一个 n*m 的网格上每个格子都有颜色,q 次询问,每次询问只保留 l 至 r 列时有多少个四连通的颜色块。两个格子同色但不连通算在不同的颜色块内。

Step 2 分析

这道题我首先大力找到一个错误规律,这个暂且不说,直接上正解。

对于每一列的格子搞线段树,记录每列有几个连通块,每列的最左侧和最右侧的节点属于哪个连通块。

合并的时候合并两个连通块相邻的两列,如果颜色一致并且fa不一样,总连通块的数量减一。不过需要初始化一下相邻两列格子的并查集。

Step 3 代码如下

#include<bits/stdc++.h>
using namespace std; const int N=1e5+10;
int n,m,q,mapi[15][N],fa[N*15];
int tot;
struct node{
int x,y,L[15],R[15],sum;
}t[N<<4]; inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0'){
s=s*10+ch-'0';
ch=getchar();
}
return s*w;
}
inline int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
inline node update(node x,node y,int l,int r){
node ans;
ans.sum=x.sum+y.sum;
for(int i=1;i<=n;i++)
fa[x.L[i]]=x.L[i],fa[x.R[i]]=x.R[i],
fa[y.L[i]]=y.L[i],fa[y.R[i]]=y.R[i];
for(int i=1;i<=n;i++)ans.L[i]=x.L[i],ans.R[i]=y.R[i];
for(int i=1;i<=n;i++)
if(mapi[i][l]==mapi[i][r]){
int xi=find(fa[x.R[i]]);
int yi=find(fa[y.L[i]]);
if(xi!=yi)fa[xi]=yi,--ans.sum;
}
for(int i=1;i<=n;i++)
ans.L[i]=find(ans.L[i]),ans.R[i]=find(ans.R[i]);
return ans;
}
inline void build(int x,int l,int r){
if(l==r){
for(int i=1;i<=n;i++)
if(mapi[i][l]==mapi[i-1][l])
t[x].L[i]=t[x].R[i]=t[x].L[i-1];
else t[x].L[i]=t[x].R[i]=++tot,++t[x].sum;
return ;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
t[x]=update(t[x<<1],t[x<<1|1],mid,mid+1);
}
inline node query(int x,int l,int r,int L,int R){
if(l>=L&&r<=R)return t[x];
int mid=(l+r)>>1;
int flaga=0,flagb=0;
node a,b,ans;
if(L<=mid)a=query(x<<1,l,mid,L,R),flaga=1;
if(R>mid)b=query(x<<1|1,mid+1,r,L,R),flagb=1;
if(flaga&&!flagb)ans=a;
else if(flagb&&!flaga)ans=b;
else if(flaga&&flagb)ans=update(a,b,mid,mid+1);
return ans;
} signed main(){
n=read();m=read();q=read();
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)mapi[i][j]=read();
//cout<<"Case 1 "<<endl;
build(1,1,m);
//cout<<"Case 2 "<<endl;
for(int i=1;i<=q;i++){
int u,v;
u=read();v=read();
node fin=query(1,1,m,u,v);
cout<<fin.sum<<endl;
}
return 0;
}

2022.02.27 CF811E Vladik and Entertaining Flags的更多相关文章

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

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

  2. CF811E Vladik and Entertaining Flags

    嘟嘟嘟 看题目这个架势,就知道要线段树,又看到维护联通块,那就得并查集. 所以,线段树维护并查集. 然而如果没想明白具体怎么写,就会gg的很惨-- 首先都容易想到维护区间联通块个数和区间端点两列的点, ...

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

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

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

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

  5. Vladik and Entertaining Flags

    Vladik and Entertaining Flags time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)

    用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...

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

    题目链接:http://codeforces.com/contest/811/problem/E 题意:给定一个行数为10 列数10w的矩阵,每个方块是一个整数, 给定l和r 求范围内的联通块数量 所 ...

  8. ROS的安装-> rosdep init /update报错2022.02.24实测有效

    ROS的安装-> rosdep init /update报错2022.02.24实测有效   一. 解决rosdep_init问题 正常执行sudo rosdep init会报错,如下: ERR ...

  9. 2022.02.21 UB

    2022.02.21 UB 参考资料: https://zhuanlan.zhihu.com/p/141467895 https://blog.csdn.net/ghscarecrow/article ...

随机推荐

  1. FrameScan-GUI CMS漏洞扫描

    工具简介 FrameScan-GUI是一款python3和Pyqt编写的具有图形化界面的cms漏洞检测框架,是FrameScan的加强版.支持多种检测方式,支持大多数CMS,可以自定义CMS类型及自行 ...

  2. 选择Key-Value Store

    [IT168 专稿]在之前的文章中,给大家介绍了<Redis快速入门:Key-Value存储系统简介>,今天进一步给大家介绍为什么选择Key-Value Store.Key-Value S ...

  3. S120的基本定位功能

    转自:https://support.industry.siemens.com/cs/document/84136148/s120%E7%9A%84%E5%9F%BA%E6%9C%AC%E5%AE%9 ...

  4. 从文件下载视角来理解Web API

    一.问题源起 从Web From过来的人应该会比较熟悉以下下载文件的代码: [HttpPost] [Route("Download")] public void Download( ...

  5. position 的值absolute、fixed、relative和static的定位原点是什么

    position 的值absolute.fixed.relative和static的定位原点是什么 absolute 成绝对定位的元素,相对于值不为static的第一个父元素进行定位,也可以理解为离自 ...

  6. 哪些是重要的bean生命周期方法? 你能重载它们吗?

    有两个重要的bean 生命周期方法,第一个是setup , 它是在容器加载bean的时候被调用.第二个方法是 teardown  它是在容器卸载类的时候被调用. The bean 标签有两个重要的属性 ...

  7. java-設計模式-單例模式

    單例模式 一种创建型设计模式, 让你能够保证一个类只有一个实例, 并提供一个访问该实例的全局节点. 一个类只有一个实例,且该类能自行创建这个实例的一种模式. 簡單的對比就是: 例如,Windows 中 ...

  8. 面试问题之计算机网络:TCP滑动窗口

    滑动窗口协议是传输层进行流量控制的一种措施,接收方通过通知发送方自己的窗口大小,从而控制发送方的发送速度,从而达到防止发送方发送速度过快而导致自己被淹没的目的,并且滑动窗口分为接收窗口和发送窗口.TC ...

  9. Serial 与 Parallel GC 之间的不同之处?

    Serial 与 Parallel 在 GC 执行的时候都会引起 stop-the-world.它们之间主要 不同 serial 收集器是默认的复制收集器,执行 GC 的时候只有一个线程,而 para ...

  10. scrapy基于请求传参实现深度爬取

    请求传参实现深度爬取 请求传参: 实现深度爬取:爬取多个层级对应的页面数据 使用场景:爬取的数据没有在同一张页面中 在手动请求的时候传递item:yield scrapy.Request(url,ca ...