Title Address: POJ 3264
In order to learn the online LCA algorithm, first learn RMQ ... RMQ first hair, pure template problem. Not much to say.
The code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL Long Long#define PI ACOs ( -1.0)Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;intdp1[51000][ -], dp2[51000][ -], a[51000], N;voidRMQ () {intI, J; for(i=1; i<=n;i++) {dp1[i][0]=dp2[i][0]=a[i]; } for(j=1;(1<<J) <=n;j++) { for(i=1; i<=n-(1<<J) +1; i++) {Dp1[i][j]=max (dp1[i][j-1],dp1[i+ (1<<j-1)][j-1]); Dp2[i][j]=min (dp2[i][j-1],dp2[i+ (1<<j-1)][j-1]); } }}intQueryintLintR) {intk=0; while((1<<k+1) <=r-l+1) k++;returnMax (dp1[l][k],dp1[r+1-(1<<k)][k])-min (dp2[l][k],dp2[r+1-(1<<k)][k]);}intMain () {intQ, I, L, R, J; while(scanf("%d%d", &n,&q)!=eof) { for(i=1; i<=n;i++) {scanf("%d", &a[i]); } RMQ (); while(q--) {scanf("%d%d", &l,&r);printf("%d\n", query (l,r)); } }return 0;}
POJ 3264 Balanced Lineup (RMQ)