Luogu: P3865 [Template] ST table (popularization/improvement -, binary stack and ST table)

topic:

Insert picture description here

Analysis: After thinking about it, it seems that there was a small question about this question before. At that time, it seemed to be a multiplier or something, anyway, a multiple of 2 was used. There is also dp.

Well, the same is true in the solution.

Insert picture description here

The rest are written by those who try first.

No, after flipping the previous one, it really was done.

Previous blog

I thought about it a little bit, why j first and then I:

Insert picture description here

Value thinking is indeed like this.

Code:

#include<bits/stdc++.h>
using namespace std;
int A[100005];
int m,n;
int A1[100005][100];
void f(int x1,int x2)
{
    
    
 int k=0;
 while(1)
 {
    
    
  if((1<<(k+1)) > x2-x1+1 ) break;
  k++;
 }
 cout<<max(A1[x1][k],A1[x2-(1<<k)+1][k])<<endl;
}
int main()
{
    
    
 cin>>m>>n;
 for(int i=1;i<=m;i++) cin>>A[i];
 for(int i=1;i<=m;i++) A1[i][0]=A[i];
 for(int j=1;(1<<j)<=m;j++)
 for(int i=1;i+(1<<j)-1<=m;i++)
 A1[i][j]=max(A1[i][j-1],A1[i+(1<<(j-1))][j-1]);
 for(int i=1;i<=n;i++)
 {
    
    
  int a,b; cin>>a>>b;
  f(a,b);
 }
} 

T is just because the input given by the title is useless.

Guess you like

Origin blog.csdn.net/weixin_42721412/article/details/108555694