zoukankan      html  css  js  c++  java
  • Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments
     
     

    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
     
    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).

    题意:

      给你n天平行x,y轴的线段

      问你遍历的点有多少个

    题解:

      将线段 扩展成一个长度为x * 1 的矩阵

      做一遍线段树扫描线求矩阵面积并

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    
    const int N = 5e5+10, M = 5e5+11, inf = 2e9, mod = 1e9+7;
    const double Pi = acos(-1.0);
    typedef long long LL;
    #define ls k<<1
    #define rs ls | 1
    
    int san[N], sum[N], vis[N], n, cnt = 0;
    struct edge{
        int l,r,x,in;
        edge(int l = 0, int r = 0, int x = 0, int in = 0) : l(l), r(r), x(x), in(in) {}
        bool operator < (const edge &b) const {
            return x < b.x || x == b.x && in > b.in;
        }
    }e[N];
    int Hash(int x) {return lower_bound(san+1,san+cnt+1,x) - san;}
    void push_up(int k,int ll,int rr) {
        if(vis[k]) sum[k] = san[rr + 1] - san[ll];
        else if(ll == rr) sum[k] = 0;
        else sum[k] = sum[ls] + sum[rs];
    }
    void update(int l,int r,int c,int ll,int rr,int k) {
        if(ll == l && rr == r) {
            vis[k] += c;
            push_up(k,ll,rr);
            return ;
        }
        int mid = (ll + rr) >> 1;
        if(r <= mid) update(l,r,c,ll,mid,ls);
        else if(l > mid) update(l,r,c,mid+1,rr,rs);
        else update(l,mid,c,ll,mid,ls), update(mid+1,r,c,mid+1,rr,rs);
        push_up(k,ll,rr);
    }
    int main() {
            scanf("%d",&n);
            for(int i = 1; i <= n; ++i) {
                    int x,y,xx,yy;
                    scanf("%d%d%d%d",&x,&y,&xx,&yy);
                    if(x > xx) swap(x,xx);
                    if(y > yy) swap(y,yy);
                    xx++, yy++;
                    san[++cnt] = y;
                    san[++cnt] = yy;
                    e[i] = edge(y,yy,x,1);
                    e[i+n] = edge(y,yy,xx,-1);
            }
    
            sort(san+1,san+cnt+1);
            cnt = unique(san + 1, san + cnt + 1) - san - 1;
    
            int m = n << 1;
            sort(e+1,e+m+1);
    
            LL ans = 0;
            for(int i = 1; i <= m; ++i) {
                int l = Hash(e[i].l);
                int r = Hash(e[i].r) - 1;
                if(l <= r) update(l,r,e[i].in,1,m,1);
                ans += 1LL * sum[1] * (e[i+1].x - e[i].x);
            }
            cout<<ans<<endl;
    }
  • 相关阅读:
    Win10安装系统后,提示initializing and establishing link无法启动系统
    CSVN使用入门
    CSVN(SVN)命令入门及使用过程中遇到的错误问题汇总
    CSVN部署安装,实现web管理svn
    xenserver使用快照创建虚拟机,提示eth0 has different mac错误
    开机或者安装系统时提示tsc: Fast TSC calibration failed解决方法
    Nginx——debug的使用
    nginx屏蔽某一ip的访问
    plsql快速入门
    Web02_HTML&CSS
  • 原文地址:https://www.cnblogs.com/zxhl/p/5761077.html
Copyright © 2011-2022 走看看