嘟嘟嘟




看题目这个架势,就知道要线段树,又看到维护联通块,那就得并查集。

所以,线段树维护并查集。




然而如果没想明白具体怎么写,就会gg的很惨……

首先都容易想到维护区间联通块个数和区间端点两列的点,然后就是区间合并了。

关键在于pushup,线段树是自底向上的,而并查集是自上而下的,因此,每到达一层,那么这一层的点就应该是每一个并查集的根节点,然后再考虑相邻的两个节点所在的联通块能否合并。


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const int maxN = 1e6 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
} int n, m, q, a[12][maxn]; int p[maxn * 10], cnt = 0;
In int Find(int x) {return x == p[x] ? x : p[x] = Find(p[x]);}
In bool merge(int x, int y)
{
int px = Find(x), py = Find(y);
if(px == py) return 0;
p[px] = py; return 1;
} struct Tree
{
int l, r, sum;
int L[12], R[12];
friend In Tree operator + (Tree A, Tree B)
{
Tree ret;
ret.l = A.l, ret.r = B.r;
ret.sum = A.sum + B.sum;
for(int i = 1; i <= n; ++i)
{
p[A.L[i]] = A.L[i], p[A.R[i]] = A.R[i];
p[B.L[i]] = B.L[i], p[B.R[i]] = B.R[i];
}
for(int i = 1; i <= n; ++i)
if(a[i][A.r] == a[i][B.l]) ret.sum -= merge(A.R[i], B.L[i]);
for(int i = 1; i <= n; ++i)
ret.L[i] = Find(A.L[i]), ret.R[i] = Find(B.R[i]);
return ret;
}
}t[maxn << 2];
In void build(int L, int R, int now)
{
t[now].l = L, t[now].r = R;
if(L == R)
{
for(int i = 1; i <= n; ++i)
if(a[i][L] == a[i - 1][L]) t[now].L[i] = t[now].R[i] = t[now].L[i - 1];
else t[now].L[i] = t[now].R[i] = ++cnt, ++t[now].sum;
return;
}
int mid = (L + R) >> 1;
build(L, mid, now << 1);
build(mid + 1, R, now << 1 | 1);
t[now] = t[now << 1] + t[now << 1 | 1];
}
In Tree query(int L, int R, int now)
{
if(t[now].l == L && t[now].r == R) return t[now];
int mid = (t[now].l + t[now].r) >> 1;
if(R <= mid) return query(L, R, now << 1);
else if(L > mid) return query(L, R, now << 1 | 1);
else return query(L, mid, now << 1) + query(mid + 1, R, now << 1 | 1);
} int main()
{
MYFILE();
n = read(), m = read(), q = read();
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) a[i][j] = read();
build(1, m, 1);
for(int i = 1; i <= q; ++i)
{
int L = read(), R = read();
write(query(L, R, 1).sum), enter;
}
return 0;
}

CF811E Vladik and Entertaining Flags的更多相关文章

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

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

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

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

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

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

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

  5. Vladik and Entertaining Flags

    Vladik and Entertaining Flags time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

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

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

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

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

  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. 深度剖析Kubernetes API Server三部曲 - part 1

    欢迎来到深入学习Kubernetes API Server的系列文章,在本系列文章中我们将深入的探究Kubernetes API Server的相关实现.如果你对Kubernetes 的内部实现机制比 ...

  2. Java Web 深入分析(7) Jetty原理解析

    1Jetty的基本架构 Jetty有一个基本的数据模型,这个模式就是handle,所有拷贝拓展的组件都被当做一个handler被添加到server中,然后由jetty统一管理. 1.1Jetty基本架 ...

  3. Ubuntu 14.04 64位机上不带CUDA支持的Caffe

    Caffe是一个高效的深度学习框架.它既可以在CPU上执行也可以在GPU上执行. 下面介绍在Ubuntu上不带CUDA的Caffe配置编译过程: 1.      安装BLAS:$ sudo apt-g ...

  4. POJ1083(Moving Tables)--简单模拟

    题目链接:http://poj.org/problem?id=1083 如图所示在一条走廊的两侧各有200个房间,现在给定一些成对的房间相互交换桌子,但是走廊每次只能通过一组搬运, 也就是说如果两个搬 ...

  5. mac使用sourcetree跳过注册

    转自https://blog.csdn.net/qq_32890891/article/details/89216954 打开sourcetree 关闭sourcetree 命令终端输入default ...

  6. EtherNet/IP 协议应用层使用CIP协议&CIP协议中使用的TLS和DTLS(Network Infrastructure for EtherNet/IPTM: Introduction and Considerations)

  7. 使用vs编写arduino项目

    说实话,arduino官方自带的编辑器有时候用的真不爽.所以直接使用vs开发arduino项目,用起来真爽,一直使用一直爽. 不多废话,直接上图,三部曲结束,搞定,收工. 我用的是vs2015版本的. ...

  8. ORACLE归档日志满了之后,如何删除归档日志

    当ORACLE归档日志满后如何正确删除归档日志 版权声明:本文为博主原创文章,未经博主允许不得转载. 当ORACLE 归档日志满了后,将无法正常登入ORACLE,需要删除一部分归档日志才能正常登入OR ...

  9. 为什么要用BigDecimal

    一般货币计算的时候都要用到BigDecimal类,为什么一般不适用float或者double呢? 先看一下浮点数的二进制表示: 小数 0.125 0.125 * 2 = 0.25 0 0.25 * 2 ...

  10. 什么是OAuth授权

    什么是OAuth授权?   一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...