zoukankan      html  css  js  c++  java
  • Codeforces Round #337 Vika and Segments

    D. Vika and Segments
    time limit per test: 
    2 seconds
     
     
    memory limit per test: 
    256 megabytes
    input
    : standard input
    output: 
    standard 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.

    Sample test(s)
    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).

    分析:

    这是一道计数题,方法比较传统,离散、扫描、树状数组、求差。

    首先考虑将共线的线段合并,用O(nlogn)排序后线性合并。处理后的线段都是相离的。

    再考虑用水平扫描线自顶向下扫描,将线段的左右端点看作是对纵线范围数值求和,事先将纵线按照较高的y值从大大小排列。

    那么用树状数组储存那些横跨当前水平扫描线y值得纵线,如果是考虑线段,那么需要同时考虑进队和出队,然而若将线段看成前缀之差,那么只需考虑进队,进行两次计算

    作差即可。从而计数交点的数目。

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <map>
    using namespace std;
    typedef __int64 ll;
    const int maxn = 2e5 + 10;
    int n, nh, nv;
    int base;
    map<int, int> mapi;
    struct H{
        int x1, x2, y;
        H(int x1 = 0, int x2 = 0, int y = 0) : x1(x1), x2(x2), y(y) {}
        bool operator < (const H &rhs) const{
            if(y < rhs.y) return 1;
            if(y == rhs.y && x1 < rhs.x1) return 1;
            if(y == rhs.y && x1 == rhs.x1 && x2 < rhs.x2) return 1;
            return 0;
        }
    }h[maxn], h1[maxn];
    
    struct V{
        int y1, y2, x;
        V(int y1 = 0, int y2 = 0, int x = 0) : y1(y1), y2(y2), x(x) {}
        bool operator < (const V &rhs) const{
            if(x < rhs.x) return 1;
            if(x == rhs.x && y1 < rhs.y1) return 1;
            if(x == rhs.x && y1 == rhs.y1 && y2 < rhs.y2) return 1;
            return 0;
        }
    }v[maxn];
    
    int inv[maxn];
    int buf[maxn], nbuf;
    ll x[maxn];
    
    bool cmpv(V p, V q){
        if(p.y2 > q.y2) return 1;
        if(p.y2 == q.y2 && p.y1 > q.y1) return 1;
        return 0;
    }
    
    bool cmph(H p, H q){
        if(p.y > q.y) return 1;
        if(p.y == q.y && p.x1 < q.x1) return 1;
        if(p.y == q.y && p.x1 == q.x1 && p.x2 < q.x2) return 1;
        return 0;
    }
    
    bool cmpvy2(V p, V q){
        return p.y2 > q.y2;
    }
    
    int low_bit(int x) { return x & (-x); }
    
    void Push(int i){
        int tem = mapi[v[i].x];
        while(tem <= base){
            x[tem]++;
            tem += low_bit(tem);
        }
    }
    
    ll query(int i, int j){
        i = mapi[i], j = mapi[j];
        ll tem = 0;
        while(j >= 1){
            tem += x[j];
            j -= low_bit(j);
        }
        --i;
        while(i >= 1){
            tem -= x[i];
            i -= low_bit(i);
        }
        return tem;
    }
    
    void solve(){
        sort(h, h + nh);
        sort(v, v + nv);
        mapi.clear();
        ll ans = 0;
        int nh1 = 0, nv1 = 0;
        int head, tail;
        if(nh){
        head = h[0].x1, tail = h[0].x2;
        nbuf = 0;
        for(int i = 1; i < nh; i++){
            if(h[i].y != h[i - 1].y || h[i].x1 > tail){
                ans += tail - head + 1;
                h[nh1++] = H(head, tail, h[i - 1].y);
                buf[nbuf++] = head, buf[nbuf++] = tail;
                head = h[i].x1, tail = h[i].x2;
            }else if(h[i].x2 > tail) tail = h[i].x2;
        }
        ans += tail - head + 1;
        h[nh1++] = H(head, tail, h[nh - 1].y);
        buf[nbuf++] = head, buf[nbuf++] = tail;
        }if(nv){
        head = v[0].y1, tail = v[0].y2;
        for(int i = 1; i < nv; i++){
            if(v[i].x != v[i - 1].x || v[i].y1 > tail){
                ans += tail - head + 1;
                v[nv1++] = V(head, tail, v[i - 1].x);
                if(v[i].x != v[i - 1].x) buf[nbuf++] = v[i - 1].x;
                head = v[i].y1, tail = v[i].y2;
            }else if(v[i].y2 > tail) tail = v[i].y2;
        }
        ans += tail - head + 1;
        v[nv1++] = V(head, tail, v[nv - 1].x);
        buf[nbuf++] = v[nv - 1].x;
        }
        sort(buf, buf + nbuf);
        base = 1;
        mapi[buf[0]] = base, inv[base] = buf[0];
        for(int i = 1; i < nbuf; i++){
            if(buf[i] == buf[i - 1]) continue;
            mapi[buf[i]] = ++base, inv[base] = buf[i];
        }
        nv = nv1, nh = nh1;
        sort(v, v + nv, cmpv);
        sort(h, h + nh, cmph);
        memset(x, 0, sizeof x);
        int pointer = 0;
        while(pointer < nv && v[pointer].y2 >= h[0].y) Push(pointer), ++pointer;
        ll tem = query(h[0].x1, h[0].x2);
        ans -= tem;
        for(int i = 1; i < nh; i++){
            if(h[i].y != h[i - 1].y){
                while(pointer < nv && v[pointer].y2 >= h[i].y) Push(pointer), ++pointer;
            }
            tem = query(h[i].x1, h[i].x2);
            ans -= tem;
        }
        for(int i = 0; i < nv; i++) v[i].y2 = v[i].y1 - 1;
        pointer = 0;
        sort(v, v + nv, cmpvy2);
        memset(x, 0, sizeof x);
        while(pointer < nv && v[pointer].y2 >= h[0].y) Push(pointer), ++pointer;
        tem = query(h[0].x1, h[0].x2);
        ans += tem;
        for(int i = 1; i < nh; i++){
            if(h[i].y != h[i - 1].y){
                while(pointer < nv && v[pointer].y2 >= h[i].y) Push(pointer), ++pointer;
            }
            tem = query(h[i].x1, h[i].x2);
            ans += tem;
        }
        printf("%I64d
    ", ans);
    }
    
    int main(){
        //freopen("in.txt", "r", stdin);
        while(~scanf("%d", &n)){
            int x1, y1, x2, y2;
            nh = nv = 0;
            for(int i = 0; i < n; i++){
                scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
                if(y1 == y2) h[nh++] = H(min(x1, x2), max(x1, x2), y1);
                else v[nv++] = V(min(y1, y2), max(y1, y2), x1);
            }
            solve();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    关于React的入门级安装和最浅显解释
    Node开发文件上传系统及向七牛云存储和亚马逊AWS S3的文件上传
    AWS S3 CLI的安装和配置
    用Node完成AWS S3的Upload流程之全世界最简版
    在Web应用中接入微信支付的流程之极简清晰版
    storm metrics
    hadoop 2.2.0 centos 6.4 x64 编译
    如何打造核心竞争力(经验总结)
    mysql event scheduler机制 与 动态表名创建
    hadoop 2.2.0 安装
  • 原文地址:https://www.cnblogs.com/astoninfer/p/5102321.html
Copyright © 2011-2022 走看看