線段樹+掃描線【p1884】[Usaco12FEB]過度種植(銀)Overplanting …

來源:互聯網
上載者:User

標籤:char s   mmm   block   ons   sig   build   線段   cpp   opera   

Description

在一個笛卡爾平面座標系統裡(則X軸向右是正方向,Y軸向上是正方向),有\(N(1<=N<=1000)\)個矩形,第i個矩形的左上方座標是\((x1, y1)\),右下角座標是\((x2,y2)\)。問這\(N\)個矩形所覆蓋的面積是多少?注意:被重複覆蓋的地區的面積只算一次。

Input

第一行,一個整數N。 \((1<=N<=1000)\)。

接下來有\(N\)行,每行描述一個矩形的資訊,分別是矩形的\(x1、y1、x2、y2\)。

其中 \(?10^8<=x1,y1,x2,y2<=10^8\)。

Ouput

一個整數,被N個矩形覆蓋的地區的面積。

難得遇到一個裸的掃描線的題,竟然沒切掉 emmm.

看到\(x,y\)的座標範圍,離散化就好了!

沒有一遍切,竟然是沒開\(long \ \ long\)!!!

太難受了,關於這個的話就不多BB,網上講解很多.

大家可以去搜一下。(貌似NOIP不會考,暫且學了)

將來有時間寫講解好了 qwq.

代碼

#include<cstdio>#include<algorithm>#include<iostream>#define int long long #define R registerusing namespace std;const int gz=10086;inline void in(int &x){    int f=1;x=0;char s=getchar();    while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}    while(isdigit(s)){x=x*10+s-'0';s=getchar();}    x*=f;}struct cod{    int l,r,h;    int f;    bool operator <(const cod&a)const    {        return h<a.h;    }}edge[gz];struct tre{    int l,r,s;    int len;}tr[gz];#define ls o<<1#define rs o<<1|1int x[gz],n,tot;void build(R int o,R int l,R int r){    tr[o].l=l;tr[o].r=r;    if(l==r)return;    R int mid=(l+r)>>1;    build(ls,l,mid);    build(rs,mid+1,r);}inline void up(R int o){    if(tr[o].s)        tr[o].len=x[tr[o].r+1]-x[tr[o].l];    else if(tr[o].l==tr[o].r)        tr[o].len=0;    else tr[o].len=tr[ls].len+tr[rs].len;}void change(R int o,R int l,R int r,R int del){    if(tr[o].l==l and tr[o].r==r)    {        tr[o].s+=del;        up(o);        return;    }    R int mid=(tr[o].l+tr[o].r)>>1;    if(r<=mid) change(ls,l,r,del);    else if(l>mid) change(rs,l,r,del);    else change(ls,l,mid,del),change(rs,mid+1,r,del);    up(o);}signed main(){    in(n);    for(R int i=1;i<=n;i++)    {        R int x1,x2,y1,y2;        in(x1),in(y1),in(x2),in(y2);        edge[++tot].l=x1;edge[tot].r=x2;edge[tot].f=-1;        edge[tot].h=y1;x[tot]=x1;        edge[++tot].l=x1;edge[tot].r=x2;edge[tot].f=1;        edge[tot].h=y2;x[tot]=x2;    }    sort(edge+1,edge+tot+1);    sort(x+1,x+tot+1);    int new_n=1;    for(R int i=2;i<=tot;i++)        if(x[new_n]!=x[i])x[++new_n]=x[i];    build(1,1,new_n);    int ans=0;    for(R int i=1;i<=tot;i++)    {        R int l=lower_bound(x+1,x+new_n+1,edge[i].l)-x;        R int r=lower_bound(x+1,x+new_n+1,edge[i].r)-x-1;        change(1,l,r,edge[i].f);        ans+=(edge[i+1].h-edge[i].h)*tr[1].len;    }    printf("%lld",ans);}

線段樹+掃描線【p1884】[Usaco12FEB]過度種植(銀)Overplanting …

聯繫我們

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