题目链接: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; }