HDU 1823 Luck and Love

Luck and Love

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4575    Accepted Submission(s): 1129


Problem Description
世界上上最远的距离不是相隔天涯海角
而是我在你面前
可你却不知道我爱你
                ―― 张小娴

前段日子,枫冰叶子给Wiskey做了个征婚启事,聘礼达到500万哦,天哪,可是天文数字了啊,不知多少MM蜂拥而至,顿时万人空巷,连扫地的大妈都来凑热闹来了。―_―|||
由于人数太多,Wiskey实在忙不过来,就把统计的事情全交给了枫冰叶子,自己跑回家休息去了。这可够枫冰叶子忙的了,他要处理的有两类事情,一是得接受MM的报名,二是要帮Wiskey查找符合要求的MM中缘分最高值。
 
Input
本题有多个测试数据,第一个数字M,表示接下来有连续的M个操作,当M=0时处理中止。
接下来是一个操作符C。
当操作符为‘I’时,表示有一个MM报名,后面接着一个整数,H表示身高,两个浮点数,A表示活泼度,L表示缘分值。 (100<=H<=200, 0.0<=A,L<=100.0)
当操作符为‘Q’时,后面接着四个浮点数,H1,H2表示身高区间,A1,A2表示活泼度区间,输出符合身高和活泼度要求的MM中的缘分最高值。 (100<=H1,H2<=200, 0.0<=A1,A2<=100.0)
所有输入的浮点数,均只有一位小数。
 
Output
对于每一次询问操作,在一行里面输出缘分最高值,保留一位小数。
对查找不到的询问,输出-1。
 
Sample Input
8
I 160 50.5 60.0
I 165 30.0 80.5
I 166 10.0 50.0
I 170 80.5 77.5
Q 150 166 10.0 60.0
Q 166 177 10.0 50.0
I 166 40.0 99.9
Q 166 177 10.0 50.0 0
 
Sample Output
80.5
50.0
99.9
 
Author
威士忌
 
Source
 
Recommend
威士忌
 
思路:第一个二维线段树,接触线段树四天了,这个题最难过,我使用树套树的方式实现二维线段树;
就是高度为一颗线段树,然后该线段树的每一个节点都套一颗表示活跃度的线段树,二维线段树的节点
定义为:
struct son_Node
{
    int ld,rd;
    double max_chance;
};
struct mon_Node
{
    int leftd,rightd;
    son_Node Tree[4040];
}TTree[450];
在建树,更新,查询时都要对母树和子树进行操作
这个题还应该注意的是H1 和 H2 以及 A1 和 A2
的大小未定,在这里我RE了半个点啊
 
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
int t_time;
char str;
int high;
double active,chance;
int left_high,right_high;
double first_active,end_active;
int left_active,right_active;
double the_last_chance;
struct son_Node
{
    int ld,rd;
    double max_chance;
};
struct mon_Node
{
    int leftd,rightd;
    son_Node Tree[4040];
}TTree[450];
void son_buildtree(int son_position,int mon_position,int begin,int end)
{
    TTree[mon_position].Tree[son_position].ld = begin;
    TTree[mon_position].Tree[son_position].rd = end;
    TTree[mon_position].Tree[son_position].max_chance = 0.0;
    if(TTree[mon_position].Tree[son_position].ld == TTree[mon_position].Tree[son_position].rd)
        return ;
    son_buildtree(2 * son_position,mon_position,begin,(begin + end) / 2);
    son_buildtree(2 * son_position + 1,mon_position,(begin + end) / 2 + 1,end);
}
void mon_buildtree(int mon_position,int highleft,int highright,int activeleft,int activeright)
{
    TTree[mon_position].leftd = highleft;
    TTree[mon_position].rightd = highright;
    son_buildtree(1,mon_position,activeleft,activeright);
    if(highleft == highright)
        return ;
    mon_buildtree(2 * mon_position,highleft,(highleft + highright) / 2,activeleft,activeright);
    mon_buildtree(2 * mon_position + 1,(highleft + highright) / 2 + 1,highright,activeleft,activeright);
}
void son_insert(int son_position,int mon_position,int position,double chance)
{
    if(TTree[mon_position].Tree[son_position].ld <= position && TTree[mon_position].Tree[son_position].rd >= position)
    {
        if(chance - TTree[mon_position].Tree[son_position].max_chance > 0.01)
            TTree[mon_position].Tree[son_position].max_chance = chance;
    }
    if(TTree[mon_position].Tree[son_position].ld == TTree[mon_position].Tree[son_position].rd)
       return ;
    int mid = (TTree[mon_position].Tree[son_position].ld + TTree[mon_position].Tree[son_position].rd) / 2;
    if(position <= mid)
       son_insert(2 * son_position,mon_position,position,chance);
    if(position > mid)
       son_insert(2 * son_position + 1,mon_position,position,chance);
}
void mon_insert(int mon_position,int high_position,int active_position,double chance)
{
    if(TTree[mon_position].leftd <= high_position && TTree[mon_position].rightd >= high_position)
    {
        son_insert(1,mon_position,active_position,chance);
    }
    if(TTree[mon_position].leftd == TTree[mon_position].rightd)
        return ;
    int mid = (TTree[mon_position].leftd + TTree[mon_position].rightd) / 2;
    if(high_position <= mid)
       mon_insert(2 * mon_position,high_position,active_position,chance);
    if(high_position > mid)
       mon_insert(2 * mon_position + 1,high_position,active_position,chance);
}
void son_search(int son_position,int mon_position,int begin,int end)
{
    if(TTree[mon_position].Tree[son_position].ld == begin && TTree[mon_position].Tree[son_position].rd == end)
    {
        if(TTree[mon_position].Tree[son_position].max_chance - the_last_chance > 0.01)
              the_last_chance = TTree[mon_position].Tree[son_position].max_chance;
        return ;
    }
    int mid = (TTree[mon_position].Tree[son_position].ld + TTree[mon_position].Tree[son_position].rd) / 2;
    if(end <= mid)
    {
         son_search(2 * son_position,mon_position,begin,end);
    }
    else if(begin > mid)
    {
         son_search(2 * son_position + 1,mon_position,begin,end);
    }
    else
    {
         son_search(2 * son_position,mon_position,begin,mid);
         son_search(2 * son_position + 1,mon_position,mid + 1,end);
         return ;
    }
}
void mon_search(int mon_position,int highleft,int highright,int activeleft,int activeright)
{
    if(TTree[mon_position].leftd == highleft && TTree[mon_position].rightd == highright)
    {
         son_search(1,mon_position,activeleft,activeright);
            return ;
    }
    int mid = (TTree[mon_position].leftd + TTree[mon_position].rightd) / 2;
    if(highright <= mid)
    {
         mon_search(2 * mon_position,highleft,highright,activeleft,activeright);
    }
    else if(highleft > mid)
    {
         mon_search(2 * mon_position + 1,highleft,highright,activeleft,activeright);
    }
    else
    {
         mon_search(2 * mon_position,highleft,mid,activeleft,activeright);
         mon_search(2 * mon_position + 1,mid + 1,highright,activeleft,activeright);
        return ;
    }
}
int main()
{
    while(scanf("%d",&t_time),t_time != 0)
    {
        memset(TTree,0,sizeof(TTree));
        mon_buildtree(1,1,110,1,1002);
        for(int i = 1;i <= t_time;i ++)
        {
            scanf(" %c",&str);
            if(str == 'I')
            {
                scanf("%d%lf%lf",&high,&active,&chance);
                high -= 90;
                active *= 10;
                int a = (int)active;
                a = a + 1;
                mon_insert(1,high,a,chance);
            }
            else
            {
                scanf("%d%d%lf%lf",&left_high,&right_high,&first_active,&end_active);
                left_high -= 90;
                right_high -= 90;
                int flag;
                if(left_high > right_high)
                {
                    flag = right_high;
                    right_high = left_high;
                    left_high = flag;
                }
                left_active = (int)10 * first_active + 1;
                right_active = (int)10 * end_active + 1;
                if(left_active > right_active)
                {
                    flag = right_active;
                    right_active = left_active;
                    left_active = flag;
                }
                the_last_chance = 0.0;
                mon_search(1,left_high,right_high,left_active,right_active);
                if(the_last_chance == 0.0)
                    printf("-1
");
                else
                    printf("%.1lf
",the_last_chance);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/GODLIKEING/p/3362092.html