好题好题。


难点在建图,因为图的边数将会决定最小生成树的时间复杂度。我们肯定希望能够只建 \(O(n)\) 级别的边,这样时间复杂度就可以做到 \(O(n\log n)\)。

观察到当 \(i,j,k\) 三个区间能够互相连边时(这里假设 \(a_i<a_j<a_k\)),我们绝对不会连 \((i,k)\) 这条边。

那么假如我们将所有区间按 \(a\) 值大小从小到大排序,每次将当前区间与该区间中所有颜色连边,然后再将这个区间染上新颜色,我们就能保证不会连上述所谓的 \((i,k)\) 边。这很明显是可以珂学解决的。

考虑证明珂朵莉树时间复杂度正确性(实际上也是在证明边数正确性)。

假设原先有 \(x\) 个颜色段,若连 \(y\) 条边,则至少减少 \(y-3\) 个颜色段(除最左边的颜色和最右边的颜色外,中间所有颜色块都一定会被删去,同时全部更新为一个新的颜色段),假如 \(y\) 很小,那么本次操作的单次时间复杂度可忽略不计;假如 \(y\) 很大,则本次操作减少的颜色段数将与花费的时间复杂度在一个量级,就相当于这些参与连边操作的颜色段将不会再次花费时间复杂度,总体时间复杂度依旧正确。

所以我们 极端口胡的 证明了边数级别就是 \(O(n)\),那么珂朵莉树时间复杂度就是 \(O(n\log n)\)。

最小生成树就不用说了。

最终时间复杂度 \(O(n\log n)\),可以通过。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e6+5;
int t,n,m,k,ans,fa[N];
struct edge{int x,y,w;}ed[N];
struct line{int l,r,a;}ln[N];
int cmp(edge x,edge y){
return x.w<y.w;
}int cmp2(line x,line y){
return x.a<y.a;
}struct odt{
int l,r;
mutable int v;
bool operator<(const odt &c)const{
return l<c.l;
}
};set<odt>st;
#define iter set<odt>::iterator
struct chtholly{
iter spilt(int x){
iter it=st.lower_bound({x,0,0});
if(it!=st.end()&&(*it).l==x) return it;
it--;if((*it).r<x) return st.end();
int l=(*it).l,r=(*it).r,v=(*it).v;
st.erase(it);st.insert({l,x-1,v});
return st.insert({x,r,v}).first;
}void assign(int l,int r,int v){
iter tr=spilt(r+1),tl=spilt(l);
st.erase(tl,tr);st.insert({l,r,v});
}void con(int l,int r,int id){
iter tr=spilt(r+1),tl=spilt(l);
for(iter it=tl;it!=tr;it++) if((*it).v)
ed[++m]={id,(*it).v,ln[id].a-ln[(*it).v].a};
}
}seniorious;
void init(){
for(int i=1;i<=n;i++) fa[i]=i;
}int find(int x){
return fa[x]=(fa[x]==x?x:find(fa[x]));
}void solve(){
cin>>n,init(),m=ans=0,k=n;
for(int i=1;i<=n;i++)
cin>>ln[i].l>>ln[i].r>>ln[i].a;
sort(ln+1,ln+n+1,cmp2);
st.clear(),st.insert({1,(int)1e9,0});
for(int i=1;i<=n;i++){
seniorious.con(ln[i].l,ln[i].r,i);
seniorious.assign(ln[i].l,ln[i].r,i);
}sort(ed+1,ed+m+1,cmp);
for(int i=1;i<=m;i++){
int x=find(ed[i].x);
int y=find(ed[i].y);
if(x==y) continue;
k--,fa[x]=y,ans+=ed[i].w;
if(k==1){
cout<<ans<<"\n";
return;
}
}cout<<"-1\n";
}signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--) solve();
return 0;
}/*
In this world where the sun dips in the west.
Set in the heavenly forest.
When this war is over.
The people who do not return and the multitudes who look on.
In the name of justice.
A past that will never die,The fading future.
I'm back.Even if the sun is fading.
Even if I can't see the future.
The light of this moment.
I hope you won't forget.
---- The Happiest Girl in the World
*/

[CF1981E] Turtle and Intersected Segments 题解的更多相关文章

  1. POJ3304:Segments——题解

    http://poj.org/problem?id=3304 题目大意:给n条线段,求是否存在一条直线,将所有线段投影到上面,使得所有投影至少交于一点. ——————————————————————— ...

  2. [CF846C]Four Segments题解

    我们暴力枚举一下\(delim_{1}\) 然后对于每个\(delim_{1}\),O(n)扫一遍+前缀和求出最大\(delim_{0}\)和\(delim_{2}\),然后记录一下它们的位置就行啦 ...

  3. Codeforces Round #535(div 3) 简要题解

    Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...

  4. Codeforces Round #690 (Div. 3)

    第一次 ak cf 的正式比赛,不正式的是寒假里 div4 的 Testing Round,好啦好啦不要问我为什么没有 ak div4 了,差一题差一题 =.= 不知不觉已经咕了一个月了2333. 比 ...

  5. 题解-CF1389F Bicolored Segments

    题面 CF1389F Bicolored Segments 给 \(n\) 条线段 \([l_i,r_i]\),每条有个颜色 \(t_i\in\{0,1\}\),求最多选出多少条线段,使没有不同颜色的 ...

  6. CodeForces 430A Points and Segments (easy)(构造)题解

    题意:之前愣是没看懂题意...就是给你n个点的坐标xi,然后还规定了Li,Ri,要求给每个点染色,每一组L,R内的点红色和黑色的个数不能相差大于1个,问你能不能染成功,不能输出-1,能就按照输入的顺序 ...

  7. LeetCode题解之Number of Segments in a String

    1.题目描述 2.题目分析 找到字符串中的空格即可 3.代码 int countSegments(string s) { ){ ; } vector<string> v; ; i < ...

  8. PAT甲题题解-1104. Sum of Number Segments (20)-(水题)

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  9. 【题解】CF1426D Non-zero Segments

    题目戳我 \(\text{Solution:}\) 若\([l,r]\)子段和是\(0,\)则\(sum[r]=sum[l-1].\) 于是我们可以考虑维护当前哪一个前缀和出现过.对于区间\([l,r ...

  10. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

随机推荐

  1. 鸿蒙应用开发从入门到入行 - 篇8:Tabs选项卡页签视图切换

    鸿蒙应用开发从入门到入行 第八天 - Tabs选项卡 导读:在本篇文章里,您将掌握使用Tabs选项卡做栏目分类,这是未来应用开发中极为常用的组件 首先说一声抱歉,比较忙很久没更新了.但放心吧,目前该忙 ...

  2. 在 .NET 环境下访问 SOAP 服务

    在 .NET 环境下访问 SOAP 服务 SOAP 服务有着悠久的历史,目前仍然存在大量的 SOAP 服务,它是基于 HTTP 协议和 XML 技术的简单对象访问协议. 在 .NET Framewor ...

  3. 浅聊web前端性能测试

    最近正好在做web前端的性能测试,这次就来聊聊关于这个的测试思路~ 首先从用户的思维去思考,关于web前端性能,用户最看重的是什么...... 其实就是下面三个点: 1. 加载性能(即页面加载时间+资 ...

  4. Linux新用户登录时出现“-bash-4.2$”的解决办法

    Linux服务器新建的用户在登录时显示"-bash-4.2$",而不是"user@hostname"的显示方式,出现此问题的原因是在添加普通用户时,用户家目录下 ...

  5. vue-cli@4搭建 vue + element-ui 项目实操

    原文链接: vue-cli@4搭建 vue + element-ui 项目实操

  6. COCI 2024/2025 #3

    T1 P11474 [COCI 2024/2025 #3] 公交车 / Autobus 愤怒,从红升橙足以说明其恶心,考场上调了半小时才过. 这道题的车能够开 \(24\) 小时,并且他能从前一天开到 ...

  7. IM通讯协议专题学习(十):初识 Thrift 序列化协议

    本文由字节跳动技术团队杨晨曦分享,本文有修订和改动. 1.引言 本文将带你一起初步认识Thrift的序列化协议,包括Binary协议.Compact协议(类似于Protobuf).JSON协议,希望能 ...

  8. 【Java 温故而知新系列】基础知识-03 基本类型对应之包装类

    1.包装类都有哪些? 基本类型都有对应的包装类型,这些包装类提供了一种面向对象的方式来处理基本数据类型,允许它们被用于需要对象的场景,如集合框架.泛型等. 对应关系: 基本类型 包装类型 boolea ...

  9. Spring Cloud Alibaba实战,从微服务架构到基本服务配置

    https://blog.csdn.net/itcast_cn/article/details/124558887 Spring Cloud Alibaba 实战 1目标理解什么是微服务架构理解什么是 ...

  10. 深入理解第一范式(1NF):数据库设计中的基础与实践

    title: 深入理解第一范式(1NF):数据库设计中的基础与实践 date: 2025/1/15 updated: 2025/1/15 author: cmdragon excerpt: 在关系型数 ...