Codeforces 279C C. Ladder (rmq+ pretreatment)

Source: Internet
Author: User

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&LT;&LT;J) <= N; J + +) for(inti =1; i+ (1&LT;&LT;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&LT;&LT;K) +1][k]);if(MAXN = = Dp[l][k]) temp = ans[l][k];Elsetemp = ans[r-(1&LT;&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.