zoukankan      html  css  js  c++  java
  • 最基础二维线段树 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 }
  • 相关阅读:
    字符串函数---atof()函数具体解释及实现(完整版)
    curl的简单使用
    [7] 算法之路
    springMVC3.0(文件上传,@RequestMapping加參数,@SessionAttributes,@ModelAttribute,转发,重定向,数值获取,传參,ajax,拦截器)
    hdu 1754 I Hate It 线段树 点改动
    经典的7种排序算法 原理C++实现
    自己定义View实现水平滚动控件
    centos编译ffmpeg x264
    工作脚本处理文本
    A*寻路算法
  • 原文地址:https://www.cnblogs.com/nuoyan2010/p/2752869.html
Copyright © 2011-2022 走看看