码迷,mamicode.com
首页 > 其他好文 > 详细

hihocoder 1299 打折机票 线段树

时间:2016-05-16 21:39:48      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://hihocoder.com/problemset/problem/1299

code:

//线段树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100005
using namespace std;
int price[maxn];
int Max[maxn];
int segTree[4*maxn];
int maxtime;
void built(int node,int begin,int end)
{
    if(begin == end){
        segTree[node] = price[begin];
    }
    else{
        built(node*2,begin,(begin+end)/2);
        built(node*2+1,(begin+end)/2+1,end);
        segTree[node] = max(segTree[node*2],segTree[node*2 + 1]);
    }
}
int query(int node,int begin,int end,int left,int right)
{
    int p1, p2;
    if (left > end || right < begin)
        return -1;
    if (begin >= left && end <= right)
        return segTree[node];
    p1 = query(2 * node, begin, (begin + end) / 2, left, right);
    p2 = query(2 * node + 1, (begin + end) / 2 + 1, end, left, right);
    if (p1 == -1)
        return p2;
    if (p2 == -1)
        return p1;
    if (p1 >= p2)
        return  p1;
    return  p2;
}
int main()
{
    int n,m;
    memset(price,0,sizeof(price));
    memset(Max,0,sizeof(Max));
    maxtime = 0;
    cin >> n >> m;
    for(int i = 0;i < n;i ++)
    {
        int t,p;
        cin >> t >> p;
        price[t] = max(price[t],p);
        maxtime = max(maxtime,t);
    }
    built(1,1,maxtime);
    while(m --)
    {
        int a,b;
        cin >> a >> b;
        int ans =  query(1,1,maxtime,a,b);
        if(ans == 0)
            cout << "None" << endl;
        else
            cout << ans << endl;
    }
    return 0;
}

 

hihocoder 1299 打折机票 线段树

标签:

原文地址:http://www.cnblogs.com/zhangjialu2015/p/5499345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!