This topic is to examine RMQ, this vegetable slag or not will write line segment tree, wait to write the line tree, then do this topic; This topic is used, the cursor is encoded, then the number of occurrences is encoded, and then, for each interval, corresponds to the number of times, finally, ans = max (right[ K1]-X + 1,Y-LEFT[K2] + 1); ans = max (ans,rmq (k1 + 1,k2-1)); This is the case for K2-K1 > 1, when k2-k1 = 1 o'clock only need the first formula can be, specific look at the code to write the comparison dish
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < algorithm> #include <vector> #include <map> #include <queue> #include <math.h> #include < stack> #include <utility> #include <string> #include <sstream> #include <cstdlib> #include &
Lt;set> #define LL Long long using namespace std;
const int INF = 0X3F3F3F3F;
const int MAXN = 100000 + 10;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int COUNT1[MAXN];
int NUM[MAXN];
int LEFT[MAXN];
int RIGHT[MAXN];
int DATA[MAXN];
int n;
int m;
int d[maxn][20];
void Rmq_init (int k) {for (int i = 1;i <= k;i++) d[i][0] = Count1[i]; for (int j = 1, (1<<j) <= k;j++) {for (int i = 1;i + (1<<j) <= k;i++) {d[i][j] = max (d[i][j-1],d[
i+ (1<< (j-1))][j-1]);
}}} int RMQ (int l,int R) {int k = 0;
while (1<< (k+1) <= r-l + 1) k++;
Return Max (D[l][k],d[r-(1<<k) + 1][k]); } int Main() {while (scanf ("%d", &n) && N) {scanf ("%d", &m);
for (int i = 1;i <= n;i++) {scanf ("%d", &data[i]);
} int k = 1;
for (int i = 1;i <= n;i++) {left[k] = i;
Num[i] = k;
int cnt = 1;
while (data[i] = = data[i + 1]) {cnt++;
i++;
Num[i] = k;
} Right[k] = i;
COUNT1[K] = cnt;
k++;
} rmq_init (k-1);
for (int i = 1;i <= m;i++) {int x, y;
scanf ("%d%d", &x,&y);
int k1 = num[x];
int k2 = Num[y];
int ans = 0;
if (k1 = = K2) printf ("%d\n", Y-x + 1);
else if (k2-k1 = = 1) {ans = max (Right[k1]-X + 1,Y-LEFT[K2] + 1);
printf ("%d\n", ans);
} else if (K2-k1 > 1) {ans = max (Right[k1]-X + 1,Y-LEFT[K2] + 1);
ans = max (ans,rmq (k1 + 1,k2-1));
printf ("%d\n", ans);
}}} return 0;
}