最基础二维线段树 hdu 1823 (简单)

刚刚看到二维线段树,吓到了,,还以为很难的类。。   刚刚入门的话,其实很简单的啦,之后就不知道了。

其实就是在原来的一维线段树上面再建立一维就ok了。。。  但是我在想的是  现在是因为数据不够大,要是大了怎么办,哎,还没做到那样的题,先不管了

因为每个节点都只用记录最大值。。  仔细看代码吧。。  我已经尽力调到好看了。。。

View Code
  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<string.h>
  5 #include<stdlib.h>
  6 using std::swap;
  7 const int N = 111;

  8 const int M = 1111;
  9 struct parent
 10 {
 11     double son[M<<2];
 12 } tre[N<<2];
 13 double Max(double a,double b)
 14 {
 15     return a>b?a:b;
 16 }
 17 int ll,rr;
 18 void update_s(int s,int t,int l,int r,int L,double val)
 19 {
 20     if(l==r)
 21     {
 22         tre[s].son[t]=Max(val,tre[s].son[t]);
 23         return ;
 24     }
 25     int m=(l+r)>>1;
 26     if(L<=m)update_s(s,t<<1,l,m,L,val);
 27     else update_s(s,t<<1|1,m+1,r,L,val);
 28     tre[s].son[t]=Max(tre[s].son[t<<1],tre[s].son[t<<1|1]);
 29 }
 30 void update_p(int t,int u,int l,int r,double val)
 31 {
 32     update_s(t,1,0,1000,ll,val);
 33     if(l==r)
 34     {
 35         update_s(t,1,0,1000,ll,val);
 36         return ;
 37     }
 38     int m=(r+l)>>1;
 39     if(u<=m)update_p(t<<1,u,l,m,val);
 40     else update_p(t<<1|1,u,m+1,r,val);
 41 }
 42 double query_s(int s,int t,int l,int r,int L,int R)
 43 {
 44     if(L<=l&&r<=R)
 45     {
 46         return tre[s].son[t];
 47     }
 48     int m=(r+l)>>1;
 49     double ans=(-1)*1.0;
 50     if(L<=m)ans=Max(ans,query_s(s,t<<1,l,m,L,R));
 51     if(R>m)ans=Max(ans,query_s(s,t<<1|1,m+1,r,L,R));
 52     return ans;
 53 }
 54 double query_p(int t,int l,int r,int L,int R)
 55 {
 56     if(L<=l&&r<=R)
 57     {
 58         return query_s(t,1,0,1000,ll,rr);
 59     }
 60     int m=(l+r)>>1;
 61     double ans=(-1)*1.0;
 62     if(L<=m)ans=Max(ans,query_p(t<<1,l,m,L,R));
 63     if(R>m)ans=Max(ans,query_p(t<<1|1,m+1,r,L,R));
 64     return ans;
 65 }
 66 void build_s(int s,int t,int l,int r)
 67 {
 68     tre[s].son[t]=(-1)*1.0;
 69     if(l==r)
 70     {
 71         tre[s].son[t]=(-1)*1.0;
 72         return ;
 73     }
 74     int m=(l+r)>>1;
 75     build_s(s,t<<1,l,m);
 76     build_s(s,t<<1|1,m+1,r);
 77 }
 78 void build_p(int t,int l,int r)
 79 {
 80     build_s(t,1,0,1000);
 81     if(l==r)
 82     {
 83         build_s(t,1,0,1000);
 84         return ;
 85     }
 86     int m=(l+r)>>1;
 87     build_p(t<<1,l,m);
 88     build_p(t<<1|1,m+1,r);
 89 }
 90 int main()
 91 {
 92     int n;
 93     int H1,H2;
 94     double A1,A2,val;
 95     char temp[5];
 96     while(scanf("%d",&n)&&n)
 97     {
 98         build_p(1,100,200);
 99         for(int i=0; i<n; i++)
100         {
101             scanf("%s",temp);
102             if(temp[0]=='I')
103             {
104                 scanf("%d",&H1);
105                 scanf("%lf%lf",&A1,&val);
106                 ll=int(A1*10);
107                 update_p(1,H1,100,200,val);
108             }
109             else
110             {
111                 scanf("%d%d",&H1,&H2);
112                 if(H1>H2)swap(H1,H2);
113                 scanf("%lf%lf",&A1,&A2);
114                 ll=int(A1*10);
115                 rr=int(A2*10);
116                 if(ll>rr)swap(ll,rr);
117                 double ans=query_p(1,100,200,H1,H2);
118                 if(ans<0.0)printf("-1\n");
119                 else
120                     printf("%.1lf\n",query_p(1,100,200,H1,H2));
121             }
122         }
123     }
124     return 0;
125 }

 

 

转载于:https://www.cnblogs.com/nuoyan2010/archive/2012/11/03/2752869.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值