Vladik and Entertaining Flags
2 seconds
256 megabytes
standard input
standard output
In his spare time Vladik estimates beauty of the flags.
Every flag could be represented as the matrix n × m which consists of positive integers.
Let's define the beauty of the flag as number of components in its matrix. We call component a set of cells with same numbers and between any pair of cells from that set there exists a path through adjacent cells from same component. Here is the example of the partitioning some flag matrix into components:
But this time he decided to change something in the process. Now he wants to estimate not the entire flag, but some segment. Segment of flag can be described as a submatrix of the flag matrix with opposite corners at (1, l) and (n, r), where conditions 1 ≤ l ≤ r ≤ m are satisfied.
Help Vladik to calculate the beauty for some segments of the given flag.
First line contains three space-separated integers n, m, q (1 ≤ n ≤ 10, 1 ≤ m, q ≤ 105) — dimensions of flag matrix and number of segments respectively.
Each of next n lines contains m space-separated integers — description of flag matrix. All elements of flag matrix is positive integers not exceeding 106.
Each of next q lines contains two space-separated integers l, r (1 ≤ l ≤ r ≤ m) — borders of segment which beauty Vladik wants to know.
For each segment print the result on the corresponding line.
4 5 4
1 1 1 1 1
1 2 2 3 3
1 1 1 2 5
4 4 5 5 5
1 5
2 5
1 2
4 5
6
7
3
4
Partitioning on components for every segment from first test case:
分析:给一个10*n矩阵,q次询问l到r内联通块个数;
用线段树维护区间,每个节点维护左右两边即可,合并区间时使用”并查集“实现;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls rt<<1
#define rs rt<<1|1
const int maxn=1e5+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,a[][maxn],fa[],id[];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int Union(int x,int y)
{
x=find(x),y=find(y);
if(x==y)return ;
return fa[x]=y,;
}
struct node
{
int s[];
int cnt;
}s[maxn<<];
void pup(node &s,node l,node r,int pos)
{
s.cnt=l.cnt+r.cnt;
for(int i=;i<=*n;i++)fa[i]=i,id[i]=;
for(int i=;i<=n;i++)
{
if(a[i][pos]==a[i][pos+])s.cnt-=Union(l.s[i+n],r.s[i]+*n);
}
int cnt=;
for(int i=;i<=n;i++)
{
int &x=id[find(l.s[i])];
if(!x)x=++cnt;
s.s[i]=x;
int &y=id[find(r.s[i+n]+*n)];
if(!y)y=++cnt;
s.s[i+n]=y;
}
return ;
}
void build(int l,int r,int rt)
{
if(l==r)
{
s[rt].cnt=;
for(int i=;i<=n;i++)
{
if(a[i][l]!=a[i-][l])
{
s[rt].cnt++;
}
s[rt].s[i]=s[rt].s[i+n]=s[rt].cnt;
}
return ;
}
int mid=l+r>>;
build(l,mid,ls);
build(mid+,r,rs);
pup(s[rt],s[ls],s[rs],mid);
}
node gao(int L,int R,int l,int r,int rt)
{
if(L==l&&R==r)return s[rt];
int mid=l+r>>;
if(R<=mid)return gao(L,R,l,mid,ls);
else if(L>mid)return gao(L,R,mid+,r,rs);
else
{
node x=gao(L,mid,l,mid,ls);
node y=gao(mid+,R,mid+,r,rs);
node ret;
pup(ret,x,y,mid);
return ret;
}
}
int main()
{
int i,j;
int q;
scanf("%d%d%d",&n,&m,&q);
rep(i,,n)rep(j,,m)scanf("%d",&a[i][j]);
build(,m,);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",gao(l,r,,m,).cnt);
}
return ;
}
Vladik and Entertaining Flags的更多相关文章
- codeforces 811E Vladik and Entertaining Flags(线段树+并查集)
codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...
- 【Codeforces811E】Vladik and Entertaining Flags [线段树][并查集]
Vladik and Entertaining Flags Time Limit: 20 Sec Memory Limit: 512 MB Description n * m的矩形,每个格子上有一个 ...
- 2022.02.27 CF811E Vladik and Entertaining Flags
2022.02.27 CF811E Vladik and Entertaining Flags https://www.luogu.com.cn/problem/CF811E Step 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 ...
- Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)
用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...
- codeforces 811 E. Vladik and Entertaining Flags(线段树+并查集)
题目链接:http://codeforces.com/contest/811/problem/E 题意:给定一个行数为10 列数10w的矩阵,每个方块是一个整数, 给定l和r 求范围内的联通块数量 所 ...
- CF811E Vladik and Entertaining Flags
嘟嘟嘟 看题目这个架势,就知道要线段树,又看到维护联通块,那就得并查集. 所以,线段树维护并查集. 然而如果没想明白具体怎么写,就会gg的很惨-- 首先都容易想到维护区间联通块个数和区间端点两列的点, ...
- codeforces 416div.2
A CodeForces 811A Vladik and Courtesy B CodeForces 811B Vladik and Complicated Book C CodeFo ...
- Codeforces Round#416 Div.2
A. Vladik and Courtesy 题面 At regular competition Vladik and Valera won a and b candies respectively. ...
随机推荐
- VS2010 使用TeeChart绘图控件 - 之二 - 绘制图形(折线图,柱状图)
1.前期准备 具体可见VS2010 使用TeeChart绘图控件 - 之一 控件和类的导入 1. 1 添加TeeChart控件,给控件添加变量m_TeeChart 添加TeeChart控件,右击控件, ...
- Request returned failure status 401
Request returned failure status 401.Invalid OpenStack Identity credentials.
- MSP430:输入捕获
在做超声模块时用到 //捕获上升沿 void Capture_Pos(void) { P2SEL |= Echo; //选择P23作为捕捉的输入端子 Timer1_A //TA1CCTL1 |=CM_ ...
- Spark中统计程序运行时间
import java.text.SimpleDateFormat import java.util.Date val s=NowDate() //显示当前的具体时间 val now=new Date ...
- [转]Linux命令之iconv
转自:http://lorna8023.blog.51cto.com/777608/420313 用途说明 iconv命令是用来转换文件的编码方式的(Convert encoding of given ...
- Mybatis的Dao向mapper传多个参数(三种解决方案)
第一种方案 : DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id=" ...
- C#学习-图片的处理
1.在图片上加防伪标记 private void btnAddString_Click(object sender, EventArgs e) { //以流的方式,获取一张图片 using (File ...
- Hive扩展功能(五)--HiveServer2服务高可用
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- 显示log里的ansi codecs颜色字符
方法: vim AnsiEsc插件 http://www.vim.org/scripts/script.php?script_id=302 less -r cat和tail命令都可以正常显示,而且ta ...
- Django中图片的上传并显示
一.settings配置文件中配置 MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'medias').replace('\\', ...