当前位置:网站首页>#795 Div.2 E. Number of Groups set *
#795 Div.2 E. Number of Groups set *
2022-06-28 00:33:00 【Strezia】
The question
Line segments with two colors , In line segments with different colors , There are connecting edges between segments with intersecting parts . Find the number of connected blocks of a graph .
The number of line segments shall not exceed 1 0 5 10^5 105 .
Ideas
Divide each line segment into outgoing point and incoming point , And sort from small to large , Traverse this one by one 2 n 2n 2n A little bit , The entry point is equivalent to adding this line segment , The point is deleted . Maintain... Separately through set consolidation 1 1 1 and 0 0 0 Line segment of .
because n ≤ 1 0 5 n\leq 10^5 n≤105, So we can't O ( n 2 ) O(n^2) O(n2) Violence is everywhere , Consider how to optimize .
Because we sort the coordinates from small to large , We Just keep the segment with the largest right endpoint among all segments , Other line segments can be deleted ( Because as long as it can merge with the segment with the largest right endpoint , It must be possible to merge the points previously merged with the right endpoint with other deleted segments ). In this way, the number of times to divide and check the collection becomes O ( n ) O(n) O(n), The total complexity is O ( n log n ) O(n\log n) O(nlogn).
Code
int n;
int fa[maxn];
int find(int x) {
if(fa[x] == x) return x;
return fa[x] = find(x);
}
void merge(int x, int y) {
int fx = find(x);
int fy = find(y);
if(fx == fy) return;
fa[fy] = fx;
find(fy);
}
struct Node {
int c, l, r;
}a[maxn];
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) fa[i] = i;
vector<pii> op;
for(int i = 1; i <= n; i++) {
cin >> a[i].c >> a[i].l >> a[i].r;
op.pb({
a[i].l, -i});
op.pb({
a[i].r, i});
}
sort(op.begin(), op.end());
set<pii> s[2];
for(auto [pos, id] : op) {
if(id < 0) {
id = -id;
s[a[id].c].insert({
a[id].r, id});
int t = a[id].c ^ 1;
while(s[t].size() > 1) {
merge(id, s[t].begin()->second);
s[t].erase(s[t].begin());
}
if(!s[t].empty()) {
merge(id, s[t].begin()->second);
}
}
else {
s[a[id].c].erase({
a[id].r, id});
}
}
int res = 0;
for(int i = 1; i <= n; i++) {
res += (find(i) == i);
}
cout << res << endl;
}
边栏推荐
- MongoDB-在windows电脑本地安装一个mongodb的数据库
- Arduino UNO通过电容的直接检测实现简易触摸开关
- Every time I started the service of the project, the computer helped me open the browser, revealing the 100 lines of source code!
- Alchemy (3): how to do a good job in interfacing a business process
- #795 Div.2 D. Max GEQ Sum 单调栈
- 线程池实现:信号量也可以理解成小等待队列
- Sword finger offer 61 Shunzi in playing cards
- Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
- internship:业务流程初识
- 線程池實現:信號量也可以理解成小等待隊列
猜你喜欢

What is promise

NoSQL之Redis配置与优化
![[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science

Introduction to data warehouse

Modern programming language: rust

MATLB|基于复杂网络的配电系统微电网优化配置

MySQL企业级参数调优实践分享

Modern programming languages: zig

Local visualization tool connects to redis of Alibaba cloud CentOS server

Alchemy (1): identify prototype, demo and MVP in project development
随机推荐
Differences and functions between intranet IP and public IP
[microservices sentinel] sentinel data persistence
The limits of Technology (11): interesting programming
C语言malloc函数的功能及用法
Feign通过自定义注解实现路径的转义
互联网的发展为产业的变革和转型提供了新的解决方案
Is it safe for Huatai Securities to open an account online?
[paper reading | deep reading] sdne:structural deep network embedding
Alchemy (3): how to do a good job in interfacing a business process
On charsequence
积分体系和营销活动结合在一起有哪些玩法
数仓的字符截取三胞胎:substrb、substr、substring
【无标题】
代码整洁之道--格式
ValidateRequest=”false” 是做什么的「建议收藏」
互联网业衍生出来了新的技术,新的模式,新的产业类型
Understand the basic structure of wechat applet project
Modern programming languages: zig
The development of the Internet provides new solutions for industrial transformation
Summary of wuenda's machine learning course (11)_ Support vector machine