POJ 3264 Balanced Lineup

需求:单点更新,区间最值。

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

const double  eps = 1e-8;
const double   pi = acos(-1.0);
const int     inf = 0x7f7f7f7f;

#define loop(a,n)                            \
    for(int i=0;n>i;i++)                     \
        cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m)                         \
    for(int i=0;n>i;i++)                     \
        for(int j=0;m>j;j++)                 \
            cout<<a[i][j]<<(j!=m-1?' ':'\n')

#define   at(a,i) ((a)&(1<<(i)))
#define   nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))

#define gret(a,b) (((a)-(b))>eps)
#define less(a,b) (((b)-(a))>eps)
#define greq(a,b) (((a)-(b))>-eps)
#define leeq(a,b) (((b)-(a))>-eps)
#define equl(a,b) (fabs((a)-(b))<eps)

#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (gret(a,b)?(a):(b))
#define fmin(a,b) (less(a,b)?(a):(b))

const int MAXV = 50002;

namespace ST
{
    int left,right,up[MAXV<<2],down[MAXV<<2];

    #define lc(i) ((i)<<1)
    #define rc(i) ((i)<<1|1)

    void clear(int l,int r)
    {
        left=l;
        right=r;
        memset(up,0,sizeof(up));
        memset(down,inf,sizeof(up));
    }
    void change(int x,int w,int l=left,int r=right,int i=1)
    {
        if(l==x&&x==r)
        {
            up[i]=max(up[i],w);
            down[i]=min(down[i],w);
        }
        else
        {
            if(x<=(l+r)/2) change(x,w,l,(l+r)/2,lc(i));
            if((l+r)/2<x) change(x,w,(l+r)/2+1,r,rc(i));

            up[i]=max(up[lc(i)],up[rc(i)]);
            down[i]=min(down[lc(i)],down[rc(i)]);
        }
    }
    int query_up(int x,int y,int l=left,int r=right,int i=1)
    {
        if(x<=l&&r<=y) return up[i];
        else
        {
            int cnt=0;
            if(x<=(l+r)/2) cnt=max(cnt,query_up(x,y,l,(l+r)/2,lc(i)));
            if((l+r)/2<y) cnt=max(cnt,query_up(x,y,(l+r)/2+1,r,rc(i)));

            return cnt;
        }
    }
    int query_down(int x,int y,int l=left,int r=right,int i=1)
    {
        if(x<=l&&r<=y) return down[i];
        else
        {
            int cnt=inf;
            if(x<=(l+r)/2) cnt=min(cnt,query_down(x,y,l,(l+r)/2,lc(i)));
            if((l+r)/2<y) cnt=min(cnt,query_down(x,y,(l+r)/2+1,r,rc(i)));

            return cnt;
        }
    }
    int query(int x,int y)
    {
        return query_up(x,y)-query_down(x,y);
    }
}

int n,m,w,x,y;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("Balanced Lineup.txt","r",stdin);
    #endif

    while(scanf("%d %d",&n,&m)!=EOF)
    {
        ST::clear(1,n);
        for(int i=1;n>=i;i++)
        {
            scanf("%d",&w);
            ST::change(i,w);
        }
        for(int i=1;m>=i;i++)
        {
            scanf("%d %d",&x,&y);
            printf("%d\n",ST::query(x,y));
        }
    }

    return 0;
}

你可能感兴趣的:(POJ 3264 Balanced Lineup)