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. ...
随机推荐
- Quartz.net使用入门(三)
Windows服务,自定义安装,卸载服务+Quartz.net app.config配置文件 <?xml version="1.0"?> <configurati ...
- bzoj 1601 灌水
题目大意: 决定把水灌到n块农田,农田被数字1到n标记 把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库 建造一个水库需要花费wi,连接两块土地需要花费Pij. 计算所需的最少代价 ...
- PCB javascript解析Gerber274X格式实现方法
解析钻Gerber274X格式前首先得了解此格式,这样才能更好的解析呀. 一个Gerber274X里面包含的基本信息如下: 1.单位:公式mm,英制inch 2.省零方式:前省零,后省零 3.坐标方式 ...
- 【WIP】Bootstrap nav
创建: 2017/09/28 更新: 2017/10/14 标题加上[WIP]
- github 用户不被识别问题
期末考完,继续开发. 用过的都知道,直接用的话贡献者上面显示不出自己. 查一下就知道是因为github的识别是靠邮箱设置的. 但是如果频繁创建新仓库,容易忘记设定用户名和邮箱. 突发奇想,发现 ...
- Text段、Data段和BSS段
不同的compiler在编译的过程中对于存储的分配可能略有不同,但基本结构大致相同. 大体上可分为三段:Text段.Data段和BSS段. text段用于存放代码,通常情况下在内存中被映射为只读,但d ...
- shopnc学习
---恢复内容开始--- 以前没有怎么接触过shopnc,感觉界面挺漂亮的,不过后来自己需要开发一个电商系统,就顺便参考了下,感觉构架垃圾的一塌糊涂.不过平时做这个系统二次开发的业务比较多,所以简单的 ...
- BZOJ 5277 IQ题orz
思路: 首先我们注意到,对一个序列按分割点分开以后分别冒泡其实就相当于对整个序列进行冒泡.每一个元素都会对复杂度贡献1,除非一个元素两边的分割点都出现了.因此我们可以完全忽略快排的递归过程.只需考虑每 ...
- 【洛谷2926/BZOJ1607】[USACO08DEC]Patting Heads拍头(筛法)
题目: 洛谷2926 (截止至本博客发表时,BZOJ1607题面有误,正确题面请到洛谷2926查看) 分析: = 一句话题意:给定\(n\)个数\(\{a_i\}\),求对于每个\(a_i\)有多少个 ...
- Android 蓝牙开发基本流程
此例子基于 android demo 对于一般的软件开发人员来说,蓝牙是很少用到的,尤其是Android的蓝牙开发,国内的例子很少 Android对于蓝牙开发从2.0版本的sdk才开始支持,而 ...