Vladik and Entertaining Flags
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

First line contains three space-separated integers nmq (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 lr (1 ≤ l ≤ r ≤ m) — borders of segment which beauty Vladik wants to know.

Output

For each segment print the result on the corresponding line.

Example
input
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
output
6
7
3
4
Note

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的更多相关文章

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

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

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

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

  3. 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 题意 在一 ...

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

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

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

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

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

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

  7. CF811E Vladik and Entertaining Flags

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

  8. codeforces 416div.2

        A CodeForces 811A Vladik and Courtesy   B CodeForces 811B Vladik and Complicated Book   C CodeFo ...

  9. Codeforces Round#416 Div.2

    A. Vladik and Courtesy 题面 At regular competition Vladik and Valera won a and b candies respectively. ...

随机推荐

  1. 【Hnoi2010】Bzoj2002 Bounce & Codevs2333 弹飞绵羊

    Position: http://www.lydsy.com/JudgeOnline/problem.php?id=3143 http://codevs.cn/problem/2333/ Descri ...

  2. [Codeplus 2017] 晨跑

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5105 [算法] 答案为三个数的最小公倍数 [代码] #include<bits ...

  3. setings.py配置文件详解

    BASE_DIR指的是项目的根目录.SECRET_KEY是安全码. # SECURITY WARNING: don't run with debug turned on in production! ...

  4. PCB genesis Slot槽转钻孔(不用G85命令)实现方法

    PCB钻Slot槽一般都采用G85命令钻槽孔,而采用G85命令工程CAM无法准确的知道Slot槽钻多少个孔,并不能决定钻槽孔的顺序,因为采用G85命令钻孔密度与钻槽顺序由钻机本身决定的.在这里介绍一种 ...

  5. python配置文件编写

    from configparser import ConfigParser # 配置类,专门来读取配置文件# 配置文件结尾:.ini .conf .config .properties .xml# 配 ...

  6. ubuntu16.04更改源

    最近用apt-get安装软件总是提示列表无法全部更新,导致一些软件安装不上,下面我们通过讲/etc/apt/sources.list里为阿里源,实现访问. 第一步: 备份/etc/apt/source ...

  7. Gson 转日期中的错误

    今天在用Gson做json转化是遇到一个问题,本地执行没有问题(windows 7),包丢到服务器上(Centos)就报错了. 后经分析发现DateTypeDapter类中取本地环境的日期格式参考ht ...

  8. Eclipse 添加 YAML插件

    官网:https://github.com/oyse/yedit 离线版本:(链接: https://pan.baidu.com/s/1PJzkS1tI-VigZvfbYXUh9A 密码: gfep) ...

  9. python自动化测试学习笔记-1

    一.什么是自动化 自动化测试是把以人为驱动的测试行为转化为机器执行的一种过程.直白的就是为了节省人力.时间或硬件资源,提高测试效率,便引入了通过软件或程序自动化执行测试用例进行测试: 二.python ...

  10. BZOJ 3779 LCT 线段树 DFS序 坑

    hhhh抄了半天lty代码最后T了  对拍也没事.. 药丸 mine #pragma GCC optimize("O3") //By SiriusRen #include < ...