忠诚

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const long long  maxn=5e5+5;

long long  a[maxn];

struct Segment{
    
    long long  l,r;
    long long  sum;
    long long  tag;
    Segment *lef,*rig;
    
    Segment(const long long  L,const long long  R )
    {
        l=L;r=R;
        tag=0;
        if(l != r)
        {
            int mid=l+r>>1;//这nm对了就离谱 
            lef=new Segment(l,mid);
            rig=new Segment(mid+1,r);
            sum=min(lef->sum,rig->sum);
            tag=0;
        }
        else{
            lef=rig=NULL;
            sum=a[l];
        }
    }
/*    
    inline void maketag(){
        sum=(r-l+1)-sum;
        tag^=1;
    }
*/
/*    
    void spread()
    {
//        if(lef == NULL) lef=new Segment(l,mid);
//        if(rig == NULL) rig =new Segment(mid+1,r);
        if(tag==0) return;
        else {
            lef->maketag();
            rig->maketag();
            tag=0;
        }
    }    
*/
    inline bool Out(const long long  L,const long long  R) { return (R<l || r<L ) ;}

/*    void change(long long  L,long long  R)
    {
        if(L<=l && r<=R){
            maketag();
        }
        else {
            if(Out(L,R)) return ;
            else{
            spread();
            lef->change(L,R);
            rig->change(L,R);
            sum=lef->sum+rig->sum;
            } 
            
        }
    }
    
*/    
    long long  ask(long long  L,long long  R)
    {
        if(L<=l && r<=R) return sum;
        else {
            if(Out(L,R)) return 1<<30;
            else{
            return  min(lef->ask(L,R),rig->ask(L,R));
            
            } 
            
        }
    }
    
};

Segment *root;

int  main(void)
{
    long long  n,m,q;
    
    ios_base::sync_with_stdio(false);
    cout.tie(NULL);
    cin.tie(NULL);
    
    cin>>n>>m;
    
    for(int i=1;i<=n;i++)
    cin>>a[i];
    
    root=new Segment(1,n);
    
    for(long long  i=1;i<=m;i++)
    {
        long long  x,y,z;
        cin>>x>>y;
    
        cout<<root->ask(x,y)<<" ";
    }
    return 0;
}

来自lsq's code

posted @ 2020-07-05 15:50  头顶凉风  阅读(100)  评论(0编辑  收藏  举报