跳到正文

个人记录

[CF997E] Good Subsegments(离线+线段树)

Lunch约 9 分钟
原文
文章信息与版本

文章信息

发布
2019年1月17日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

题意

  • 给你一个11到nn的排列,qq次询问,每次询问区间内好区间的个数,好区间定义为区间的数排序后是一段连续的自然数。

首先好区间条件可以转化为:(max[l,r]−min[l,r])−(r−l) (1)(max[l, r] - min[l, r]) - (r - l)\ (1) 我们考虑把询问离线挂在右端点上,每次枚举右端点 ,用线段树和单调栈维护每个位置到r(1)r(1)式的最小值,最小值的个数。

那么我们可以很轻松求出以每个rr为右端点的好区间个数,但我们还要求出一段区间内所有的好区间,那么我们再维护一个tagtag,表示当前区间最小值对当前区间贡献的次数,每次改变右端点的时候,要把当前右端点的贡献加到整个区间里去。然后这个tagtag在右端点移动后一定会被下放,所以每次直接把整个区间的tagtag赋为11就好了。

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>

#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define go(x, i) for(register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define FOR(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define debug(x) cout << #x << " = " << x << endl
#define mem(a, b) memset(a, b, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(a))
#define min(a, b) (a < b ? a : b)
#define max(a, b) (b < a ? a : b)
#define inf (0x3f3f3f3f)
#define INF (1e18)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define y1 orzorz

typedef unsigned long long ull;
typedef unsigned int uint;
typedef long long ll;
typedef std::pair<ll, int> PLI;
typedef std::pair<int, int> PII;
typedef long double ldb;
typedef double db;

namespace IO {
#define getc() ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), S_ == T_) ? 0 : *S_ ++)
#define putc(x) *nowps ++ = (x)
	const uint Buffsize = 1 << 15, Output = 1 << 23;
	static char Ch_[Buffsize], *S_ = Ch_, *T_ = Ch_;
	static char Out[Output], *nowps = Out;
	inline void flush(){fwrite(Out, 1, nowps - Out, stdout); nowps = Out;}
	template<class T>inline void read(T &_) {
		_ = 0; static char __; T ___ = 1;
		for(__ = getc(); !isdigit(__); __ = getc()) if(__ == '-') ___ = -1;
		for(; isdigit(__); __ = getc()) _ = (_ << 3) + (_ << 1) + (__ ^ 48);
		_ *= ___;
	}
	template<class T>inline void write(T _, char __ = '\n') {
		if(!_) putc('0');
		if(_ < 0) putc('-'), _ = -_;
		static uint sta[111], tp;
		for(tp = 0; _; _ /= 10) sta[++ tp] = _ % 10;
		for(; tp; putc(sta[tp --] ^ 48)); putc(__);
	}
	template<class T>inline bool chkmax(T &_, T __) {return _ < __ ? _ = __, 1 : 0;}
	template<class T>inline bool chkmin(T &_, T __) {return _ > __ ? _ = __, 1 : 0;}
}

using namespace std;
using namespace IO;

const int N = 1.2e5 + 10;

ll ans[N];

vector<PII> Q[N];

int q, n, tp1, tp2;
int Sta1[N], Sta2[N], a[N];

struct Segment_Tree {
#define ls (bh << 1)
#define rs (ls | 1)
#define mid ((l + r) >> 1)
#define lson ls, l, mid
#define rson rs, mid + 1, r
	ll S[N << 2], tag[N << 2];
	int mn[N << 2], sum[N << 2], lazy[N << 2];

	void pushup(int bh) {
		int lc = ls, rc = rs; sum[bh] = 0;
		if(mn[lc] > mn[rc]) swap(lc, rc);
		if(mn[lc] == mn[rc]) sum[bh] += sum[rc];
		mn[bh] = mn[lc], sum[bh] += sum[lc];
		S[bh] = S[lc] + S[rc];
	}

	void pushdown(int bh) {
		if(lazy[bh]) {
			mn[ls] += lazy[bh], lazy[ls] += lazy[bh];
			mn[rs] += lazy[bh], lazy[rs] += lazy[bh];
		}
		if(tag[bh]) {
			if(mn[bh] == mn[ls])
				S[ls] += tag[bh] * sum[ls], tag[ls] += tag[bh];
			if(mn[bh] == mn[rs])
				S[rs] += tag[bh] * sum[rs], tag[rs] += tag[bh];
		}
		lazy[bh] = tag[bh] = 0;
	}

	void build(int bh, int l, int r) {
		mn[bh] = l, sum[bh] = 1;
		if(l ^ r) build(lson), build(rson);
	}

	void update(int bh, int l, int r, int x, int y, int z) {
		if(x > r || y < l) return;
		if(x <= l && r <= y) mn[bh] += z, lazy[bh] += z;
		else {
			pushdown(bh);
			update(lson, x, y, z);
			update(rson, x, y, z);
			pushup(bh);
		}
	}

	ll query(int bh, int l, int r, int x, int y) {
		if(x > r || y < l) return 0;
		if(x <= l && r <= y) return S[bh];
		pushdown(bh);
		return query(lson, x, y) + query(rson, x, y);
	}

}T;

int main() {
#ifdef ylsakioi
	file("cf997e");
#endif
	int x, y;

	read(n);
	For(i, 1, n) read(a[i]);
	read(q);
	For(i, 1, q) {
		read(x), read(y);
		Q[y].pb(mp(x, i));
	}

	T.build(1, 1, n);

	For(r, 1, n) {
		T.update(1, 1, n, 1, n, -1);

		for(; tp1 && a[Sta1[tp1]] < a[r]; -- tp1)
			T.update(1, 1, n, Sta1[tp1 - 1] + 1, Sta1[tp1], a[r] - a[Sta1[tp1]]);
		Sta1[++ tp1] = r;

		for(; tp2 && a[Sta2[tp2]] > a[r]; -- tp2)
			T.update(1, 1, n, Sta2[tp2 - 1] + 1, Sta2[tp2], a[Sta2[tp2]] - a[r]);
		Sta2[++ tp2] = r;

		T.S[1] += T.sum[1], T.tag[1] = 1;

		for(auto v : Q[r]) ans[v.y] = T.query(1, 1, n, v.x, r);
	}

	For(i, 1, q) write(ans[i]);

	return flush(), 0;
}
原文