BZOJ1012: [JSOI2008] Maximum number - problem solving

https://www.lydsy.com/JudgeOnline/problem.php?id=1012

https://www.luogu.org/problemnew/show/P1198

Now I ask you to maintain an array and provide the following two operations: 1. Query operation. Syntax: QL Function: Query
the largest number among the last L numbers in the current sequence, and output the value of this number. Restriction: L does not exceed the length of the current sequence. 2. Insert operation. Syntax: An Function: Add n
to t, where t is the answer of the most recent query operation (if the query operation has not been performed, then t=0), and the result is
modulo a fixed constant D, the The resulting answer is inserted at the end of the sequence. Restrictions: n is a non-negative integer and is in the range of long integers. Note: Initially the number column is empty without a
number.

The st table can be turned upside down.

(Of course, you can do balanced tree, dynamic segment tree, block, and choose the easiest one to do.)

Luogu data is disgusting, if you TLE, you might as well compare it with the main function.

#include<cstdio>
#include<cctype>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=4e5+5;
const int B=18;
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
ll dp[N][B+4];
int lg[N];
char s[101];
inline ll qry(int l,int r){
    if(l>r)return 0;
    int h=lg[r-l+1];
    return max(dp[r][h],dp[l+(1<<h)-1][h]);
}
int main(){
    int m=read(),d=read(),n=0;ll t=0,k;
    lg[1]=0;
    while(m--){
    scanf("%s%lld",s,&k);
    if(s[0]=='Q'){
        printf("%lld",t=qry(n-k+1,n));
        putchar(10);
    }
    if(s[0]=='A'){
        k=(k+t)%d;
        dp[++n][0]=k;
        if(n!=1){
        lg[n] =lg[n- 1 ];
        if (( 1 <<lg[n]+ 1 )==n)lg[n]++ ;
        }
        for(int j=1;j<=lg[n];j++){
        if(n-(1<<j)+1<1)break;
        dp[n][j]=max(dp[n][j-1],dp[n-(1<<j-1)][j-1]);
        }
    }
    }
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+ Author of this article: luyouqi233. +

+Welcome to my blog: http://www.cnblogs.com/luyouqi233/  +

+++++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325366100&siteId=291194637