zoukankan      html  css  js  c++  java
  • 【20.51%】【codeforces 610D】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).

    【题目链接】:http://codeforces.com/contest/610/problem/D

    【题解】

    给你n条横线和纵线;
    让你求这些线覆盖的点的面积(线上的一个点覆盖的面积为1);
    最后面积不能重复;
    做法:
    只要把右上角的横纵坐标都加1;
    就转化为扫描线求并矩形的面积问题了;
    要把横坐标离散化下;
    具体扫描线求并矩形面积请看这篇文章
    http://blog.csdn.net/harlow_cheng/article/details/53027415

    【完整代码】↓↓↓

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    
    using namespace std;
    
    const int MAXN = 1e5+100;
    const int dx[5] = {0,1,-1,0,0};
    const int dy[5] = {0,0,0,-1,1};
    const double pi = acos(-1.0);
    struct abc
    {
        LL l,r,h;
        int k;
    };
    
    int n,num = 0,cnt[MAXN<<3];
    LL sum[MAXN<<3];
    LL a1,b1,a2,b2;
    abc bian[MAXN*2];
    vector <LL> a;
    
    void read2(LL &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t) && t!='-') t = getchar();
        LL sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    void read1(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)&&t!='-') t = getchar();
        int sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    bool cmp(abc a,abc b)
    {
        return a.h<b.h;
    }
    
    void push_up(int rt,int l,int r)
    {
        if (cnt[rt])
            sum[rt]=a[r+1]-a[l];
        else
            if (l==r)
                sum[rt] = 0;
            else
                sum[rt] = sum[rt<<1]+sum[rt<<1|1];
    }
    
    void up_data(int L,int R,int c,int l,int r,int rt)
    {
        if (L<=l && r<=R)
        {
            cnt[rt]+=c;
            push_up(rt,l,r);
            return;
        }
        int m = (l+r)>>1;
        if (L <= m)
            up_data(L,R,c,lson);
        if (m < R)
            up_data(L,R,c,rson);
        push_up(rt,l,r);
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        read1(n);
        for (int i = 1;i <= n;i++)
        {
            read2(a1);read2(b1);read2(a2);read2(b2);
            if (a1>a2)
                swap(a1,a2);
            if (b1>b2)
                swap(b1,b2);
            a2++;b2++;
            a.push_back(a1);a.push_back(a2);
            bian[++num].l = a1,bian[num].r = a2,bian[num].h = b1,bian[num].k = 1;
            bian[++num].l = a1,bian[num].r = a2,bian[num].h = b2,bian[num].k = -1;
        }
        sort(a.begin(),a.end());
        a.erase(unique(a.begin(),a.end()),a.end());
        sort(bian+1,bian+1+num,cmp);
        LL ans = 0;
        for (int i = 1;i <= num-1;i++)
        {
            int l = lower_bound(a.begin(),a.end(),bian[i].l)-a.begin();
            int r = lower_bound(a.begin(),a.end(),bian[i].r)-a.begin()-1;
            up_data(l,r,bian[i].k,0,a.size()-1,1);
            ans += sum[1]*(bian[i+1].h-bian[i].h);
        }
        cout << ans<<endl;
        return 0;
    }
  • 相关阅读:
    Python3爬虫之爬取某一路径的所有html文件
    python获取数据网页数据并创建文件夹保存(基于python3.6)
    Python 爬取单个网页所需要加载的地址和CSS、JS文件地址
    java的关闭钩子(Shutdown Hook)
    如何形象的解释 webhook 这个词
    Webhook
    什么是webhook
    瞎折腾之Webhooks
    Java-马士兵设计模式学习笔记-观察者模式-读取properties文件,动态增加观察者
    怎么解决重装系统后“我的文档”拒绝访问(更改权限就可以了)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632087.html
Copyright © 2011-2022 走看看