跳到正文

题解

ZYPRESSEN

pystraf11约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年3月11日
收录
2026年7月10日
更新
2026年11月3日
篇幅
约 11 分钟
获赞
2
题目
P12084

版本

  1. v1当前

Description

给定序列 a1∼na_{1\sim n},qq 次查询 l,rl,r,求满足 l≤i<j<k≤rl\le i<j<k\le r,以 (ai,aj,ak)(a_i,a_j,a_k) 为三边的 三角形 周长最小值,若不能输出 yumi!。

Limitations

1≤n≤2.5×1051\le n\le 2.5\times 10^5

1≤q≤5×1051\le q\le 5\times 10^5

1≤ai≤1071\le a_i\le 10^7

1≤l≤r≤n1\le l\le r\le n

4s,512MB4\text{s},512\text{MB}

Solution

把值域倍增分块,[2k,2k+1)[2^k,2^{k+1}) 为一块,总共 O(log⁡V)O(\log V) 块。

对于询问,找到最小的 kk 使得块内有 ≥3\ge 3 个数。则在本块可以任选,建线段树维护前 33 小即可。

若有 2∼32\sim 3 个数在本块前,把所有本块前的数按升序提出来记为 tit_i,显然较长的两边的长度一定取 ti,ti+1t_i,t_{i+1},双指针扫一遍,取尽量小的 jj 使得 (ti,ti+1,tj)(t_{i},t_{i+1},t_j) 合法即可。注意最长边可以在本块。

若有 11 个数在本块前,设其为 axa_x,显然它是最小的。考虑对每块求支配对,对于 axa_x 所在的块,区间 a[l,r]a[l,r] 中只能有 ≤2\le 2 个值在本块,否则不优。对块内的每个 axa_x,设向左/右最多到 l,rl,r 合法。令 bi=⌊ai/ax⌋b_i=\lfloor a_i/a_x\rfloor,那么若 (ai,aj,ax)(a_i,a_j,a_x) 合法则 ∣bi−bj∣≤1|b_i-b_j|\le 1。

枚举 a[l,r]a[l,r] 中 >ax> a_x 的数 aja_j,若 bi=bjb_i=b_j,对每种出现的 bb 开单调栈维护 aia_i,每次弹出所有 ai≥aja_i\ge a_j,(ai,aj,ax)(a_i,a_j,a_x) 就是一个支配对(最后的栈顶也算)。否则可证 aia_i 一定是取 aja_j 它左/右边第一个满足 bi=bj−1b_i=b_j-1 的位置,直接记录即可。

支配对数量是 O(nlog⁡V)O(n\log V),扫描线 + BIT 维护即可回答询问。复杂度 O(nlog⁡nlog⁡V+qlog⁡V)O(n\log n\log V+q\log V)。

code
C++
#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
	if(a < b){ a = b; return true; }
	return false;
}

template<class T>
bool chmin(T &a, const T &b){
	if(a > b){ a = b; return true; }
	return false;
}

constexpr int inf = 1e9, V = 1e7, K = 24;
struct Info {
	array<int, 3> mn;
	inline Info() { mn.fill(inf); }
	inline Info(int a, int b, int c) { mn = {a, b, c}; }
	friend Info operator+(const Info &lhs, const Info &rhs) {
		array<int, 6> a{lhs.mn[0], lhs.mn[1], lhs.mn[2], rhs.mn[0], rhs.mn[1], rhs.mn[2]};
		sort(a.begin(), a.end());
		return {a[0], a[1], a[2]};
	}
};

struct SGT {
	int size, k;
	vector<Info> d;
	inline SGT() {}
	inline SGT(const vector<int>& a) {
		int n = a.size();
		for (size = 1; size < n; size <<= 1);
		k = __lg(size);
		d.resize(size << 1);
		for (int i = 0; i < n; i++) d[size + i] = {a[i], inf, inf};
		for (int i = size - 1; i >= 1; i--) pull(i);
	}
	
	inline void pull(int u) {
		d[u] = d[u << 1] + d[u << 1 | 1];
	}
	
	inline Info query(int l, int r) {
		l += size, r += size + 1;
		Info sml, smr;
		while (l < r) {
			if (l & 1) sml = sml + d[l++];
			if (r & 1) smr = d[--r] + smr;
			l >>= 1, r >>= 1;
		}
		return sml + smr;
	}
};

struct BIT {
	vector<int> t;
	int n;
	BIT() {}
	BIT(int n) {
		this->n = n;
		t.assign(n + 1, inf);
	}
	
	void upd(int x, int val) {
		for (x++; x <= n; x += (x & -x)) chmin(t[x], val);
	}

	int qry(int x) {
		int ans = inf;
		for (x++; x; x -= (x & -x)) chmin(ans, t[x]);
		return ans;
	}
};

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, q;
	cin >> n >> q;
	
	vector<int> a(n);
	vector<vector<int>> blk(K);
	for (int i = 0; i < n; i++) cin >> a[i], blk[__lg(a[i])].push_back(i);
	
	vector<int> seq, pos(K);
	for (int i = 0; i < K; i++) {
		for (auto j : blk[i]) seq.push_back(a[j]);
		pos[i] = seq.size() - 1;
	}
	
	SGT T(seq);
	vector<vector<pair<int, int>>> Q(n), M(n);
	vector<int> ans(q);
	
	auto hang = [&](int l, int r, int id) {
		Q[l].emplace_back(r, id), ans[id] = inf;
		static vector<int> q, vec;
		q.clear();
		
		for (int j = 0; j < K; j++) {
			auto bl = lower_bound(blk[j].begin(), blk[j].end(), l), br = upper_bound(blk[j].begin(), blk[j].end(), r);
			if (br - bl >= 3) {
				int L = (j ? pos[j - 1] : -1) + 1;
				auto Ans = T.query(bl - blk[j].begin() + L, br - blk[j].begin() - 1 + L);
				ans[id] = Ans.mn[0] + Ans.mn[1] + Ans.mn[2];
				q.push_back(Ans.mn[0]);
				break;
			}
			
			vec.clear();
			while (bl < br) vec.push_back(a[*bl++]);
			if (vec.size() == 2 && vec[0] > vec[1]) swap(vec[0], vec[1]);
			for (auto val : vec) q.push_back(val);
		}
		
		for (int i = 1, sz = q.size(), j = sz - 1, mn = inf; i < sz - 1; i++) {
			if (q[i - 1] > q[i + 1] - q[i]) chmin(mn, q[i + 1] - q[i]);
			while (j > 0 && q[j - 1] > mn) --j;
			if (j < i && q[j] > q[i + 1] - q[i]) chmin(ans[id], q[j] + q[i] + q[i + 1]);
		}
	};
	
	for (int i = 0, l, r; i < q; i++) {
		cin >> l >> r;
		l--, r--, hang(l, r, i);
	}
	
	auto mdf = [&](int x, int y, int z) {
		M[min({x, y, z})].emplace_back(max({x, y, z}), a[x] + a[y] + a[z]);
	};
	
	vector<vector<int>> stk(n);
	vector<int> tab(V, -1), b(n);
	auto init = [&](int id) {
		auto &q = blk[id];
		for (int i = 0, sz = q.size(); i < sz; i++) {
			int x = q[i], l = (i - 2 >= 0 ? (q[i - 2] + 1) : 0), r = (i + 2 <= sz - 1 ? (q[i + 2] - 1) : n - 1), idx = 0;
			for (int j = r; j >= l; j--)
				if (a[j] > a[x]) {
					b[j] = a[j] / a[x];
					if (tab[b[j]] == -1) tab[b[j]] = idx++;
					auto &st = stk[tab[b[j]]];
					while (!st.empty() && a[st.back()] >= a[j]) mdf(x, j, st.back()), st.pop_back();
					if (!st.empty()) mdf(x, j, st.back());
					st.push_back(j);
				}
			
			for (int j = l; j <= r; j++) tab[b[j]] = -1;
			for (int j = 0; j < idx; j++) stk[j].clear();
			for (int j = l; j <= r; j++)
				if (a[j] > a[x]) {
					int u = tab[b[j] - 1];
					if (u != -1 && a[x] + a[u] > a[j]) mdf(x, u, j);
					tab[b[j]] = j;
				}
			
			for (int j = l; j <= r; j++) tab[b[j]] = -1;
			for (int j = r; j >= l; j--)
				if (a[j] > a[x]) {
					int u = tab[b[j] - 1];
					if (u != -1 && a[x] + a[u] > a[j]) mdf(x, u, j);
					tab[b[j]] = j;
				}
			for (int j = l; j <= r; j++) tab[b[j]] = -1;
		}
	};
	for (int i = 0; i < K; i++) init(i);
	
	BIT bit(n);
	for (int i = n - 1; i >= 0; i--) {
		for (auto it : M[i]) bit.upd(it.first, it.second);
		for (auto it : Q[i]) chmin(ans[it.second], bit.qry(it.first));
	}
	
	for (int i = 0; i < q; i++) {
		if (ans[i] != inf) cout << ans[i] << '\n';
		else cout << "yumi!\n";
	}
	
	return 0;
}
原文
存档评论1 条抓取于