[HDOJ1823] Luck and Love(线段树,树套树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1823

体重就一位小数,可以扩展10倍更新。

坑在了更新人的时候,不应该直接更新,而应该求max,竟然在这里犯二。。

外层更新的时候,线段树的每一棵字数都要在判return之前update内层的树。相当于及时更新了吧。

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define lrt rt << 1
  5 #define rrt rt << 1 | 1
  6 const double EXP = 1e-9;
  7 const int maxn = 210;
  8 const int maxm = 2020;
  9 
 10 int m;
 11 char op[3];
 12 
 13 typedef struct NodeY {
 14     double val;
 15 }NodeY;
 16 
 17 typedef struct NodeX {
 18     NodeY segY[maxm<<2];
 19 
 20     void pushup(int rt) {
 21         segY[rt].val = max(segY[lrt].val, segY[rrt].val);
 22     }
 23 
 24     void build(int l, int r, int rt) {
 25         segY[rt].val = -1;
 26         if(l == r) return;
 27         int mid = (l + r) >> 1;
 28         build(l, mid, lrt);
 29         build(mid+1, r, rrt);
 30     }
 31 
 32     void update(int p, double val, int l, int r, int rt) {
 33         if(l == r) {
 34             segY[rt].val = max(segY[rt].val, val);
 35             return;
 36         }
 37         int mid = (l + r) >> 1;
 38         if(p <= mid) update(p, val, l, mid, lrt);
 39         else update(p, val, mid+1, r, rrt);
 40         pushup(rt);
 41     }
 42 
 43     double query(int L, int R, int l, int r, int rt) {
 44         if(L <= l && r <= R) return segY[rt].val;
 45         int mid = (l + r) >> 1;
 46         double ret = -1;
 47         if(L <= mid) ret = max(ret, query(L, R, l, mid, lrt));
 48         if(mid < R) ret = max(ret , query(L, R, mid+1, r, rrt));
 49         return ret;
 50     }
 51 }NodeX;
 52 
 53 NodeX segX[maxn<<2];
 54 
 55 void build(int l, int r, int rt) {
 56     segX[rt].build(1, 1000, 1);
 57     if(l == r) return;
 58     int mid = (l + r) >> 1;
 59     build(l, mid, lrt);
 60     build(mid+1, r, rrt);
 61 }
 62 
 63 void update(int x, int y, double val, int l, int r, int rt) {
 64     segX[rt].update(y, val, 1, 1000, 1);
 65     if(l == r) return;
 66     int mid = (l + r) >> 1;
 67     if(x <= mid) update(x, y, val, l, mid, lrt);
 68     else update(x, y, val, mid+1, r, rrt);
 69 }
 70 
 71 double query(int yl, int yr, int L, int R, int l, int r, int rt) {
 72     if(L <= l && r <= R) return segX[rt].query(yl, yr, 1, 1000, 1);
 73     double ret = -1;
 74     int mid = (l + r) >> 1;
 75     if(L <= mid) ret = max(ret, query(yl, yr, L, R, l, mid, lrt));
 76     if(mid < R) ret = max(ret, query(yl, yr, L, R, mid+1, r, rrt));
 77     return ret;
 78 }
 79 
 80 int main() {
 81     // freopen("in", "r", stdin);
 82     int h, h1;
 83     double aa, ll;
 84     while(~scanf("%d", &m) && m) {
 85         build(1, 200, 1);
 86         while(m--) {
 87             scanf("%s", op);
 88             if(op[0] == 'I') {
 89                 scanf("%d%lf%lf",&h,&aa,&ll);
 90                 int a = int(aa * 10.0 + EXP);
 91                 update(h, a, ll, 1, 200, 1);
 92             }
 93             else {
 94                 scanf("%d%d%lf%lf",&h,&h1,&aa,&ll);
 95                 int a = int(aa * 10.0 + EXP);
 96                 int b = int(ll * 10.0 + EXP);
 97                 if(h > h1) swap(h, h1);
 98                 if(a > b) swap(a, b);
 99                 double ret = query(a, b, h, h1, 1, 200, 1);
100                 if(ret == -1.0) puts("-1");
101                 else printf("%.1f\n", ret);
102             }
103         }
104     }
105     return 0;
106 }

 

posted @ 2017-04-30 23:28  Kirai  阅读(128)  评论(0编辑  收藏  举报