HDU 1832 Luck and Love (二維線段樹)

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   os   sp   資料   div   

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
8I 160 50.5 60.0I 165 30.0 80.5I 166 10.0 50.0I 170 80.5 77.5Q 150 166 10.0 60.0Q 166 177 10.0 50.0I 166 40.0 99.9Q 166 177 10.0 50.00
 


Sample Output
80.550.099.9



裸二維線段樹。

有幾個地方需要注意,查詢可能x1 > x2, y1 > y2,還有更新的時候如果該位置已經有值 不是直接覆蓋而是取max。

(還有一個地方我也沒注意但是AC了,就是一開始不應該置0, 因為題目活了緣分值是可能為0的)


#include <cstdio>#include <cstring>#include <algorithm>#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;int T, h, N = 202, M = 1002, x1, x2, y1, y2, a;double tmpa, ll, tmpy1, tmpy2;char s[3];double mx[222<<2][1111<<2];void updatey(int o, int l, int r, int oo, int ok) {    if(l == r) {        if(ok == -1) mx[oo][o] = max(ll, mx[oo][o]); // 非直接覆蓋,而是取最大值        else mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]);        return;    }    int m = (l+r) >> 1;    if(a <= m) updatey(lson, oo, ok);    else updatey(rson, oo, ok);    mx[oo][o] = max(mx[oo][o<<1], mx[oo][o<<1|1]);}void updatex(int o, int l, int r) {    if(l == r) {        updatey(1, 1, M, o, -1);        return;    }    int m = (l+r) >> 1;    if(h <= m) updatex(lson);    else updatex(rson);    updatey(1, 1, M, o, 1);}double queryy(int o, int l, int r, int oo) {    if(y1 <= l && r <= y2) return mx[oo][o];    int m = (l+r) >> 1;    double ans = -1.0;    if(y1 <= m) ans = queryy(lson, oo);    if(m < y2 ) ans = max(ans, queryy(rson, oo));    return ans;}double queryx(int o, int l, int r) {    if(x1 <= l && r <= x2) return queryy(1, 1, M, o);    int m = (l+r) >> 1;    double ans = -1.0;    if(x1 <= m) ans = queryx(lson);    if(m < x2 ) ans = max(ans, queryx(rson));    return ans;}int main(){    //freopen("in.txt", "r", stdin);    while(~scanf("%d", &T) && T) {        memset(mx, 0, sizeof(mx));        while(T--) {            scanf("%s", s);            if(s[0] == 'I') {                scanf("%d%lf%lf", &h, &tmpa, &ll);                a = (int)(tmpa*10);               // printf("(%d %d) -> %.1lf\n", h, a, ll);                updatex(1, 1, N);            } else {                scanf("%d%d%lf%lf", &x1, &x2, &tmpy1, &tmpy2);                y1 = (int)(tmpy1*10);                y2 = (int)(tmpy2*10);                if(x1 > x2) swap(x1, x2);                if(y1 > y2) swap(y1, y2);  // 開始的時候忽略了。。               // printf("x1 = %d , x2 = %d , y1 = %d , y2 = %d\n", x1, x2, y1, y2);                double ans = queryx(1, 1, N);                if(ans == 0) puts("-1");                else printf("%.1lf\n", ans);            }        }    }    return 0;}




HDU 1832 Luck and Love (二維線段樹)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.