Topic Connection:
Codeforces 279C
Main topic:
Give a sequence, M query, each given a substring, ask whether the substring is satisfied, in the middle can find an element, let this element as a separate monotonous demarcation.
Topic Analysis:
- First for each query, we know that the demarcation must be the largest element, so we can use RMQ to preprocess the maximum interval.
- Then in order to determine whether this interval can be through the largest element as the boundary point and the monotony, we can through preprocessing, positive and reverse sweep, record a point of the largest element of the maximum number of left and right to get the length of the longest monotone substring, and then only need O (1) at a time to judge it, Determines whether the largest substring of the current element's largest extension overwrites the substring of the current query. If it is, then the result is yes, otherwise no
AC Code:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#define MAX 100007using namespace STD;typedefpair<int,int> PII;intn,m,a[max],lef[max],rig[max],dp[max][ -],ans[max][ -];voidMake () { for(inti =1; I <= N; i++) {dp[i][0] = A[i]; ans[i][0] = i; } for(intj =1; (1<<J) <= N; J + +) for(inti =1; i+ (1<<J)-1<= N; i++) {Dp[i][j] = max (dp[i][j-1], dp[i+ (1<< (J-1))][j-1]);if(dp[i][j-1] = = Dp[i][j]) ans[i][j] = ans[i][j-1];ElseANS[I][J] = ans[i+ (1<< (J-1))][j-1]; } for(inti =1; I <= N; i++) Lef[i] = rig[i] =1; for(inti =2; I <= N; i++)if(a[i-1] <= A[i]) lef[i] = lef[i-1]+1; for(inti = n1; I >=1; i--)if(a[i+1] <= A[i]) rig[i] = rig[i+1]+1;} PII Query (intLintR) {intK = (int)((Log((r-l+1)*1.0))/Log(2.0));intMAXN;intTemp MAXN = Max (Dp[l][k], dp[r-(1<<K) +1][k]);if(MAXN = = Dp[l][k]) temp = ans[l][k];Elsetemp = ans[r-(1<<K) +1][K];returnMake_pair (MAXN, temp);}intMain () { while( ~scanf("%d%d", &n, &m)) { for(inti =1; I <= N; i++)scanf("%d", &a[i]); Make (); while(m--) {intL,r;scanf("%d%d", &l, &r); PII temp = Query (l, R);intx = Temp.second;//cout << x << "<< lef[x] <<" << rig[x] << Endl; if(x-lef[x]+1<= L && x+rig[x]-1>= R)puts("Yes");Else puts("No"); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 279C C. Ladder (rmq+ pretreatment)