hihoCoder挑战赛20 (区间最值)

题目地址:http://hihocoder.com/contest/challenge20/problem/1

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 100010;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int value[maxn<<2];
void pushup(int rt)
{
    value[rt] = max(value[rt<<1],value[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l == r)
    {
        value[rt] = 0;
        return;
    }
    int m = (r + l )>> 1;
    build(lson);
    build(rson);
    pushup(rt);
}
void updata(int p,int sc,int l,int r,int rt)
{
    if(l == r)
    {
        value[rt] = max(value[rt],sc);
        return;
    }
    int m = (l + r) >> 1;
    if(p <= m) updata(p,sc,lson);
    else updata(p,sc,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(l >= L && r <= R) return value[rt];
    int m = (l + r) >> 1;
    int ans = 0;
    if(L <= m) ans = max(ans,query(L,R,lson));
    if(R > m) ans = max(ans,query(L,R,rson));
    return ans;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        build(1,maxn,1);
        int loc,price;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&loc,&price);
            updata(loc,price,1,maxn,1);
        }
        int a,b;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&a,&b);
            int x = query(a,b,1,maxn,1);
            if(x != 0)
                printf("%d\n",x);
            else
                printf("None\n");
        }
    }
    return 0;
}


你可能感兴趣的:(hihoCoder挑战赛20 (区间最值))