Balanced Lineup(区间更新+查询)

给你一个长度为n的序列a[N] (1 ≤ N ≤ 50000),询问Q(1 ≤ Q ≤ 200000) 次,每次输出【L, R】区间最大值与最小值的差是多少。
so easy~
so easy~
so easy~

Input

多组用例
第一行是两个整数 N,Q
然后是N个数a[i] 保证a[i] 都小于1e9
然后是Q个询问 每次给你L,R 保证(1<=L<=R<= N)

Output

输出每次询问【L, R】区间最大值与最小值的差是多少

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0
#include 
#include 
#include 
#include <string>
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 
#include 
typedef long long ll;
#define pql priority_queue
#define pq priority_queue
#define v vector
#define vl vector
#define lson index<<1, l, mid
#define rson index<<1|1, mid+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define no printf("NO\n");
#define gcd __gcd
#define cn cin>>
#define ct cout<<
#define ed <#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {cin>>a[i] ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define re return ;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"++++++"<#define lc index<<1
#define rc index<<1|1
using namespace std;
const int MX=1000005;
int t,ans,maxn=-MX,minn= MX;
typedef struct node
{
    int val,l,r,mx,mn,lazy,sum;
}node;
node dp[MX];
int a[MX];
void up(int index)
{
    dp[index].mx = max( dp[lc].mx ,dp[rc].mx );
    dp[index].mn = min( dp[lc].mn ,dp[rc].mn );
}
void build(int index,int l,int r)
{
    dp[index].l = l;
    dp[index].r = r;
    if(l==r)
    {
        dp[index].mx = a[l];
        dp[index].mn = a[l];
        re;
    }
    int mid = (l+r)>>1;
    build(lc,l,mid) ;
    build(rc,mid+1,r) ;
    up(index);
}

void query(int index,int l,int r)
{
    //if(dp[root].mx<=minn&&dp[root].mi>=maxn) re;
    if(dp[index].l>=l&&dp[index].r<=r)
    {
        maxn = max(dp[index].mx,maxn);
        minn = min(dp[index].mn,minn);
        re;
    }
    int mid=(dp[index].l+dp[index].r)>>1;
    if(l<=mid)
    {
        query(lc,l,r);
    }
    if(r>mid)
    {
        query(rc,l,r);
    }

}
int main()
{
    TLE;
    int n,k,ll,rr;
    while(cn n>>k)
    {
        rep(1,n)  cn a[i];
        build(1,1,n);
        rep(1,k)
        {
            maxn=-1,minn= 1e9+5;
            cin>>ll>>rr;
            query(1,ll,rr);
            cout<endl;
        }


    }
    ok;
}

 

你可能感兴趣的:(Balanced Lineup(区间更新+查询))