[RMQ problem] uva1_35frequent values and rmq Problems
/* RMQ problem using array: cnt [I]: number of segments I (each segment refers to the same number of segments) (here this array is equivalent to the [] array in the RMQ question.) num [I]: left1 [I]: location I left endpoint location right1 [I]: location I right endpoint location other right1 [L]-L + 1: number of elements from L to the end of the segment R-left [R] + 1: Number of elements starting from the segment to r rmq (num [L] + 1, num [R]-1): From num [L] + 1 to num [R] -The maximum cnt in the 1 segment; the final result is the maximum value of the above three numbers: If: num [L] = num [R] (I .e: L and R in the same segment), the result is R-L + 1; 1_rmq: void RMQ_init () preprocessing {int n = size; for (int I = 0; I <n; I ++) d [I] [0] = cnt [I]; for (int j = 1; (1 <j) <= n; j ++) {for (int I = 0; I + (1 <j)-1 <n; I ++) {d [I] [j] = max (d [I] [J-1], d [I + (1 <(J-1)] [J-1]) ;}}int RMQ (int L, int R) query {if (L> R) return 0; int k = 0; while (1 <(k + 1) <= R-L + 1) k ++; return max (d [L] [k], d [R-(1 <k) + 1] [k]);} struct cnt array, left1 array, right1 Array Construction: int x, p = 100000000; ---------------------------- P must be greater than the value range of x int ct =-1 ;------------------------------ ------ Number of input data segments int _ left; ---------------------------------------- left endpoint of the current segment for (int I = 0; I <n; I ++) {scanf ("% d ", & x); if (x = p) {cnt [ct] ++; -------------------------------- if the number entered is the same as the previous one, the number of this section + 1 num [I] = ct; ---------------------------- constantly update the left1 [I] = _ left; ------------------------- left endpoint} else {if (ct> = 0) {for (int j = _ left; j <= I-1; j ++) ------- update right endpoint right1 [j] = I-1;} ct ++; _ left = I; num [I] = ct; left1 [I] = _ Left; cnt [ct] = 1; p = x ;}} for (int I = _ left; I <n; I ++) right1 [I] = n-1; size = ct + 1; optional */# include <iostream> # include <cstdio >#include <climits> # include <cstring> using namespace std; const int N = 100010; int n, q; int cnt [N], num [N], left1 [N], right1 [N]; int a [N]; int size; int d [N] [30]; void RMQ_init () {int n = size; for (int I = 0; I <n; I ++) d [I] [0] = cnt [I]; for (int j = 1; (1 <j) <= n; j ++) {for (int I = 0; I + (1 <j)-1 <n; I ++) {d [I] [j] = max (d [I] [J-1], d [I + (1 <(J-1)] [J-1]) ;}} int RMQ (int L, int R) {if (L> R) return 0; int k = 0; while (1 <(k + 1 )) <= R-L + 1) k ++; return max (d [L] [k], d [R-(1 <k) + 1] [k]);} int main () {// freopen ("input.txt", "r", stdin); while (scanf ("% d", & n, & q )! = EOF & n) {memset (cnt, 0, sizeof (cnt); memset (left1, 0, sizeof (left1); memset (right1, 0, sizeof (right1); memset (d, 0, sizeof (d); int x, p = 100000000; int ct =-1; int _ left; for (int I = 0; I <n; I ++) {scanf ("% d", & x); if (x = p) {cnt [ct] ++; num [I] = ct; left1 [I] = _ left;} else {if (ct> = 0) {for (int j = _ left; j <= I-1; j ++) right1 [j] = I-1;} ct ++; _ left = I; num [I] = ct; left1 [I] = _ left; cnt [ct] = 1; p = x ;}} for (int I = _ left; I <n; I ++) right1 [I] = n-1; size = ct + 1; RMQ_init (); while (q --) {int L, R; scanf ("% d", & L, & R); L --; R --; int t1 = right1 [L]-L + 1; int t2 = RMQ (num [L] + 1, num [R]-1), int t3 = R-left1 [R] + 1; if (num [L] = num [R]) printf ("% d \ n", R-L + 1); else printf ("% d \ n ", max (t1, max (t2, t3) ;}} return 0 ;}