标签:
题目链接:http://hihocoder.com/problemset/problem/1299
线段树,按照t为下标去更新v,更新的时候要保留最大的那个。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 //kirai²»ÊÇɳ²è£¬²»»áÍü¼ÇÐÞ¸Ämaxn 23 #define lson l, m, rt << 1 24 #define rson m + 1, r, rt << 1 | 1 25 typedef long long ll; 26 typedef struct Node { 27 int t, v; 28 }Node; 29 const int maxn = 100010; 30 int n, m; 31 int sum[maxn<<2]; 32 33 void pushUP(int rt) { 34 //modify 35 sum[rt] = max(sum[rt<<1], sum[rt<<1|1]); 36 } 37 38 void build(int l, int r, int rt) { 39 if(l == r) { 40 sum[rt] = -1; 41 return; 42 } 43 int m = (l + r) >> 1; 44 build(lson); 45 build(rson); 46 pushUP(rt); 47 } 48 49 void update(int p, int add, int l, int r, int rt) { 50 if(l == r) { 51 sum[rt] = max(sum[rt], add); 52 return; 53 } 54 int m = (l + r) >> 1; 55 if(p <= m) { 56 update(p, add, lson); 57 } 58 else { 59 update(p, add, rson); 60 } 61 pushUP(rt); 62 } 63 64 int query(int L, int R, int l, int r, int rt) { 65 if(L <= l && r <= R) { 66 return sum[rt]; 67 } 68 int m = (l + r) >> 1; 69 int ret = 0; 70 if(L <= m) { 71 //modify 72 ret = max(ret, query(L, R, lson)); 73 } 74 if(R > m) { 75 //modify 76 ret = max(ret, query(L, R, rson)); 77 } 78 return ret; 79 } 80 81 int main() { 82 // freopen("in", "r", stdin); 83 while(~scanf("%d%d", &n, &m)) { 84 int t, v; 85 build(1, n, 1); 86 for(int i = 0; i < n; i++) { 87 scanf("%d%d", &t, &v); 88 update(t, v, 1, n, 1); 89 } 90 int a, b; 91 while(m--) { 92 scanf("%d%d", &a, &b); 93 int ans = query(a, b, 1, n, 1); 94 if(ans <= 0) printf("None\n"); 95 else printf("%d\n", ans); 96 } 97 } 98 return 0; 99 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/5467545.html