CF1303F - Number of Components

Solution

思路还是有点妙的。

容易想到并查集,但是并查集不容易维护删边,怎么办呢?

我们考虑拆贡献,把加边的贡献和删边的贡献拆开,分别维护
只加边就是四连通加边,算一下新增多少个连通块。
只删边的贡献可以从后往前做,变成加边,对答案的贡献就是新增连通块个数的相反数(因为删边就是加边的逆过程,贡献相反)。

并查集维护即可。

时间复杂度O(qlog⁡n)O(q\log n)O(qlogn)。

Code

#include <bits/stdc++.h>using namespace std;template<typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; }
template<typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; }#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se secondtypedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI; const lod eps = 1e-9;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 60;
const int mods = 1e9 + 7;
const int inv2 = (mods + 1) >> 1;
const int MAXN = 100005;
const int MAXM = 2000005;
const int INF = 0x3f3f3f3f; //1061109567
/*--------------------------------------------------------------------*/namespace FastIO{constexpr int SIZE = (1 << 21) + 1;int num = 0, f;char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf, *oT = obuf + SIZE - 1, c;#define gc() (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS ++)) : *iS ++)inline void flush() {fwrite(obuf, 1, oS - obuf, stdout);oS = obuf;}inline void putc(char c) {*oS ++ = c;if (oS == oT) flush();}inline void getc(char &c) {for (c = gc(); !isdigit(c) && c != EOF; c = gc());}inline void reads(char *st) {char c;int n = 0;getc(st[++ n]);for (c = gc(); isdigit(c) ; c = gc()) st[++ n] = c;st[n + 1] = '\0';}template<class I>inline void read(I &x) {for (f = 1, c = gc(); c < '0' || c > '9' ; c = gc()) if (c == '-') f = -1;for (x = 0; c >= '0' && c <= '9' ; c = gc()) x = (x << 3) + (x << 1) + (c & 15);x *= f;}template<class I>inline void print(I x) {if (x < 0) putc('-'), x = -x;if (!x) putc('0');while (x) que[++ num] = x % 10 + 48, x /= 10;while (num) putc(que[num --]);}struct Flusher_{~Flusher_(){flush();}} io_Flusher_;
}
using FastIO :: read;
using FastIO :: putc;
using FastIO :: reads;
using FastIO :: print;vector<PR> A[MAXM], _A[MAXM];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int f[MAXN], col[305][305], Ans[MAXM], _Ans[MAXM], n, m, Case;
int getid(int x, int y) { return (x - 1) * m + y; }
int find(int x) { return f[x] == x ? f[x] : f[x] = find(f[x]); }
void solve(vector<PR> &A, int opt) {for (auto v : A) f[v.fi] = v.fi;for (auto v : A) {int x = (v.fi - 1) / m + 1, y = (v.fi - 1) % m + 1, t = 1;col[x][y] = 1;for (int i = 0; i < 4 ; ++ i) {int _x = x + dx[i], _y = y + dy[i], p = getid(_x, _y);if (_x < 1 || _y < 1 || _x > n || _y > m || col[x][y] != col[_x][_y] || find(p) == find(v.fi)) continue; f[find(p)] = find(v.fi);-- t;}if (opt == 1) Ans[v.se] += t;else Ans[v.se] -= t;}   for (auto v : A) col[(v.fi - 1) / m + 1][(v.fi - 1) % m + 1] = 0;
}
signed main() {
#ifndef ONLINE_JUDGEfreopen("a.in", "r", stdin);
#endifread(n), read(m), read(Case);int MX = max(1000, 2000000 / n / m + 1);for (int i = 1; i <= n * m ; ++ i) A[0].PB(MP(i, 0));for (int i = 1; i <= Case ; ++ i) {int x, y, z;read(x), read(y), read(z);_A[col[x][y]].PB(MP(getid(x, y), i));col[x][y] = z;A[col[x][y]].PB(MP(getid(x, y), i));   }for (int i = 1; i <= n * m ; ++ i) _A[col[(i - 1) / m + 1][(i - 1) % m + 1]].PB(MP(i, Case + 1));for (int i = 0; i <= MX ; ++ i) reverse(_A[i].begin(), _A[i].end());for (int i = 0; i <= MX ; ++ i) solve(A[i], 1);for (int i = 0; i <= MX ; ++ i) solve(_A[i], -1);for (int i = 1; i <= Case ; ++ i) Ans[i] += Ans[i - 1];for (int i = 1; i <= Case; ++ i) print(Ans[i]), putc('\n');return 0;
}

CF1303F - Number of Components(并查集)相关推荐

  1. codeforces1303 F. Number of Components(并查集+添_正序、删_逆序)

    F. Number of Components 并查集,每次修改考虑的是这个修改带来的贡献,就是和相邻颜色的对比,如果不考虑先不考虑颜色覆盖,那么添加颜色首先会产生一个新的连通块,然后考虑合并,每合并 ...

  2. 并查集(Union-Find)算法介绍

    本文主要介绍解决动态连通性一类问题的一种算法,使用到了一种叫做并查集的数据结构,称为Union-Find. 更多的信息可以参考Algorithms 一书的Section 1.5,实际上本文也就是基于它 ...

  3. File Transfer(并查集)

    题目大意:将多个电脑通过网线连接起来,不断查询2台电脑之间是否连通. 问题来源:中国大学mooc 05-树8 File Transfer (25 分) We have a network of com ...

  4. codeforces 884E Binary Matrix 并查集,滚动数组

    E. Binary Matrix time limit per test 3 seconds memory limit per test 16 megabytes input standard inp ...

  5. HDU5923-Prediction-有继承味道的并查集

    目录 目录 思路: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 题意:传送门  原题目描述在最下面.  有一个n个节点m条边的无向图和一个m个节点的有根树(根为1).树上每 ...

  6. 05-树8 File Transfer(并查集)

    05-树8 File Transfer 分数 25 作者 陈越 单位 浙江大学 We have a network of computers and a list of bi-directional ...

  7. 暑期集训5:并查集 线段树 练习题B: HDU - 1213 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题B  --   HDU - 1213 How Many Tables Today is Ignatius' birthday. He invites ...

  8. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集

    C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...

  9. hdu 1213 How Many Tables ([kuangbin带你飞]专题五 并查集)

    点击打开链接 C - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

最新文章

  1. C++编程进阶6(public继承与组合、private继承、多重继承、处理模板基类内的名称、如何避免模板代码膨胀)
  2. 第十二天:规划成本管理,成本类型, 资产折旧;和 估算成本,估算成本知识点提示
  3. iphone/ipad图标尺寸
  4. 计算机文化基础在线作业答案,中国石油大学17年秋《计算机文化基础》第一次在线作业答案...
  5. xp计算机属性打不开,xp系统我的电脑右键属性打不开怎么办
  6. React开发(235):react可以这样返回dom
  7. C++---STL中迭代器失效的总结
  8. window服务器查看硬盘有几块,从多个远程Windows服务器获取磁盘空间信息
  9. 有关java中的集合List,set,Map 等
  10. StretchDIBits函数显示图片
  11. 【JavaWeb】SSH安装及验证
  12. vbs脚本巡检windows主机
  13. java开源bi_poli-java开源BI软件
  14. bat批处理复制指定目录及其子目录的指定后缀文件到指定文件夹
  15. ubuntu20磁盘新建分区与挂载
  16. php 微信代扣开发步骤,【微信支付】微信代扣开发者文档
  17. 研究100位同行,我总结了从0到5年的新媒体晋级宝典
  18. html标签验证步骤,html标签验证视频教程
  19. SQL中的DML、DDL、DCL分别是什么意思
  20. as 贪食蛇小游戏(一)

热门文章

  1. 性冷淡风的麻将,获红点奖!网友:没有烟火气了
  2. 从串行线程封闭到对象池、线程池
  3. python关联分析sklearn_Python3利用pandas,sklearn进行关联度分析以及预测的demo
  4. 仓库每天的账怎样做_新年第一站,济南:仓储匠人仓库问题解决与实战力培训...
  5. 二面京东,面试官直接问我JVM,我心里一阵暗爽~
  6. python json方法详解_python详解json模块
  7. java jdk win10安装_Java 安装 JDK WIN10
  8. pythonjson数据提取_python爬虫学习笔记(十)-数据提取之JsonPath的使用
  9. win10必须禁用的服务_【亲测】Win10系统如何彻底禁止自动更新 亲测有效的Win10关闭自动更新方法...
  10. android 设置folder类型,正确配置你的 Android 项目