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. ...
随机推荐
- scala中Map集合的简单使用
import scala.collection.mutable /** * Map集合的简单使用 */ object MapUse { def main(args: Array[String]): U ...
- jQuery ajax - post() 方法
实例 请求 test.php 网页,忽略返回值: $.post("test.php"); TIY 实例 通过 AJAX POST 请求改变 div 元素的文本: $("i ...
- 括号匹配——cf1095E
正解应该是求后缀和前缀 但是多情况讨论好像也能过.. 大概分为: 首先排除不能改的情况 1.改左括号 2.改右括号 /* 将一个位置的括号反过来,使原序列变成合法序列 */ #include<b ...
- Delphi定时模拟键盘按键例程
delphi模拟键盘按键实例delphi模拟键盘按键实例,只是模拟一个按键的例子而已.到一定时间按下模拟按下一个按键,delphi7编译通过. 10秒点击一下H键,其他键你们去找数值替换吧,网上大把的 ...
- MFC打开/保存文件对话框:CFileDialog
MFC打开/保存文件对话框:CFileDialog CFileDialog 文件选择对话框的使用:首先构造一个对象并提供相应的参数,构造函数原型如下: CFileDialog::CFileDial ...
- vue知识点汇总
一.学习vue必须了解的几个知识点 1.node.js介绍 node是一个让JavaScript运行在服务端的开发平台,使用JavaScript也可以开发后台服务.说明白些它仅仅是一个平台,我们使用 ...
- ThinkPHP 的缓存大概多久更新一次
ThinkPHP 的缓存大概多久更新一次可以自己设置: thinkPHP的缓存默认是文件缓存,保存在Runtime文件夹里面, 如果不设置过期时间,且不清除Runtime文件,就会一直存在. 如果设置 ...
- POJ-1502-MPI Maelstrom-dijkstra+输入处理
BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distribute ...
- 17.splash_case01
# 抓取今日头条,对比渲染和没有渲染的效果 import requests from lxml import etree # url = 'http://localhost:8050/render.h ...
- 05_mybatis动态sql
1.sql片段 1.sql片段**** mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 2.需求 用户信息综合查询列表和用户信息查询列表总数这两个sta ...