CF811E Vladik and Entertaining Flags
嘟嘟嘟
看题目这个架势,就知道要线段树,又看到维护联通块,那就得并查集。
所以,线段树维护并查集。
然而如果没想明白具体怎么写,就会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的更多相关文章
- 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 ...
- 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的矩形,每个格子上有一个 ...
- Vladik and Entertaining Flags
Vladik and Entertaining Flags time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 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 求范围内的联通块数量 所 ...
- 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. ...
随机推荐
- springboot项目在IDEA根据不同的开发人员读取不同的配置文件
IDEA启动项目打开项目的配置文件,修改Program argument为--spring.profiles.active=developerName启动项目,即可 命令行方式启动项目 java -j ...
- 题解-PKUWC2018 猎人杀
Problem loj2541 题意概要:给定 \(n\) 个人的倒霉度 \(\{w_i\}\),每回合会有一个人死亡,每个人这回合死亡的概率为 自己的倒霉度/目前所有存活玩家的倒霉度之和,求第 \( ...
- 此项目与Visual Studio的当前版本不兼容的报错
问题再现:程序是用visual studio 2013开发的,放在本地运行报此项目与Visual Studio的当前版本不兼容.本地是visual studio 2010. 解决办法: <1&g ...
- C# Entity Framework The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
The ObjectContext instance has been disposed and can no longer be used for operations that require a ...
- Visual Studio 开发大量 JavaScript 代码项目程序崩溃的解决方案
最近公司做新项目,基于 Bootstrap.AngularJS 和 kendo 开发一套后台的管理系统,在项目中使用了大量的 JavaScript 文件,这两天 Visual Studio 2015 ...
- SQL查询月、天、周、年(MySql的实例对比)
SQL Server实现 日期部分 缩写 year yy, yyyy quarter qq, q month mm, m dayofyear dy, y day dd, d week wk, ww w ...
- mac OS下 安装MySQL 5.7
Mac OS X 下 TAR.GZ 方式安装 MySQL 5.7 与 MySQL 5.6 相比, 5.7 版本在安装时有两处不同: 1:初始化方式改变, 从scripts/mysql_install_ ...
- Css制作table细线表格
制作细线表格,我想应该是最基本的css知识了,记录下来巩固下. 推荐: table{ border-collapse:collapse; border: 1px solid #000000; } td ...
- Visual Studio快捷键使用
1. 注释相关 添加注释:Ctrl + K,C 取消注释:Ctrl + K,U 2. 格式化相关 格式化代码:Ctrl + K,D 3. 智能提示相关 Ctrl + J
- ASE —— 第二次结对作业
目录 重现基线模型 基线模型原理 模型的优缺点 模型重现结果 提出改进 改进动机 新模型框架 评价合作伙伴 重现基线模型 基线模型原理 我们选用的的模型为DeepCS,接下来我将解释一下它的原理. 我 ...