矩形面積交-線段樹

來源:互聯網
上載者:User

hdu - 1255

世事總是如此地無常,很多事情總在意料之外。

比如,這題。

不知道是如何錯的,是因為二分寫得不好?

做完矩形周長並之後,就把這個代碼加了兩個0,就過了。

又是maxn開小了。

下面是正確代碼。

建議: 學矩形周長並、面積交,先學矩形面積並。

Pro: 0Sol:date:*/#include<iostream>#include <cstdio>#include<algorithm>#include <cstring>#define eps 1e-10#define maxn    101000#define lson l,m, rt << 1#define rson m + 1,r, rt << 1 | 1using namespace std;struct Seg{    double l,r,y;    int sign;    Seg(){}    Seg(double ll, double rr, double yy, int ss):l(ll),r(rr),y(yy),sign(ss){}    bool operator < (const Seg &cmp) const{        if(y == cmp.y)  return sign > cmp.sign;        return y < cmp.y;    }}seg[maxn << 1];//sum存的是全部被覆蓋的長度,rep存的是被重複覆蓋的長度double sum[maxn << 2],rep[maxn];//c存的是該區間被覆蓋幾次int c[maxn << 2];//存的是原值,用於離散化double X[maxn << 1];void push_up(int rt,int l, int r){    if(c[rt])   sum[rt] = X[r + 1] - X[l];    else if(l == r) sum[rt] = 0.0;    else    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];    if(c[rt] >= 2)  rep[rt] = X[r + 1] - X[l];    else if(l == r) rep[rt] = 0.0;//這裡需要討論一下    else if(c[rt] == 1) rep[rt] = sum[rt << 1] + sum[rt << 1 | 1];    else    rep[rt] = rep[rt << 1] + rep[rt << 1 | 1];}void update(int L, int R, int ss, int l , int r, int rt){    if(L <= l && r <= R){        c[rt] += ss;        push_up(rt,l,r);    return;    }    int m = (l + r) >> 1;    if(L <= m)  update(L,R,ss,lson);    if(R > m)   update(L,R,ss,rson);    push_up(rt,l,r);}//二分的時候對double注意//int bin(int low, int high, double key){//    int mid;//    while(low < high){//        mid = (low + high) >> 1;//        if(X[mid] - key + eps < 0)    low = mid + 1;//        else    high = mid;//    }//    return low;//}int bin(int low, int high, double key){    int mid;    while(low <= high){        mid = (low + high) >> 1;        if(X[mid] == key)    return mid;        if (X[mid] < key) low = mid + 1;        else    high = mid - 1;    }    return -1;}int n,xx,t,s,p;double x1,x2,y1,y2,ret;void init(){    memset(sum,0,sizeof(sum));    memset(rep,0,sizeof(rep));    memset(c,0,sizeof(c));    s=0;  xx= 0;  p = 1;    ret = 0.0;    return;}int main(){    scanf("%d",&t);    while(t --){        scanf("%d",&n) ;        init();        for(int i = 0; i < n; i ++){            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            seg[s ++] = Seg(x1,x2,y1,1);            seg[s ++] = Seg(x1,x2,y2,-1);            X[xx ++] = x1;            X[xx ++] = x2;        }        sort(X,X+xx);        for(int i = 1; i < xx; i ++){            if(X[i - 1] != X[i])    X[p ++] = X[i];        }        sort(seg,seg+s);    p --;        for(int i = 0; i < s - 1; i ++){            int l = bin(0,p,seg[i].l);            int r = bin(0,p,seg[i].r) - 1;//            cout << l << "      " << r << endl;            if(l <= r) update(l,r,seg[i].sign,0,p - 1,1);//            cout << rep[1] << "###" << endl;//            cout << sum[1] << "&&&" << endl;            ret +=  rep[1] * (seg[i + 1].y - seg[i].y);        }        printf("%.2lf\n",ret);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.