【LOJ2391】「JOISC 2017 Day 1」港口设施

这篇博客主要解析了LOJ2391题目的解题思路,指出一系列区间满足正常操作的条件是区间间只有包含或相离关系。通过构建边来表示相交但不包含的区间,形成图的联通块,其中二分图的联通块对答案有乘以2的贡献。博主利用线段树求得生成森林,并提出任意染色方案验证的解决方案,实现了O(NLogN)的时间复杂度。
摘要由CSDN通过智能技术生成

【题目链接】

【思路要点】

  • 显然,一系列区间能够正常出栈入栈等价于它们之间只存在包含或相离关系。
  • 在存在相交但不包含关系的区间之间连一条边,则显然各个联通块相互独立,若某联通块为二分图,则其对答案的贡献为 × 2 \times2 ×2 ,否则其对答案的贡献为 × 0 \times0 ×0
  • 用线段树找到图的一个生成森林,此时我们已经可以知道该图的联通块个数。任意取一组染色方案验证即可。
  • 时间复杂度 O ( N L o g N ) O(NLogN) O(NLogN)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 5;
const int P = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {
    x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {
    x = min(x, y); } 
template <typename T> void read(T &x) {
    
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
    
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
    
	write(x);
	puts("");
}
struct SegmentTreeMax {
    
	struct Node {
    
		int lc, rc;
		int Max, pos;
	} a[MAXN * 4];
	int n, size, root;
	void update(int root) {
    
		a[root].Max = max(a[a[root].lc].Max, a[a[root].rc].Max);
		if (a[root].Max == a[a[root].lc].Max) a[root].pos = a[a[root].lc].pos;
		else a[root].pos = a[a[root].rc].pos;
	}
	void build(int &root, int l, int r
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值