1. 程式人生 > 實用技巧 >CF768D Jon and Orbs

CF768D Jon and Orbs

這段時間 \(iq\) 持續低迷,有點難受啊,\(wtcl\)

要線性統計以每個點作為區間右端點的最長距離。

考慮一些性質,已知區間 \(l...r\) 合法,則區間 \(l...{r + 1}\) 必須滿足 \(top_{r + 1} \ge \max low_{l...r}\),顯然這也是充要條件。

然後就可以用單調佇列維護區間最大值即可。

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define fi first
#define se second
typedef long long LL;
typedef pair <int, int> P;
const int inf = 0x3f3f3f3f, mod = 1e9 + 7, N = 1e6 + 5;
template <typename T>
inline void rd_(T &x) {
	x = 0; int f = 1;
	char ch = getchar();
	while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') x = x*10 + ch - '0', ch = getchar();
	x *= f;
}

int n, low[N], top[N], ans, q[N], l, r, pos;

int main() {
	rd_(n);
	rep (i, 1, n) rd_(low[i]), rd_(top[i]);
	
	ans = l = r = 1, q[1] = 1, pos = 1;
	rep (i, 2, n) {
		while (l <= r && top[i] < low[q[l]]) pos = q[l++] + 1;
		while (l <= r && low[q[r]] <= low[i]) r--;
		q[++r] = i, ans = max(ans, i - pos + 1);
	}
	
	printf("%d\n", ans);
}