skkyk: problem solution Luo Gu P3865 [[template]] ST table

I will not watch ST

Chi pushing to this problem
was found in the label actually have tree line. . ?
So hastily came out of the tree line
we all know, is the tree line query log (n) of
the title " Please note that the maximum time limit data only 0.8s, not low-intensity data, make sure that every time you query complexity is O ( 1) "
and then hastily finished the code actually the AC. . exm? ?
But also the slowest 400ms

Good water data

Well, not much said on the code
first, data storage, are left child, right child node, maxx store the maximum value of the current node

struct node{
    int left,right,maxx;
}tree[100000*4+10];

Achievements, and the same routine

void build(int index,int l,int r)
{
    tree[index].left=l,tree[index].right=r;
    if(l==r)
    {
        int x=read();
        tree[index].maxx=x;
        return ;
    }
    int mid=(l+r)>>1;
    build(index<<1,l,mid),build(index<<1|1,mid+1,r);
    tree[index].maxx=max(tree[index<<1].maxx,tree[index<<1|1].maxx);
}

Range queries, the maximum value of each record and intersect the target range of the interval

int intervalask(int index,int l,int r)
{
    if(tree[index].left>=l&&tree[index].right<=r)   
        return tree[index].maxx;
    int tempmax=-0x7fffffff;
    if(tree[index<<1].right>=l)
        tempmax=max(intervalask(index<<1,l,r),tempmax);
    if(tree[index<<1|1].left<=r)
        tempmax=max(intervalask(index<<1|1,l,r),tempmax);
    return tempmax;
}

AC Code

#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
struct node{
    int left,right,maxx;
}tree[100000*4+10];
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-f;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return f*x;
}

void build(int index,int l,int r)
{
    tree[index].left=l,tree[index].right=r;
    if(l==r)
    {
        int x=read();
        tree[index].maxx=x;
        return ;
    }
    int mid=(l+r)>>1;
    build(index<<1,l,mid),build(index<<1|1,mid+1,r);
    tree[index].maxx=max(tree[index<<1].maxx,tree[index<<1|1].maxx);
}

int intervalask(int index,int l,int r)
{
    if(tree[index].left>=l&&tree[index].right<=r)   
        return tree[index].maxx;
    int tempmax=-0x7fffffff;
    if(tree[index<<1].right>=l)
        tempmax=max(intervalask(index<<1,l,r),tempmax);
    if(tree[index<<1|1].left<=r)
        tempmax=max(intervalask(index<<1|1,l,r),tempmax);
    return tempmax;
}

int main()
{
    n=read(),m=read();
    build(1,1,n);
    for(register int i=1,l,r;i<=m;i++)
    {
        l=read(),r=read();
        printf("%d\n",intervalask(1,l,r));
    }
}

So much to learn and a half, if there are places we can not understand private letters

Guess you like

Origin www.cnblogs.com/skkyk/p/11058716.html