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 68 69 70 71 72 73 74
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int N = 1e5+10;
int n, q; int a[N], b[N], vis[N], cnt[N];
int mx[N*4], siz[N*4], ls[N*4], rs[N*4];
void change(int k1, int l, int r) { int mid = l+r >> 1; ls[k1] = ls[k1*2]; rs[k1] = rs[k1*2|1]; mx[k1] = max(mx[k1*2], mx[k1*2|1]); if(a[mid] == a[mid+1]) { if(mx[k1*2] == mid-l+1) { ls[k1] += ls[k1*2+1]; } if(mx[k1*2|1] == r-mid) { rs[k1] += rs[k1*2]; } mx[k1] = max(mx[k1], rs[k1*2] + ls[k1*2+1]); }
}
void buildtree(int k1, int l, int r) { siz[k1] = r-l+1; if(l == r) { mx[k1] = ls[k1] = rs[k1] = 1; return; } int mid = l+r >> 1; buildtree(k1*2, l, mid); buildtree(k1*2+1, mid+1, r); change(k1, l, r); }
int Find(int k1, int l, int r, int L, int R) { if(l > R || r < L) return 0; if(L <= l && r <= R) { return mx[k1]; } int mid = l+r >> 1; int m1 = Find(k1*2, l, mid, L, R); int m2 = Find(k1*2+1, mid+1, r, L, R); int ans = max(m1, m2); if(a[mid] == a[mid+1]) { ans = max(ans, min(rs[k1*2], mid-L+1)+min(ls[k1*2+1], R-mid)); } return ans; }
int main() { while(scanf("%d", &n) != EOF) { if(n == 0) break; scanf("%d", &q); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); } buildtree(1, 1, n); for(int i = 1; i <= q; i++) { int x, y; scanf("%d%d", &x, &y); printf("%d\n", Find(1, 1, n, x, y)); } }
return 0; }
|