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
| const int N = 4000100; const int INF = 0x3fffffff;
int n, m, a[N], len[N]; int vis[N], st[N][30], lg2[N];
template <typename T> inline T Max(const T a, const T b) { return a > b ? a : b; }
inline void get_len() { for (int l = 1, r = 0; l <= n; ++ l) { while (r < n && !vis[a[r + 1]]) ++ r, vis[a[r]] = 1; len[l] = r - l + 1, vis[a[l]] = 0; } }
inline void prework() { lg2[1] = 0; for (int i = 2; i <= n; ++ i) lg2[i] = lg2[i >> 1] + 1; for (int i = 1; i <= n; i ++) st[i][0] = len[i]; for (int j = 1; j <= 22; ++ j) for (int i = 1; i <= n; ++ i) if (i + (1 << (j - 1)) <= n) st[i][j] = Max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); else st[i][j] = st[i][j - 1]; }
inline bool check(int l, int r, int x) { int L = lg2[r - l - x + 2]; int res = Max(st[l][L], st[r - x - (1 << L) + 2][L]); if (res >= x) return true; else return false; }
int main(){ scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++ i) { scanf("%d", &a[i]); a[i] += 1e6; } get_len(); prework(); while (m --) { int x, y; scanf("%d%d", &x, &y); x ++, y ++; int l = 0, r = y - x + 1, ans = 0; while (l <= r) { int mid = (l + r) >> 1; if (check(x, y, mid)) ans = mid, l = mid + 1; else r = mid - 1; } printf("%d\n", ans); } return 0; }
|