線段樹維護區間最大值最小值


poj3264

對於每天擠奶,農民約翰的ñ奶牛(1≤ ñ ≤50,000)總是以相同的順序排隊。有一天,農夫約翰決定與一些母牛一起組織一場極限飛盤比賽。為簡單起見,他將從擠奶陣容中選擇一頭連續的奶牛來玩游戲。但是,為了讓所有的奶牛玩得開心,它們的身高不應相差太大。

農夫約翰已經做過的Q(1≤ Q ≤200,000)奶牛的潛在群體和他們的高度(1個≤ 高度 ≤1,000,000)。對於每個組,他都需要您的幫助來確定該組中最矮和最高的母牛之間的身高差異。

輸入項

Line 1: Two space-separated integers,  N and  Q.
Lines 2.. N+1: Line  i+1 contains a single integer that is the height of cow  i
Lines  N+2.. N+ Q+1: Two integers  A and  B (1 ≤  A ≤  B ≤  N), representing the range of cows from  A to  B inclusive.

Output

Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

題目大意就是:

就是給你一個l,r讓你求區間內的最大值減去最小值

#include <iostream>
#include <cstdio>
#include  <cstring>
#include  <algorithm>
using namespace std;
typedef long long ll;
template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch>'9'||ch<'0')){
        ch=getchar();
    }
    if(ch=='-'){
        fh=-1;ch=getchar();
    }else fh=1;
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
    }
    x*=fh;
}
inline char read1()
{
    register char ch=getchar();
    while(ch<'A'||ch>'M')ch=getchar();
    return ch;
}
const int maxn=1e6+100;
struct node{
    int l,r,ma,mi;
}t[maxn];
int a[maxn];
int n,q,x,y,ans1,ans2;
void build(int p,int l,int r){
    t[p].l=l,t[p].r=r;
    if(l==r){
        t[p].ma=t[p].mi=a[l];
        return ;
    }
    int mid=(t[p].l+t[p].r)>>1;    
    build(p<<1,l,mid);
    build((p<<1)+1,mid+1,r);
    t[p].ma=max(t[p<<1].ma,t[(p<<1)+1].ma);
    t[p].mi=min(t[p<<1].mi,t[(p<<1)+1].mi);
}
void query(int p,int l,int r){
    if(l<=t[p].l&&r>=t[p].r){
        ans1=max(ans1,t[p].ma);
        ans2=min(ans2,t[p].mi);
        return ;
    }
    int mid=(t[p].l+t[p].r)>>1;
    if(l<=mid){
        query(p<<1,l,r);
    }
    if(r>mid){
        query((p<<1)+1,l,r);
    }
}
int main(){
    read(n),read(q);
    for(int i=1;i<=n;i++){
        read(a[i]);
    }
    build(1,1,n);
    while(q--){
        read(x),read(y);
        ans1=0,ans2=0x3f3f3f3f;
        query(1,x,y);
        printf("%d\n",ans1-ans2); 
    }
} 

 

 

#include <iostream>
#include <cstdio>
#include  <cstring>
#include  <algorithm>
using namespace std;
typedef long long ll;
template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch>'9'||ch<'0')){
        ch=getchar();
    }
    if(ch=='-'){
        fh=-1;ch=getchar();
    }else fh=1;
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
    }
    x*=fh;
}
inline char read1()
{
    register char ch=getchar();
    while(ch<'A'||ch>'M')ch=getchar();
    return ch;
}
const int maxn=1e6+100; 
int z[maxn];
struct node{
    int mi,ma;
}a[maxn];
void build(int p,int l,int r){
    if(l==r){
        a[p].ma=z[l],a[p].mi=z[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(p<<1,l,mid);
    build((p<<1)+1,mid+1,r);
    a[p].mi=min(a[p<<1].mi,a[(p<<1)+1].mi);
    a[p].ma=max(a[p<<1].ma,a[(p<<1)+1].ma);
}
int find1(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y){
        return a[p].ma;
    }
    int mid=(l+r)>>1;
    if(y<=mid){
        return find1(p<<1,l,mid,x,y);
    }
    else if(x>mid){
        return find1((p<<1)+1,mid+1,r,x,y);
    }
    else return max(find1(p<<1,l,mid,x,mid),find1((p<<1)+1,mid+1,r,mid+1,y));
}
int find2(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y){
        return a[p].mi;
    }
    int mid=(l+r)>>1;
    if(y<=mid){
        return find2(p<<1,l,mid,x,y);
    }
    else if(x>mid){
        return find2((p<<1)+1,mid+1,r,x,y);
    }
    else return min(find2(p<<1,l,mid,x,mid),find2((p<<1)+1,mid+1,r,mid+1,y));
}
int main(){
    int n,q;
    int x,y;
    while(~scanf("%d%d",&n,&q)){
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++){
            scanf("%d",&z[i]);
        }
        build(1,1,n);
        while(q--){
            scanf("%d%d",&x,&y);
            printf("%d\n",find1(1,1,n,x,y)-find2(1,1,n,x,y));
        }
    }
    return 0; 
} 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM