Picture【扫描线+线段树】

106 篇文章 0 订阅
9 篇文章 0 订阅

A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. 

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. 



The corresponding boundary is the whole set of line segments drawn in Figure 2. 



The vertices of all rectangles have integer coordinates.

Input

Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate. 

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. 

Please process to the end of file.

Output

Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

Sample Input

7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16

Sample Output

228

  一开始想用两次暴力去写的,就是竖着一遍再横着一遍去查询的……后来想到了一个好的主意(为了偷懒),然后就决定开始尝试一下,就决定开始写了,就是,对于每个查询的X轴上的长度,可以通过作与上一个状态的差值的绝对值来得到的,那么要乘以多少次Y轴的长度,就只会受到连续区间段的影响……推出来的,然后写了一下,就过了。

  就是,我们更新每次区间的时候,不忘更新点是否是断点,断点得记录下断出来的区间数,要是不是断点,就要记录下此区间内的断区间数目,然后推下去就是了,返回到tree[1],就可以得到该树的左右连续区间数目。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e4 + 5;
int N, num, cnt_x, X[maxN], lazy[maxN<<2];
struct node
{
    int lx, rx, y, val;
    node(int a=0, int b=0, int c=0, int d=0):lx(a), rx(b), y(c), val(d) {}
}line[maxN];
bool cmp(node e1, node e2) { return e1.y < e2.y; }
struct TREE
{
    int val, range; //val是此时该段被覆盖次数
    bool lc, rc;
    TREE(int a=0, bool b=false, bool c=false, int d=0, int f=0):val(a), lc(b), rc(c), range(d) {}
}tree[maxN<<2];
inline void buildTree(int rt, int l, int r)
{
    lazy[rt] = 0;
    tree[rt] = TREE();
    if(l == r) return;
    int mid = HalF;
    buildTree(Lson);
    buildTree(Rson);
}
void pushup(int rt, int l, int r)
{
    if(lazy[rt])
    {
        tree[rt].val = X[r + 1] - X[l];
        tree[rt].range = 1;
        tree[rt].lc = tree[rt].rc = true;
    }
    else if(l == r) tree[rt] = TREE();
    else
    {
        tree[rt].val = tree[lsn].val + tree[rsn].val;
        tree[rt].range = tree[lsn].range + tree[rsn].range;
        tree[rt].lc = tree[lsn].lc;
        tree[rt].rc = tree[rsn].rc;
        if(tree[lsn].rc && tree[rsn].lc) tree[rt].range--;
    }
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
    if(ql <= l && qr >= r)
    {
        lazy[rt] += val;
        pushup(myself);
        return;
    }
    int mid = HalF;
    if(qr <= mid) update(QL, val);
    else if(ql > mid) update(QR, val);
    else { update(QL, val); update(QR, val); }
    pushup(myself);
}
inline void init()
{
    num = 0;
    cnt_x = 1;
}
int main()
{
    while(scanf("%d", &N)!=EOF)
    {
        init();
        for(int i=1; i<=N; i++)
        {
            int lx, ly, rx, ry;
            scanf("%d%d%d%d", &lx, &ly, &rx, &ry);
            line[++num] = node(lx, rx, ly, 1);
            X[num] = lx;
            line[++num] = node(lx, rx, ry, -1);
            X[num] = rx;
        }
        sort(X + 1, X + num + 1);
        sort(line + 1, line + num + 1, cmp);
        for(int i=2; i<=num; i++) if(X[i] != X[i-1]) X[++cnt_x] = X[i];
        buildTree(1, 1, cnt_x);
        ll ans = 0, las = 0;
        for(int i=1; i<num; i++)
        {
            int l = (int)(lower_bound(X + 1, X + cnt_x + 1, line[i].lx) - X);
            int r = (int)(lower_bound(X + 1, X + cnt_x + 1, line[i].rx) - X - 1);
            update(1, 1, cnt_x, l, r, line[i].val);
            ans += abs(tree[1].val - las) + tree[1].range * 2 * (line[i+1].y - line[i].y);
            las = tree[1].val;
        }
        ans += line[num].rx - line[num].lx;
        printf("%lld\n", ans);
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
在 MFC 中,如果你想在 `Picture Control` 控件中绘制十字线,可以按照以下的步骤进行: 1. 在 `Picture Control` 控件的消息处理函数(例如 `OnPaint()` 函数)中获取设备环境句柄。你可以使用 `CPaintDC` 类来获取设备环境句柄,例如: ```cpp CPaintDC dc(this); ``` 其中,`this` 是指向 `Picture Control` 控件的指针。 2. 创建 `CPen` 对象,设置画笔的属性,例如: ```cpp CPen pen(PS_SOLID, 1, RGB(255, 0, 0)); dc.SelectObject(&pen); ``` 其中,`PS_SOLID` 表示画笔的线型为实线,`1` 表示画笔的线宽为 1 个像素,`RGB(255, 0, 0)` 表示画笔的颜色为红色。 3. 在 `Picture Control` 控件的客户区中绘制十字线。你可以使用 `MoveTo()` 和 `LineTo()` 函数来绘制线条,例如: ```cpp CRect rect; GetClientRect(&rect); int centerX = rect.Width() / 2; int centerY = rect.Height() / 2; dc.MoveTo(0, centerY); dc.LineTo(rect.Width(), centerY); dc.MoveTo(centerX, 0); dc.LineTo(centerX, rect.Height()); ``` 其中,`GetClientRect()` 函数用于获取 `Picture Control` 控件的客户区矩形,`centerX` 和 `centerY` 分别表示客户区矩形的中心坐标。`MoveTo()` 函数用于移动画笔到起始点,`LineTo()` 函数用于绘制线条。 4. 在所有的绘制操作完成后,释放设备环境句柄和画笔对象。你可以使用 `CDC` 对象的 `ReleaseDC()` 函数来释放设备环境句柄,例如: ```cpp dc.SelectStockObject(NULL_PEN); dc.SelectStockObject(NULL_BRUSH); dc.ReleaseDC(); ``` 其中,`SelectStockObject()` 函数用于将画笔对象还原为系统默认的画笔和画刷,`ReleaseDC()` 函数用于释放设备环境句柄。 综上所述,你可以将以上的代码添加到 `Picture Control` 控件的消息处理函数中,以绘制十字线。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值