zoukankan      html  css  js  c++  java
  • hdu 1823 Luck and Love

    思路:二维线段树求区域最值。值得注意的是在进行Q操作时可能H1>H2,A1>A2.所以要swap.

    View Code
      1 #include <cstdio>
      2 #include <algorithm>
      3 using namespace std;
      4 #define lson l,m,rt<<1
      5 #define rson m+1,r,rt<<1|1
      6 #define maxn1 1002
      7 #define maxn2 102
      8 struct node
      9 {
     10     int subsetree[maxn1*3];
     11 }setree[maxn2*3];
     12 void subbuild(int num,int l,int r,int rt)
     13 {
     14     setree[num].subsetree[rt]=-1;
     15     if(l==r)
     16     return;
     17     int m=(l+r)>>1;
     18     subbuild(num,lson);
     19     subbuild(num,rson);
     20 }
     21 void build(int l,int r,int rt)
     22 {
     23     subbuild(rt,0,1000,1);
     24     if(l==r)
     25     return;
     26     int m=(l+r)>>1;
     27     build(lson);
     28     build(rson);
     29 }
     30 void subupdate(int num,int l,int r,int rt,int l2,int c)
     31 {
     32     if(l==r){
     33         setree[num].subsetree[rt]=max(setree[num].subsetree[rt],c);
     34         return;
     35     }
     36     int m=(l+r)>>1;
     37     if(l2<=m)
     38     subupdate(num,lson,l2,c);
     39     else if(l2>m)
     40     subupdate(num,rson,l2,c);
     41     setree[num].subsetree[rt]=max(setree[num].subsetree[rt<<1],setree[num].subsetree[rt<<1|1]);
     42 }
     43 void update(int l,int r,int rt,int l1,int l2,int c)
     44 {
     45     subupdate(rt,0,1000,1,l2,c);
     46     if(l==r)
     47         return;
     48     int m=(l+r)>>1;
     49     if(l1<=m)
     50     update(lson,l1,l2,c);
     51     else if(l1>m)
     52     update(rson,l1,l2,c);
     53 }
     54 int subquery(int num,int l,int r,int rt,int l2,int r2)
     55 {
     56     if(l2<=l&&r<=r2)
     57     return setree[num].subsetree[rt];
     58     int m=(l+r)>>1;
     59     int ans=-1;
     60     if(l2<=m)
     61     ans=max(ans,subquery(num,lson,l2,r2));
     62     if(r2>m)
     63     ans=max(ans,subquery(num,rson,l2,r2));
     64     return ans;
     65 }
     66 int query(int l,int r,int rt,int l1,int r1,int l2,int r2)
     67 {
     68     if(l1<=l&&r<=r1)
     69     return subquery(rt,0,1000,1,l2,r2);
     70     int m=(l+r)>>1;
     71     int ans=-1;
     72     if(l1<=m)
     73     ans=max(ans,query(lson,l1,r1,l2,r2));
     74     if(r1>m)
     75     ans=max(ans,query(rson,l1,r1,l2,r2));
     76     return ans;
     77 }
     78 int main()
     79 {
     80     int n;
     81     while(scanf("%d",&n)&&n){
     82         build(100,200,1);
     83         while(n--){
     84             char s[5];
     85             scanf("%s",s);
     86             if(s[0]=='I'){
     87                 double a,l; 
     88                 int l1;
     89                 scanf("%d%lf%lf",&l1,&a,&l);
     90                 int l2=int(a*10),c=int(l*10);
     91                 update(100,200,1,l1,l2,c);
     92             }
     93             else if(s[0]=='Q'){
     94                 double l2,r2;
     95                 int l1,r1;
     96                 scanf("%d%d%lf%lf",&l1,&r1,&l2,&r2);
     97                 int ll2=int(10*l2),rr2=int(10*r2);
     98                 if(l1>r1)swap(l1,r1);
     99                 if(ll2>rr2)swap(ll2,rr2);
    100                 int ans=query(100,200,1,l1,r1,ll2,rr2);
    101                 if(ans==-1)
    102                 printf("-1\n");
    103                 else
    104                 printf("%.1lf\n",ans/10.0);
    105             }
    106         }
    107     }
    108     return 0;
    109 }
  • 相关阅读:
    CF-1328 F. Make k Equal
    CF-1328 E. Tree Queries
    USACO_1.1_Greedy_Gift_Givers_(模拟+水题)
    USACO_1.1_Your_Ride_Is_Here_(字符串+水题)
    第3章 文件系统
    BZOJ_1629_[Usaco2007_Demo]_Cow_Acrobats_(贪心)
    BZOJ_1628_[Usaco2007_Demo]_City_skyline_(单调栈)
    BZOJ_1030_[JSOI2007]_文本生成器_(AC自动机+DP)
    BZOJ_1029_[JSOI2007]_建筑抢修_(贪心+优先队列)
    BZOJ_1028_[JSOI2007]_麻将_(模拟+贪心)
  • 原文地址:https://www.cnblogs.com/kim888168/p/2868155.html
Copyright © 2011-2022 走看看