ZYPRESSEN
Description
给定序列 yumi!。
Limitations
Solution
把值域倍增分块,
对于询问,找到最小的
若有
若有
枚举
支配对数量是
::::info[code]
#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;
}
::::