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
| #include <iostream> #include <algorithm> #include <string> #include <cstring> #include <math.h> #include <stdio.h> using namespace std; int dp[100005][100]; int sum[100005]; int a[100005]; void RMQ(int a[], int n) { memset(dp, -1, sizeof(dp)); for(int i = 1; i <= n; ++i) dp[i][0] = sum[i]; for(int j = 1; j <= log(n) / log(2); ++j) for(int i = 1; i + (1 << (j - 1)) <= n; ++i) dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]); } int query(int l,int r) { if(l > r) return 0; int k = log(r - l + 1) / log(2); return max(dp[l][k], dp[r - (1 << k) + 1][k]); } int main() { int n, m, l, r; while(scanf("%d",&n) != EOF && n) { scanf("%d",&m); for(int i = 1; i <= n; ++i) { scanf("%d",&a[i]); if(i == 1) sum[i] = 1; else { if(a[i] == a[i - 1]) sum[i] = sum[i - 1] + 1; else sum[i] = 1; } } RMQ(a, n); while(m--) { scanf("%d %d",&l,&r); int t = l; while(a[t] == a[t - 1] && t <= r) { t++; } int num = query(t, r); cout << max(num, t - l) << '\n'; } } }
|