Codeforces Round #337 (Div. 2) D (矩形面积并



链接:戳这里


D. Vika and Segments
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.

Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.

Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.

Output
Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.

Examples
input
3
0 1 2 1
1 4 1 2
0 3 2 3
output
8
input
4
-2 -1 2 -1
2 1 -2 1
-1 -2 -1 2
1 2 1 -2
output
16
Note
In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).


题意:

矩形面积求并


思路:

前面的文章有分析


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n,m;
struct Line{
    ll x1,x2,y;
    int f;
    Line(ll x1=0,ll x2=0,ll y=0,int f=0):x1(x1),x2(x2),y(y),f(f){}
    bool operator < (const Line &a)const{
        if(y==a.y) return f>a.f;
        return y<a.y;
    }
}L[500100];
ll X[500100];
ll sum[800100];
int tr[800100];
void build(int root,int l,int r){
    if(l==r){
        tr[root]=0;
        sum[root]=0LL;
        return ;
    }
    int mid=(l+r)/2;
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    sum[root]=sum[root*2]+sum[root*2+1];
}
void pushup(int root,int l,int r){
    if(tr[root]) sum[root]=X[r+1]-X[l];
    else if(l==r) sum[root]=0;
    else sum[root]=sum[root*2]+sum[root*2+1];
}
void update(int root,int l,int r,int x,int y,int v){
    if(x<=l && y>=r){
        tr[root]+=v;
        pushup(root,l,r);
        return ;
    }
    int mid=(l+r)/2;
    if(y<=mid) update(root*2,l,mid,x,y,v);
    else if(x>mid) update(root*2+1,mid+1,r,x,y,v);
    else {
        update(root*2,l,mid,x,mid,v);
        update(root*2+1,mid+1,r,mid+1,y,v);
    }
    pushup(root,l,r);
}
int main(){
    ll x1,x2,y1,y2;
    scanf("%d",&n);m=0;
    for(int i=1;i<=n;i++){
        scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
        if(x1>x2) swap(x1,x2);
        if(y1>y2) swap(y1,y2);
        x2++;y2++;
        L[++m]=Line(x1,x2,y1,1);
        X[m]=x1;
        L[++m]=Line(x1,x2,y2,-1);
        X[m]=x2;
    }
    sort(L+1,L+m+1);
    sort(X+1,X+m+1);
    int cnt=unique(X+1,X+m+1)-(X+1);
    build(1,1,cnt);
    ll ans=0;
    int l=lower_bound(X+1,X+cnt+1,L[1].x1)-X;
    int r=lower_bound(X+1,X+cnt+1,L[1].x2)-X-1;
    update(1,1,cnt,l,r,L[1].f);
    for(int i=2;i<=m;i++){
        ans+=(L[i].y-L[i-1].y)*sum[1];
        l=lower_bound(X+1,X+cnt+1,L[i].x1)-X;
        r=lower_bound(X+1,X+cnt+1,L[i].x2)-X-1;
        if(l<=r) update(1,1,cnt,l,r,L[i].f);
    }
    printf("%I64d\n",ans);
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值