【LOJ10121】与众不同
题面
题解
这题是_(tham)给(ztl)他们做的,然而这道题™居然还想了蛮久。。。
首先可以尺取出一个位置(i)上一个合法的最远位置(pre_i)
而对于一个询问((l,r)),因为(pre_i)是单调的
所以可以二分出(pre_igeq l)的第一个位置(mid)
用(st)表维护一下区间(i-pre_i+1)最大值(qmax)
则(ans=max(mid-l,qmax(mid,r)))
注意判断一下边界情况
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 200005;
const int MAX_LOG_N = 19;
const int MAX_V = 1e6;
int bln[MAX_V << 1 | 1];
int N, M, a[MAX_N], pre[MAX_N];
int st[MAX_N][MAX_LOG_N], lg2[MAX_N];
void Prepare() {
int l = 1, r = 0;
do {
bln[a[++r]]++;
while (bln[a[r]] > 1) --bln[a[l++]];
pre[r] = l;
} while (l <= N && r <= N && l <= r);
for (int i = 1; i <= N; i++) st[i][0] = i - pre[i] + 1;
for (int j = 1; j <= 18; j++)
for (int i = 1; i + (1 << j) - 1 <= N; i++)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
for (int i = 2; i <= MAX_N; i++) lg2[i] = lg2[i >> 1] + 1;
}
int qmax(int l, int r) {
int t = lg2[r - l + 1];
return max(st[l][t], st[r - (1 << t) + 1][t]);
}
int solve(int ql, int qr) {
if (qmax(qr, qr) >= qr - ql + 1) return qr - ql + 1;
int l = ql, r = qr, res = qr;
while (l <= r) {
int mid = (l + r) >> 1;
if (ql <= pre[mid]) res = mid, r = mid - 1;
else l = mid + 1;
}
return max(res - ql, qmax(res, qr));
}
int main () {
N = gi(), M = gi();
for (int i = 1; i <= N; i++) a[i] = gi() + MAX_V;
Prepare();
while (M--) {
int l = gi() + 1, r = gi() + 1;
printf("%d
", solve(l, r));
}
return 0;
}