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. ...
随机推荐
- SQL使用IN参量不能超过1000的表现形式以及解决办法
如果出现这个错误说明你传的参量是超过了一千个值:列如,你拼接了1001个id: 如何解决那,我这里提供两种方法: 1.每1000条加一个or in 列: 原:select p.* from t_pre ...
- explain 详解 (转)
原文:http://blog.csdn.net/zhuxineli/article/details/14455029 explain显示了MySQL如何使用索引来处理select语句以及连接表.可以帮 ...
- Redis学习和应用记录(2)--常用数据类型及命令
这一节主要介绍Redis支持的数据结构及常用命令. 数据类型 Redis支持多种数据类型的存储,包括字符,列表,集合,有续集合,哈希表,bit数组,超级日志等.下面分别介绍: strings:存储普通 ...
- jquery中对于为一组标签赋予点击事件
可以用each,但是each不能对动态的元素进行事件的绑定, 不过,其实也很简单,只需要获取所有的标签集,然后用动态绑定的方法,比如live进行绑定就可以了. 有时候,其实不难,只是自己想的太过复杂. ...
- 题解报告:hdu 1846 Brave Game(巴什博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1846 Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片, ...
- 3CSS基本语法
------------------------- --------------------------------------- -------------------------------- & ...
- Hadoop Hive概念学习系列之hive里的视图(十二)
不多说,直接上干货! 可以先,从MySQL里的视图概念理解入手 视图是由从数据库的基本表中选取出来的数据组成的逻辑窗口,与基本表不同,它是一个虚表.在数据库中,存放的只是视图的定义,而不存放视图包含的 ...
- [ Nowcoder Contest 165 #D ] 合法括号序列
\(\\\) \(Description\) 键盘上有三个键,敲击效果分别是: 在输出序列尾部添加一个左括号 在输出序列尾部添加一个右括号 删除输出序列尾部的第一个元素,若输出序列为空,则什么都不发生 ...
- fcc 响应式框架Bootstrap 练习1
需要通过添加下列代码到你的HTML开头来将Bootstrap添加到任意应用中: <link rel="stylesheet" href="//cdn.bootcss ...
- 来自一个用户的体验-Alpha项目测试
软件梦之队成员:201731062305 周蓉 这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 <软件梦之队>(附上团队博客 ...