嘟嘟嘟




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

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




然而如果没想明白具体怎么写,就会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. mysql存储emoji表情报错的处理方法【更改编码为utf8mb4】

    utf-8编码可能2个字节.3个字节.4个字节的字符,但是MySQL的utf8编码只支持3字节的数据,而移动端的表情数据是4个字节的字符.如果直接往采用utf-8编码的数据库中插入表情数据,Java程 ...

  2. 网络知识(1)TCP/IP五层结构

    图1 数据流向图 1,网络基础 1.1 发展 古代:①烽火狼烟最为原始的0-1单bit信息传递:②飞鸽传书.驰道快马通信,多字节通信: 近代:①轮船信号灯:②无线电报[摩尔斯码]: 现代:①有线模拟通 ...

  3. SQL Server系统函数:类型转换函数

    原文:SQL Server系统函数:类型转换函数 1.基本的转化 SELECT CAST(2008 as varchar(4)) + ' year!' SELECT CONVERT(varchar(4 ...

  4. C# 高低位获取

    ushort Tbed = 2255; byte gao = (byte)(Tbed >> 8); byte di = (byte)(Tbed & 0xff); ushort a ...

  5. wstngfw中配置snort

    wstngfw中配置snort 概述 Snort是入侵检测和预防系统.它可以将检测到的网络事件记录到日志并阻止它们.Snort使用称为规则的检测签名进行操作. Snort规则可以由用户自定义创建,或者 ...

  6. 2 vue学习

    1 vue的核心是数据与视图的双向绑定 2 当viewmodel销毁时,所有的事件处理器都会自动删除,无需自己清理 3 v-model的修饰符解释 .lazy :失去焦点或者按回车键时触发同步 .nu ...

  7. Android 主Module引用依赖Module,却无法使用里面的依赖库

    如果模块化开发中遇到 多模块的AndroidManifest.xml没有合并or多模块的资源文件没有合并or模块A include了模块B,而无法使用模块B内依赖的其他aar包中的类的时候or提示Su ...

  8. Android 音频播放速率调整实现

    最近接触到的一个项目, 有音频播放.切换播放速率和拖动进度到某处播放的需求 ,由于之前只是见过并没有尝试过切换播放速率 , 于是开始调研并最终实现,下面简单记录一下这次的调研过程. MediaPlay ...

  9. 从ABAP Netweaver的SICF到SAP Kyma的Lambda Function

    ABAP Netweaver里的事务码SICF是Jerry做原型开发时非常喜欢使用的一个工具:但凡遇到需要把ABAP系统里的资源以服务的方式暴露出来的场景,Jerry都喜欢在SICF里创建一个服务节点 ...

  10. 目标检测 — two-stage检测

    目前主流的目标检测算法主要是基于深度学习模型,其可以分成两大类:two-stage检测算法:one-stage检测算法.本文主要介绍第一类检测算法,第二类在下一篇博文中介绍. 目标检测模型的主要性能指 ...