【题目链接】
【思路要点】
- 显然,一系列区间能够正常出栈入栈等价于它们之间只存在包含或相离关系。
- 在存在相交但不包含关系的区间之间连一条边,则显然各个联通块相互独立,若某联通块为二分图,则其对答案的贡献为 × 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