CF1140F - Extending Set of Points
题意:对于点集S,定义函数F(S)为对S不断扩展到不能扩展时S的点数。一次扩展定义为如果有一个平行于坐标轴的矩形的三个点在S中,则第四个点加入S。
动态在S中加点删点,每次操作完后求F(S)的值。
解:首先有个结论就是,把这些点用平行于坐标轴的线段连接起来,则E值为每个连通块的横坐标种数 * 纵坐标种数之和。
线段树分治 + 可回退化并查集,O(nlog2n)。
#include <bits/stdc++.h> typedef long long LL;
const int N = ; struct Node {
int x, y;
Node(int X, int Y) {
x = X;
y = Y;
}
}; std::map<int, int> mp[N];
int n, m;
LL ans[N];
std::vector<Node> v[N << ]; namespace ufs { struct opt {
int x, y;
opt(){}
opt(int X, int Y) {
x = X, y = Y;
}
}stk[N * ]; int top; int fa[N * ], siz[N * ], s1[N * ], s2[N * ], clock;
LL tot, stk2[N];
inline void init() {
for(int i = ; i < N; i++) {
fa[i] = i;
fa[i + N] = i + N;
siz[i] = siz[i + N] = ;
s1[i] = s2[i + N] = ;
s2[i] = s1[i + N] = ;
}
top = clock = ;
return;
}
int find(int x) {
if(x == fa[x]) return x;
return find(fa[x]);
}
inline LL cal(int x) {
return 1ll * s1[x] * s2[x];
}
inline void merge(int x, int y) {
x = find(x);
y = find(y);
if(x == y) return;
if(siz[x] < siz[y]) {
std::swap(x, y);
}
stk2[++clock] = tot;
tot -= cal(x) + cal(y);
stk[++top] = opt(y, fa[y]);
fa[y] = x;
stk[++top] = opt(x, siz[x]);
siz[x] += siz[y];
stk[++top] = opt(x, s1[x]);
s1[x] += s1[y];
stk[++top] = opt(x, s2[x]);
s2[x] += s2[y];
tot += cal(x);
return;
}
inline void erase(int t) {
while(clock > t) {
tot = stk2[clock--];
s2[stk[top].x] = stk[top].y;
top--;
s1[stk[top].x] = stk[top].y;
top--;
siz[stk[top].x] = stk[top].y;
top--;
fa[stk[top].x] = stk[top].y;
top--;
}
return;
}
} void add(int L, int R, Node x, int l, int r, int o) {
if(L <= l && r <= R) {
v[o].push_back(x);
return;
}
int mid = (l + r) >> ;
if(L <= mid) {
add(L, R, x, l, mid, o << );
}
if(mid < R) {
add(L, R, x, mid + , r, o << | );
}
return;
} void solve(int l, int r, int o) {
int now = ufs::clock;
for(int i = ; i < (int)v[o].size(); i++) {
ufs::merge(v[o][i].x, v[o][i].y + N);
//printf("[%d %d] merge %d %d \n", l, r, v[o][i].x, v[o][i].y);
}
if(l == r) {
ans[r] = ufs::tot;
ufs::erase(now);
return;
}
int mid = (l + r) >> ;
solve(l, mid, o << );
solve(mid + , r, o << | );
ufs::erase(now);
return;
} int main() {
ufs::init();
scanf("%d", &n);
for(int i = , x, y; i <= n; i++) {
scanf("%d%d", &x, &y);
if(mp[x][y]) {
add(mp[x][y], i - , Node(x, y), , n, );
//printf("add : (%d %d) [%d %d] \n", x, y, mp[x][y], i - 1);
mp[x][y] = ;
}
else {
mp[x][y] = i;
}
}
std::map<int, int>::iterator it;
for(int i = ; i < N; i++) {
for(it = mp[i].begin(); it != mp[i].end(); it++) {
if(it->second) {
add(it->second, n, Node(i, it->first), , n, );
//printf("add : (%d %d) [%d %d] \n", i, it->first, it->second, n);
}
}
} solve(, n, );
for(int i = ; i <= n; i++) {
printf("%lld ", ans[i]);
}
return ;
}
AC代码
CF1140F - Extending Set of Points的更多相关文章
- CF1140F Extending Set of Points 【按时间分治,并查集】
题目链接:洛谷 首先我们考虑没有撤回操作的情况,就是将每一行和每一列看做一个点(代表行的称为白点,代表列的称为黑点),每个点$(x,y)$看做一条边. Extend操作实际上就是$x_1$行与$y_1 ...
- Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)
Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...
- Codeforces 1140F Extending Set of Points (线段树分治+并查集)
这题有以下几个步骤 1.离线处理出每个点的作用范围 2.根据线段树得出作用范围 3.根据分治把每个范围内的点记录和处理 #include<bits/stdc++.h> using name ...
- Educational Codeforces Round 62 (Rated for Div. 2) Solution
最近省队前联考被杭二成七南外什么的吊锤得布星,拿一场Div. 2恢复信心 然后Div.2 Rk3.Div. 1+Div. 2 Rk9,rating大涨200引起舒适 现在的Div. 2都怎么了,最难题 ...
- Embeding Python & Extending Python with FFPython
Introduction ffpython is a C++ lib, which is to simplify tasks that embed Python and extend Python. ...
- (转) Written Memories: Understanding, Deriving and Extending the LSTM
R2RT Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...
- [转]Extending the User Interface in Outlook 2010
本文转自:https://msdn.microsoft.com/en-us/library/office/ee692172%28v=office.14%29.aspx#OfficeOLExtendin ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
随机推荐
- RabbitMQ 连接不上
问题 [org.springframework.amqp.AmqpIOException: java.io.IOException] 解决 username: guest password: gues ...
- CSS——用户界面样式
所谓的界面样式, 就是更改一些用户操作样式, 比如 更改用户的鼠标样式, 表单轮廓等.但是比如滚动条的样式改动受到了很多浏览器的抵制,因此我们就放弃了. 防止表单域拖拽 鼠标样式cursor 设置或检 ...
- Flink on YARN(下):常见问题与排查思路
Flink 支持 Standalone 独立部署和 YARN.Kubernetes.Mesos 等集群部署模式,其中 YARN 集群部署模式在国内的应用越来越广泛.Flink 社区将推出 Flink ...
- Java基本语法.part02
变量 变量的概念: 内存中的一个存储区域 该区域有自己的名称(变量名)和类型(数据类型) Java中每个变量必须先声明,后使用 该区域的数据可以在同一类型范围内不断变化 使用变量注意: 变量的作用域: ...
- (转)java源程序加密解决方案(基于Classloader解密)
转:http://cjnetwork.iteye.com/blog/851544 源程序加密解决方案 1. 概述: Java源程序的加密,有如下两种: 1使用混淆器对源码进行混淆,降低反编译工具的作用 ...
- Hadoop完全分布式配置
***** 全部三个节点 *****1. 关闭防火墙 service iptables stop chkconfig iptables off2. 修改主机名,Hadoop主机名中不能出现_和- vi ...
- UMP系统架构 Mnesia
- 180608发现的一个有趣的Douyin-Bot项目
今日发现的github 项目 Douyin-Bot 抖音机器人发现漂亮小姐姐 :sweat_smile: 在开发者模式下,找到指针位置,开启之后,当点击屏幕时,可以获取点击屏幕处的xy坐标了 我使用的 ...
- CentOS7配置Docker镜像加速器
1. 将默认的配置文件复制出来 cp -n /lib/systemd/system/docker.service /etc/systemd/system/docker.service 2. 将加速器地 ...
- 使用DUILIB建立项目
使用DUILIB加载XML界面 这篇主要目的就是教给大家怎样在自己的工程中加载XML界面,这是最基本的应用,对于界面控件响应啥的,我就不讲了,在大家懂了这个之后,我会给大家一个其它人写的博客,再看他的 ...