Codeforces Round #416 (Div. 2) E. Vladik and Entertaining Flags (线段树+并查集)

 题目链接: 传送门
 题目描述:相邻两块颜色相同为同一块,问给定区间内有多少连通块
 题解:
 很容易想到用线段树做,然后用并查集实现连通块判定。
 build时对每一个块进行编号,并且同列相邻合并(给同一编号),然后考虑横向合并(at:此时两个竖向合并已完成),横向合并就简单啦:直接左右儿子编号对应的L,R丢并查集,左右两块交界处就是mid和mid+1,a[i][mid]==a[i][mid+1]时若并查集值不同,则sum--,合并连通块。
 查询操作:用rs维护当前考虑到的最右的编号的R。query先查到的肯定是[l,r]中的最左边的区间,直接加上答案,不然就逐一考虑交界处是否还有可合并的连通块,(和update操作类似)(还是用并查集维护)
看了大牛的题解模拟了好久才会的、、、还是太菜emmmm、遇到好题忍不住记录一波
具体见代码吧,
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define endl '\n'
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define INF 0x3f3f3f3f
#define sync ios::sync_with_stdio(0);
const int N=100005;
using namespace std;

int L[N*4][12],R[N*4][12],sum[N*4],cnt=0;
int fa[N*10],a[12][N],n,m,q;
int rrs[12],ans;
bool flag;
int find(int x)
{
	return x==fa[x]?x:fa[x]=find(fa[x]);
}
void update(int rt,int l,int r)
{
	int mid=(l+r)>>1;
	int ls=rt<<1,rs=rt<<1|1;
	sum[rt]=sum[ls]+sum[rs];
	for(int i=1;i<=n;i++)
	{
		fa[L[ls][i]]=L[ls][i];
		fa[L[rs][i]]=L[rs][i];
		fa[R[ls][i]]=R[ls][i];
		fa[R[rs][i]]=R[rs][i];
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i][mid]==a[i][mid+1])
		{
			int x=find(R[ls][i]),y=find(L[rs][i]);
			if(x!=y)
			{
				sum[rt]--;
				fa[y]=x;
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		L[rt][i]=find(L[ls][i]);
		R[rt][i]=find(R[rs][i]);
	}
}
void build(int rt,int l,int r)
{
	if(l==r)
	{
		for(int i=1;i<=n;i++)
		if(a[i][l]==a[i-1][l]) L[rt][i]=R[rt][i]=L[rt][i-1];
		else L[rt][i]=R[rt][i]=++cnt,sum[rt]++;
		return;
	}
	int mid=(l+r)>>1;
	build(lson);
	build(rson);
	update(rt,l,r);
}
void query(int rt,int l,int r,int ll,int rr)
{
	if(l>=ll&&r<=rr)
	{
		if(!flag)
		{
			flag=true;
			ans=sum[rt];
			for(int i=1;i<=n;i++) rrs[i]=R[rt][i];
		}
		else
		{
			ans+=sum[rt];
			for(int i=1;i<=n;i++)
			{
				fa[rrs[i]]=rrs[i];
				fa[L[rt][i]]=L[rt][i];
				fa[R[rt][i]]=R[rt][i];
			}
			for(int i=1;i<=n;i++)
			{
				if(a[i][l]==a[i][l-1])
				{
					int x=find(rrs[i]),y=find(L[rt][i]);
					if(x!=y)
					{
						ans--;
						fa[x]=y;
					}
				}
			}
			for(int i=1;i<=n;i++)
			rrs[i]=find(R[rt][i]);
		}
		return;
	}
	int mid=(l+r)>>1;
	if(ll<=mid) query(lson,ll,rr);
	if(rr>mid) query(rson,ll,rr);
}

int main()
{
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	scanf("%d",&a[i][j]);
	build(1,1,m);
	while(q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		flag=0;ans=0;
		query(1,1,m,l,r);
		printf("%d\n",ans);
	}
	return 0;
}

E. 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 ≤ 101 ≤ 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
Copy
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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值