嘟嘟嘟




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

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




然而如果没想明白具体怎么写,就会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. redis mongodb持久化的方式

    目录 redis持久化方式(两种) RDB持久化 AOF持久化 两种持续化方式需要明确的问题 对比 MongoDB持久化方式 redis持久化方式(两种) RDB持久化 redis提供了RDB持久化的 ...

  2. adb链接时报错误10061解决方法

    首先打开开发者选项然后依次操作: 1. 将安卓设备usb连接到电脑 2. 设备链接到wifi 3. Ping设备ip,检查是否可通信 4. 在cmd依次输入以下命令: 5. adb usb 6. ad ...

  3. (十三)SpringBoot之Spring-Data-Jpa(二)CRUD实现以及添加自定义方法

    一.jpa中添加自定义方法 http://blog.csdn.net/qq_23660243/article/details/43194465 二.案例 1.3 引入jpa依赖 <depende ...

  4. Java Web 深入分析(5) Java ClassLoader 工作机制

    Classloader 有3个作用 将class加载到JVM中去 审查每个类由谁去加载,是一种父优先的等级加载 把Class字节码统一编译成JVM统一要求的对象格式 ClassLoader的等级加载机 ...

  5. Centos 在VM中设置静态ip

    cd /etc/sysconfig/network-scripts 然后代开第一个文件 一般是ifcfg-ens331)开始配置原来是这样的 修改/etc/sysconfig/network # Cr ...

  6. springboot启动流程(二)SpringApplication run方法核心逻辑

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html run方法逻辑 在上一篇文章中,我们看到SpringApplication的静态方法最终是去构 ...

  7. Vue路由嵌套

    Vue路由嵌套 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  8. 四、TreeSet

    HashSet 是无序的,如果要对集合实现排序,那么就需要使用TreeSet 让TreeSet 实现集合有序有两种方法 一.让元素自身具备比较排序功能,具备比较排序功能的元素只需要实现Comparab ...

  9. 【大数据】Clickhouse基础知识

    第1章 ClickHouse概述 1.1 什么是ClickHouse ClickHouse 是俄罗斯的Yandex于2016年开源的列式存储数据库(DBMS),主要用于在线分析处理查询(OLAP),能 ...

  10. redis集群安装2

      概要:本文主要介绍如何在Centos7中单机搭建redis集群三主三从,按照本文绝对可以实现该需求,至于先搭建单机版主要为了方便理解redis集群,为下一步开发或生产上redis集群做铺垫.同时本 ...