【BZOJ 1178】【APIO 2009】CONVENTION会议中心_as2886089的博客-程序员宝宝

技术标签: php  数据结构与算法  

http://www.lydsy.com/JudgeOnline/problem.php?id=1178

这道题想了好久没想明白,倍增数组通过看题解很快就明白了,但是一小段区间内应有的最多线段数一直不知道怎么记录。

后来聪哥提醒我才明白,直接getans(l,r)不就完了吗_(:з」∠)_根本不用记录啊QwQ

我用splay维护线段的位置顺序,查找前驱后继等等。
上午因为懒得写插入线段前判断线段将要覆盖的区间是否是空的,所以又写了一棵线段树。然后因为一个自以为是线段树写残了的错误,就把线段树删了QAQ

后来才知道那个错误是因为代码宽度很大以至于最右边被遮住的一个部分因为减号打成了加号调了一下午(/= _ =)/~┴┴,最后还是写了splay查找前驱后继,调了一天了QAQ。我竟然把这种题当代码题做了!

最后,最后一条线段的编号后面不能有空格,否则会PE

时间复杂度$O(nlogn)$

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 200003;
int in() {
	int k = 0, fh = 1; char c = getchar();
	for(; c < '0' || c > '9'; c = getchar())
		if (c == '-') fh = -1;
	for(; c >= '0' && c <= '9'; c = getchar())
		k = (k << 3) + (k << 1) + c - '0';
	return k * fh;
}

struct Line {
	int l, r;
	bool operator < (const Line &A) const {
		return r == A.r ? l > A.l : r < A.r;
	}
} L[N], Lf[N], F[N], lth, rth;
int right[N << 1], cnt = 0, H[N << 1], n, tot, f[N][19], ans;

int getans(int l, int r) {
	l = right[l - 1]; int ret = 0;
	for(int j = 18; j >= 0; --j)
		if (f[l][j] <= r && f[l][j] != 0) {
			ret += (1 << j);
			l = right[f[l][j]];
		}
	return ret;
}

namespace Splay {
	struct node *null;
	struct node {
		node *ch[2], *fa;
		int l, r, s;
		node(int left = 0, int right = 0) {
			ch[0] = ch[1] = fa = null;
			l = left; r = right; s = 1;
		}
		bool pl() {return fa->ch[1] == this;}
		void setc(node *r, bool c) {ch[c] = r; r->fa = this;}
		void count() {s = ch[0]->s + ch[1]->s + 1;}
	} *root;
	void rotate(node *r) {
		node *f = r->fa;
		bool c = r->pl();
		if (f == root) root = r, r->fa = null;
		else f->fa->setc(r, f->pl());
		f->setc(r->ch[!c], c);
		r->setc(f, !c);
		f->count();
	}
	void splay(node *r, node *tar = null) {
		for(; r->fa != tar; rotate(r))
			if (r->fa->fa != tar) rotate(r->pl() == r->fa->pl() ? r->fa : r);
		r->count();
	}
	void insert(int l, int r) {
		bool c; node *now = root;
		if (root == null) {
			root = new node(l, r);
			return;
		}
		while (1) {
			if (l > now->r) c = true;
			else if (r < now->l) c = false;
			if (now->ch[c] == null) {
				now->setc(new node(l, r), c);
				splay(now->ch[c]);
				return;
			} else now = now->ch[c];
		}
	}
	void init(int l, int r) {
		null = new node();
		null->ch[0] = null->ch[1] = null->fa = null;
		null->s = 0;
		root = null;
		insert(l, l); insert(r, r);
	}
	Line query_l(node *now, int l, int tmp) {
		if (now == null) return (Line) {0, -0x7fffffff};
		if (now->r >= l) return query_l(now->ch[0], l, tmp);
		else {
			tmp += now->ch[0]->s + 1;
			return max((Line) {tmp, now->r}, query_l(now->ch[1], l, tmp));
		}
	}
	Line query_r(node *now, int r, int tmp) {
		if (now == null) return (Line) {cnt, 0x7fffffff};
		if (now->l <= r) return query_r(now->ch[1], r, tmp + now->ch[0]->s + 1);
		else return min((Line) {tmp + now->ch[0]->s + 1, now->l}, query_r(now->ch[0], r, tmp));
	}
}

int main() {
	n = in();
	for(int i = 1; i <= n; ++i) {
		L[i].l = in(); L[i].r = in();
		H[++cnt] = L[i].l; H[++cnt] = L[i].r;
	}
	sort(H + 1, H + cnt + 1);
	cnt = unique(H + 1, H + cnt + 1) - H;
	for(int i = 1; i <= n; ++i) {
		L[i].l = lower_bound(H + 1, H + cnt, L[i].l) - H;
		L[i].r = lower_bound(H + 1, H + cnt, L[i].r) - H;
		F[i] = L[i];
	}
	sort(L + 1, L + n + 1);
	
	Lf[tot = 1] = L[1];
	for(int i = 2; i <= n; ++i)
		if (L[i].l > Lf[tot].l)
			Lf[++tot] = L[i];
	Lf[tot + 1].l = 0x7fffffff;
	
	int head, tail = tot;
	for(head = cnt - 1; head > 0; --head) {
		while (head < Lf[tail].l) --tail;
		right[head] = tail + 1;
	}
	right[0] = 1;
	
	for(int i = 1; i <= tot; ++i) f[i][0] = Lf[i].r;
	for(int j = 1; j <= 18; ++j)
		for(int i = 1; i <= tot; ++i) {
			if (f[i][j - 1] == 0 || (head = f[right[f[i][j - 1]]][j - 1]) == 0) continue;
			f[i][j] = head;
		}
	
	Splay::init(0, cnt);
	printf("%d\n", (ans = getans(1, cnt - 1)));
	int l, r;
	for(int i = 1; i <= n; ++i) {
		lth = Splay::query_l(Splay::root, F[i].l, 0);
		rth = Splay::query_r(Splay::root, F[i].r, 0);
		if (rth.l - lth.l != 1) continue;
		l = lth.r; r = rth.r;
		if (getans(l + 1, F[i].l - 1) + getans(F[i].r + 1, r - 1) + 1 == getans(l + 1, r - 1)) {
			printf("%d", i);
			Splay::insert(F[i].l, F[i].r);
			--ans;
			if (ans) putchar(' ');
		}
	}
	
	puts("");
	return 0;
}

转载于:https://www.cnblogs.com/abclzr/p/5788299.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/as2886089/article/details/101685275

智能推荐

android开发 实现动态获得app的cpu占有率并导出文件的两种方法。_安卓 cpu占比获取_香菜美汁源的博客-程序员宝宝

最近在做学校实验室的项目的时候,师兄要求我对app的性能进行评估,主要是从电量、cpu占有率、python模型的响应时间三者进行统计分析,电量使用广播可以进行统计、python模型的运行速度用时间函数就可以计算,关于cpu占有率~interesting!这是值得研究一下的。

pytorch网络结构可视化(torchsummary+tensorboardX+网络多输通道入情况+多分支网络)_torchsummary 多个输入_饿不坏的企鹅的博客-程序员宝宝

**torchsummary和tensorboardX的使用****1.单通道输入网络**结构1结构2实例012.多通道输入网络结构实例02(只用了卷积层进行演示)**3.参考链接:**1.单通道输入网络单通道输入的情况大致有以下两种结构:结构1只有一条路可以走结构2输入为一条路,输出为多条路以上两种的输入只有一个input,这种是经常遇到的情况。实例01import torchimport torch.nn as nnimport torch.nn.functional as F

程序员必备编程之基于vue的 Nuxt.js(二)路由详解_nuxt vue2 路由映射_煌sir的博客-程序员宝宝

Nuxt.js(二)路由一.概述Nuxt.js 依据 pages 目录结构自动生成 vue-router 模块的路由配置。(不需要手动去配路由,文件生成自动生成对应路由的路径)要在页面之间使用路由,我们建议使用 标签。 是vue原生态切换路径标签。 在原生态的基础上,进行了增强。如果需要找一个nuxt实例代码,可以使用官方实例https://zh.nuxtjs.org/ex...

那些年我在CSDN追过的安全白帽师傅,respect_安全博客_Eastmount的博客-程序员宝宝

2019年7月,我来到了一个陌生的专业——网络空间安全专业。作为一个长期以Python数据挖掘和NLP方向为主的学生,突然换大方向,去从事系统安全和逆向分析的研究,还是挺难的,这两年的过程也极其艰辛。依稀记得,换专业当天我下定决心:希望利用未来四年时间,深入学习安全技术,学会撰写高质量论文,并通过分享让更多的初学者了解和入门安全领域。更期盼博士早日毕业,回到家乡贵州继续从事安全技术和大数据分析的教学。

已解决-您没有权限打开应用程序“xf-adesk2018.app”_mac xfadesk2018无法打开_月球挖掘机的博客-程序员宝宝

在macOS上激活Autodesk报错,“您没有权限打开应用程序“xf-adesk2018.app””解决办法:打开终端,输入命令:/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"输入序号“1”,然后输入“Y”,接着输入电脑密码(输密码什么都不显示的,输完直接回车就行了)等待1-2分钟就可以安装完。接下来安装upx,终端继续输入:brew in...

Spring Boot整合IBM MQ 连接发送和接受_springboot 连接ibmmq_程序员s的博客-程序员宝宝

从网上整理的,有些问题再更改了一下。1.pom &lt;!--ibm mq--&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-jms&lt;/artifactId&g...

随便推点

ELK是什么呢?_elk是什么意思_天真.。的博客-程序员宝宝

一、ELK是什么?ELK实际上是三个工具的集合,Elasticsearch + Logstash + Kibana,这三个工具组合形成了一套实用、易用的监控架构,很多公司利用它来搭建可视化的海量日志分析平台。1. ElasticSearchElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elastics...

使用RenderTargetBitmap获取WPF控件图像_wpf 获取控件画像_wolfzz88的博客-程序员宝宝

在获取控件图像的时候,需要使用border包含控件,否则因为margin和其他设置导致获取的图像不对。

linux(Ubuntu)安装tomcat,启动正常,但是浏览器访问不了tomcat的解决方法。_Morning_G的博客-程序员宝宝

首先关闭防火墙service iptables stop然后在 vim /etc/sysconfig/iptables配置文件中加入8080端口 -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 ...

免费基站定位api代码分享_appjiekou的博客-程序员宝宝

免费基站定位api支持基站定位,通过移动联通基站的CID和LAC进行基站位置查询。接口名称:免费基站定位api接口平台:聚合数据接口地址:http://v.juhe.cn/cell/get支持格式:JSON/XML请求方式:GET/POST请求示例:http://v.juhe.cn/cell/get?mnc=0&cell=28655&lac=17695&key=您申请的AP

FFmpeg中使用静音音源anullsrc无法使用duration指定时长的问题_ffmpeg duration_Jack_Chai的博客-程序员宝宝

本文出处:http://blog.csdn.net/chaijunkun/article/details/115406582,转载请注明。由于本人不定期会整理相关博文,会对相应内容作出完善。因此强烈建议在原始出处查看此文。在FFmpeg官方网站的帮助文档中,针对静音音源anullsrc的说明中描述了如下两个参数:nb_samples, nSet the number of samples per requested frames.duration, dSet the duration of th

Vim/Vi小技巧_dandelionj的博客-程序员宝宝

Vim/Vi一直是UNIX/Linux系统上最流行的文本编辑器,从2001年接触UNIX至今,Vim/Vi始终是我修改系统文件、编写简单程序的首选编辑器,是居家旅行必备之工具。如何提升它的编写速度,本文着重介绍了一些使用技巧供大家参考。  值得一提的是Vim是慈善软件(CharityWare),如有赞助或评比得奖,所得将全部救助乌干达孤儿,软件使用是免费的,欢迎手头有点闲钱的使用者捐款赞助,如