[机房练习赛4.4] area 扫描线

Problem 2. area

Input file: area.in
Output file: area.out
Time limit: 2 seconds
Memory limit: 256 MB
给出n 个矩形,求它们的面积并.
更准确一点,每个矩形将给出它的左上角和右下角的位置:x1; y1; x2; y2
这四个数都是整数且满足x1 x2; y1 y2.
我们需要你求:
area =j f(x; y) 2 Z Z j 9 a rect: s:t: x1 x x2 and y1 y y2g j
Input
第1 行1 个整数:n,表示矩形的个数。
接下来n 行,每行4 个整数:x1 y1 x2 y2,表示一个矩形的左上角和右下角的坐标。
Output
输出area。
Sample
area.in
31
1 2 3
1 2 3 3
3 3 4 4
area.out
11
样例解释:一共有11 个点落在了上面三个矩形所表示的区域内:
(1; 1); (1; 2); (1; 3); (2; 1); (2; 2); (2; 3); (3; 2); (3; 3); (3; 4); (4; 3); (4; 4)
Note
• 对于30% 的数据,1 n 100,1 x1 x2 100,1 y1 y2 100
• 对于100% 的数据,1 n 105,1 x1 x2 105,1 y1 y2 105
扫描线,对于一个举行(x1,y1,x2,y2),将它看成两个事件:在x1 这个时间将
(y1,y2) 这个区间加一,在x2+1 这个时间将(y1,y2) 这个区间减一。
这样,我们遍历整个时间,并在执行完这个时间的操作后看看有多少位置非
0, 将其数量加到答案中,就完了,当然时间不能傻傻地一个一个枚,因为关键
的时间点最多2n 个,其它时候面积是没有变的,所以要一段一段地算。
至于怎么用线段树实现那么查看有多少个非零的位置,需要注意对于任何一
个减一操作,前面一定有一个和它一样的加一操作,就只需要维护一下每个节
点被完全覆盖的次数。再用另一个来统计子树中的那些修改导致这个节点还有
多少个非零。有点像标记永久化(我们讲的第二种区间修改的写法)。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 100000 + 10;
typedef long long ll;
inline int read() {
    int x = 0, f = 1;char ch = getchar();
    while( ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}
    while( ch >= '0' && ch <= '9' ){x = x*10+ch-'0';ch = getchar();}
    return x*f;
}
struct Event{
    int type;
    int time;
    int lf, rg;
    Event(){}
    Event( int type, int time, int lf, int rg )
        :type(type),time(time),lf(lf),rg(rg){}
};
bool operator<( const Event &r, const Event &s ) {
    return r.time < s.time;
}
struct Node{
    int cnt,sum;
    Node *ls, *rs;
    int query( int lf, int rg ){
        return cnt? rg - lf + 1 : sum;
    }
    void update( int lf, int rg ){
        int mid = (lf + rg) >> 1;
        sum = ls->query(lf,mid) + rs->query(mid+1,rg);
    }
}pool[N*2], *tail = pool, *root;
int n,total;
Event events[N*2];
Node *build( int lf, int rg ){
    Node *nd = ++tail;
    if( lf == rg ) nd->cnt = nd->sum = 0;
    else{
        int mid = (lf + rg) >> 1;
        nd->ls = build( lf, mid );
        nd->rs = build( mid+1, rg );
        nd->sum = nd->cnt = 0;
    }
    return nd;
}
void modify( Node *nd, int lf, int rg, int L, int R, int delta ) {
    if( L <= lf && R >= rg ){
        nd->cnt += delta;
        return;
    }
    int mid = (lf + rg) >> 1;
    if( L <= mid ) modify( nd->ls, lf, mid, L, R, delta );
    if( mid < R ) modify( nd->rs, mid+1, rg, L, R, delta );
    nd->update(lf,rg);
}
int main(){
    freopen("area.in","r",stdin);
    freopen("area.out","w",stdout);
    n = read(); int yu = n; int gu = 100000;
    for( int i = 1; i <= n; i++ ){
        int x1,x2,y1,y2;
        x1 = read(); y1 = read(); x2 = read(); y2 = read();
        events[total++] = Event( 1, x1, y1, y2 );
        events[total++] = Event( 2, x2+1, y1, y2 );
        if( y2 > yu ) yu = y2;
        if( y1 < gu ) gu = y1;
    }
    std::sort( events, events+total );
    root = build( gu, yu );
    ll ans = 0;
    for( int i = 0,j; i < total; i = j + 1 ){
        for( j = i; j + 1 < total && events[j+1].time == events[i].time; j++ );
        for( int k = i; k <= j; k++ )
            modify( root, gu, yu, events[k].lf, events[k].rg, events[k].type==1?1:-1);
        if( j+1 != total )
            ans += (ll)root->query(gu,yu)*(events[j+1].time - events[i].time);
    }
    printf("%I64d",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值