1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <cstdio> #include <algorithm> #include <cctype> #include <iostream> using namespace std; int N, n, Q; inline int read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } const int maxn = 100010; int a[maxn], b[maxn], lt[maxn], tr[maxn * 2];
void build(int v = 1, int l = 0, int r = n) { if (l + 1 == r) { tr[v] = lt[r] - lt[l]; } else { int mid = (l + r) >> 1, chl = v * 2, chr = chl + 1; build(chl, l, mid); build(chr, mid, r); tr[v] = max(tr[chl], tr[chr]); } }
int query(int L, int R, int v = 1, int l = 0, int r = n) { if (l >= r || R <= l || r <= L) return -12344; if (L <= l && r <= R) return tr[v]; int mid = (l + r) >> 1, chl = v * 2, chr = chl + 1; return max(query(L, R, chl, l, mid), query(L, R, chr, mid, r)); }
void update(int &a, int b) { if (b > a) a = b; }
int main() { for(;;) { N = read(); if (!N) break; Q = read(); for (int i = 0; i < N; i++) a[i] = b[i] = read(); n = unique(b, b + N) - b; for (int i = 0; i < N; i++) a[i] = lower_bound(b, b + n, a[i]) - b; for (int i = 0; i < n; i++) lt[i] = lower_bound(a, a + N, i) - a; lt[n] = N; build(); while (Q--) { int l = read(), r = read(); l--; r--; int ans = -1244; if (a[l] == a[r]) ans = r - l + 1; else { update(ans, lt[a[l] + 1] - l); update(ans, r - lt[a[r]] + 1); int L = a[l] + 1, R = a[r]; if (L < R) update(ans, query(L, R)); } printf("%d\n", ans); } } }
|