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. ...
随机推荐
- 换npm yarn的源让install超时去死吧
安装npm install时,长时间停留在fetchMetadata: sill mapToRegistry uri http://registry.npmjs.org/whatwg-fetch处, ...
- IDEA Spark Streaming Flume数据源 --解决无法转化为实际输入数据,及中文乱码(Scala)
需要三步: 1.shell:往 1234 端口写数据 nc localhost 1234 2.shell: 启动flume服务 cd /usr/local2/flume/bin ./flume-ng ...
- PCB 利用nginx正向代理实现上网
在PCB行业中,为了保证服务器的安全性,服务器正常都是需要与外网断开的,如果想在服务器通过浏览器下载一点东西是不行.通常作法是在一台可以上网的电脑下载文件,接着放到网络盘上,再从网络盘拷贝到服务器上. ...
- [App Store Connect帮助]一、 App Store Connect 使用入门(1)App Store Connect 工作流程
您使用 App Store Connect 提交并管理您在 App Store 中销售的 App,使用 TestFlight 分发您 App 的 Beta 版本,接受法律协议,输入您的税务和银行业务信 ...
- 修路方案 Kruskal 之 次小生成树
次小生成树 : Kruskal 是先求出来 最小生成树 , 并且记录下来所用到的的边 , 然后再求每次都 去掉最小生成树中的一个边 , 这样求最小生成树 , 然后看能不能得到 和原来最小生成树一样的 ...
- BZOJ 4085 丧心病狂的毒瘤题目 线段树+矩乘
思路: 一眼矩阵快速幂 再用线段树维护一下矩阵就完了... 我hhhhh 哎我还是too young,too simple 入了这个大坑 线段树维护9个值 以上 如果A+1 转移矩阵是这个样 ...
- day03_12/13/2016_bean属性的设置之构造器方式注入
- ibatis知识点
1:ibatis是apache的一个开源的项目,是一个O/R mapping解决方案,优点,小巧,灵活.2:搭建环境:导入ibatis相关jar包,jdbc驱动包等3:配置文件: jdbc连接的属性文 ...
- 常用的SSH注解标签
常用的SSH注解标签 1.Spring的注解 关于配Bean用的 @Component @Controller @Service @Repository 作用 ...
- [Windows Server 2012] Filezilla安装方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:FileZ ...