【poj1177】Picture(矩形周长并+线段树+扫描线)

Description

    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.

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

I think

    题意:给出多个矩形左下角与右上角端点的横纵坐标,求矩形周长并。
    算法:线段树+扫描线+离散化
    思路:扫描线的基本思路与矩形面积并一致,详见 【poj1151】Atlantis(矩形面积并+线段树+扫描线)
    实现:以下代码将y轴坐标离散化。
    扫描当前边之前,用扫过上一条边之后的总区间包含不连续段数×2×两条边横坐标差值,更新ans,此即该段沿X轴方向的周长的长度。扫过后,用扫过当前线段后整个区间的被覆盖长度与扫过上一条线段后的整个区间的被覆盖长度的差值(abs)更新ans,此即沿Y轴方向的周长。
    维护区间包含段数需新建数组ll[],rr[],分别表示区间左/右端点是否被覆盖,合并两个区间信息时,当且仅当该区间的左半区间的右端点与右半区间的左端点同时被覆盖的情况下,左右区间不连续段数和-1。

Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int sm = 1e4+50;
int n,t,T,x1,y1,x2,y2,ans,pre,linesum;
int val[sm<<2],c[sm<<2],y[sm<<2];
int cnt[sm<<2],ll[sm<<2],rr[sm<<2];
//ll记最左端的区间是否被覆盖 
//rr记最右端的区间是否被覆盖
//cnt记该区间总不连续段数 
//用于计算未被离散化的轴方向的边的条数
struct line {
    int x,y1,y2,flag;
}a[sm];
bool cmp(line a,line b) { return a.x<b.x; }

void calen(int x,int l,int r) {
    if(c[x]>0) { 
        val[x]=y[r]-y[l];
        cnt[x]=ll[x]=rr[x]=1;
    }
    else if(l+1==r) val[x]=cnt[x]=ll[x]=rr[x]=0;
    else { 
        int ls=x<<1,rs=x<<1|1;
        ll[x]=ll[ls],rr[x]=rr[rs];
        val[x]=val[ls]+val[rs];
        cnt[x]=cnt[ls]+cnt[rs]-rr[ls]*ll[rs];
    }
}

void update(int k,int l,int r,line x) {
    if(x.y1<=y[l]&&y[r]<=x.y2) {
        c[k]+=x.flag;calen(k,l,r);
        return;
    }
    if(l+1==r)return;
    int m=(l+r)>>1;
    if(x.y1<=y[m])update(k<<1,l,m,x);
    if(x.y2>=y[m])update(k<<1|1,m,r,x);
    calen(k,l,r);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;++i) {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        y[++t]=y1,a[t].x=x1,a[t].y1=y1,a[t].y2=y2,a[t].flag=1;
        y[++t]=y2,a[t].x=x2,a[t].y1=y1,a[t].y2=y2,a[t].flag=-1;
    }
    sort(y+1,y+t+1);
    sort(a+1,a+t+1,cmp);
    T=unique(y+1,y+t+1)-y-1;
    pre=linesum=0;
    for(int i=1;i<=t;++i) {
        if(i>1)ans+=2*linesum*(a[i].x-a[i-1].x);
        update(1,1,T,a[i]);
        ans+=abs(val[1]-pre);
        pre=val[1],linesum=cnt[1];
    }       
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题属于技术问题。以下是一个简单的Python模拟登录POJ提交代码并抓取评测结果的代码示例: ```python import requests # 登录POJ,获取cookie def login(username, password): s = requests.Session() login_url = "http://poj.org/login" login_data = { "user_id1": username, "password1": password, "B1": "login", "url": "/" } s.post(login_url, data=login_data) return s # 提交代码 def submit_code(s, problem_id, language, source_code): submit_url = "http://poj.org/submit" submit_data = { "problem_id": problem_id, "language": language, "source": source_code } s.post(submit_url, data=submit_data) # 获取评测结果 def get_result(s, run_id): status_url = "http://poj.org/status" params = { "user_id": "", "result": "", "language": "", "top": run_id } r = s.get(status_url, params=params) table_start = r.text.find("<table cellpadding=0 cellspacing=0 border=0 width=100%>") table_end = r.text.find("</table>", table_start) table_html = r.text[table_start:table_end + 8] return table_html # 使用示例 username = "your_username" password = "your_password" problem_id = "1000" language = "G++" source_code = """ #include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << a + b << endl; return 0; } """ s = login(username, password) submit_code(s, problem_id, language, source_code) table_html = get_result(s, "12345678") # 替换成实际提交的run id print(table_html) ``` 其中,`login`函数模拟登录POJ并返回一个`Session`对象,`submit_code`函数提交代码,`get_result`函数获取评测结果。你可以根据实际需要修改代码中的`username`、`password`、`problem_id`、`language`和`source_code`等参数,并替换`get_result`函数中的`run_id`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值