分块。
\(f[i][j]\):\(i\) 一直到第 \(i\) 所在块 \(x\) 尾端,对 \(x+1\sim j\) 块造成贡献/\(i\) 一直到 \(i\) 所在块 \(x\) 开头,对 \(j\sim x-1\) 块造成贡献。
\(mn[i][j]\):块 \(i\sim j\) 的答案。
预处理:块内元素排序,双指针求 \(f\) 后得到 \(mn\)。
查询:整块以及散块对整块的贡献查询 \(mn\) 和 \(f\),散块暴力归并。
时间复杂度 \(O(n\sqrt{n}))\)。
分块题做得少,好像这是个很经典的分块(?)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++)
const int S = 200;
inline void cmn(int &x, const int y) {if (x > y) x = y;}
char buf[100000], *p1, *p2;
inline int read() {
char ch; int x = 0; while ((ch = gc) < 48);
do x = x * 10 + ch - 48; while ((ch = gc) >= 48); return x;
}
struct node {
int id, val;
inline bool operator < (const node a) const {return val < a.val;}
} b[100005];
int f[200000][505], mn[505][505], a[100005], lft[505], rgt[505], c[405], n, q;
bool mark[100005];
void calc(int l, int r) {
for (int i = lft[l], j = lft[r]; i <= rgt[l]; ++ i) {
while (j < rgt[r] && b[j + 1].val <= b[i].val) ++ j;
if (b[j].val > b[i].val) f[b[i].id][r] = b[j].val - b[i].val;
else {
f[b[i].id][r] = b[i].val - b[j].val;
if (j < rgt[r]) cmn(f[b[i].id][r], b[j + 1].val - b[i].val);
}
}
}
void build() {
memset(f, 0x3f, sizeof f), memset(mn, 0x3f, sizeof mn);
for (int i = 1; i <= (n + S - 1) / S; ++ i) {
lft[i] = (i - 1) * S + 1, rgt[i] = std::min(i * S, n);
std::sort(b + lft[i], b + rgt[i] + 1);
for (int j = lft[i]; j < rgt[i]; ++ j) cmn(mn[i][i], b[j + 1].val - b[j].val);
}
for (int l = 1; l <= (n + S - 1) / S; ++ l) {
for (int r = l - 1; r; -- r)
for (int i = (calc(l, r), cmn(f[lft[l]][r], f[lft[l]][r + 1]), lft[l] + 1); i <= rgt[l]; ++ i)
cmn(f[i][r], std::min(f[i - 1][r], f[i][r + 1]));
for (int r = l + 1; r <= (n + S - 1) / S; ++ r)
for (int i = (calc(l, r), cmn(f[rgt[l]][r], f[rgt[l]][r - 1]), rgt[l] - 1); i >= lft[l]; -- i)
cmn(f[i][r], std::min(f[i + 1][r], f[i][r - 1]));
}
for (int i = 1; i <= (n + S - 1) / S; ++ i)
for (int j = i + 1; j <= (n + S - 1) / S; ++ j) mn[i][j] = std::min({mn[i][j - 1], mn[j][j], f[rgt[j]][i]});
}
int getmin(int l, int r) {
int i = (l - 1) / S + 1, ans = 1e9, cnt = 0;
for (int j = lft[i]; j <= rgt[i]; ++ j) if (l <= b[j].id && b[j].id <= r) c[++ cnt] = b[j].val;
for (int i = 1; i < cnt; ++ i) cmn(ans, c[i + 1] - c[i]);
return ans;
}
int merge_sort(int l1, int r1, int l2, int r2) {
int ans = 1e9, cnt = 0, mid = 0, i = (l1 - 1) / S + 1, j = (l2 - 1) / S + 1;
for (int k = lft[i]; k <= rgt[i]; ++ k) if (l1 <= b[k].id && b[k].id <= r1) c[++ cnt] = b[k].val;
mid = cnt;
for (int k = lft[j]; k <= rgt[j]; ++ k) if (l2 <= b[k].id && b[k].id <= r2) c[++ cnt] = b[k].val;
std::inplace_merge(c + 1, c + mid + 1, c + cnt + 1);
for (int k = 1; k < cnt; ++ k) cmn(ans, c[k + 1] - c[k]);
return ans;
}
int query(int l, int r) {
int i = (l - 1) / S + 1, j = (r - 1) / S + 1;
if (i == j) return getmin(l, r);
return std::min({mn[i + 1][j - 1], f[l][j - 1], f[r][i + 1], merge_sort(l, i * S, (j - 1) * S + 1, r)});
}
int main() {
n = read();
for (int i = 1; i <= n; ++ i) a[i] = b[i].val = read(), b[i].id = i;
build(), q = read();
for (int i = 1, l, r; i <= q; ++ i) l = read(), r = read(), printf("%d\n", query(l, r));
return 0;
}
标签:p1,val,int,mn,lft,CF765F,505
From: https://www.cnblogs.com/stinger/p/16637066.html