@2017-2018 ACM-ICPC, Asia Daejeon Regional Contest : Rectilinear Regions ( 细节处理题,模拟)

版权声明:岂曰无衣,与子同袍 https://blog.csdn.net/sizaif/article/details/82953902

A rectilinear path connecting two points in the plane is a path consisting of only horizontal and vertical line segments. A rectilinear path is said to be monotone with respect to the x-axis (resp., y-axis) if and only if its intersection with every vertical (resp., horizontal) line is either empty or a contiguous portion of that line. A staircase is a rectilinear path if it is monotone to both the x-axis and the y-axis, and a staircase is unbounded if it starts and ends with a semi-infinite horizontal segment, i.e., a segment that extends to infinity on both ends of the x-axis. Note that staircases can be either increasing or decreasing, depending on whether they go up or down as we move along them from left to right on the y-axis. A staircase with n vertical line segments is called a staircase with n steps.

Considering two unbounded staircases L and U, there can be several or no closed rectilinear regions bounded by staircases L and U. Among the closed rectilinear regions, some regions are bounded by a staircase L to the bottom and by a staircase U to the top. For example, in the following figure, the two regions colored yellow are that kind of closed rectilinear regions. We would like to compute the total area of such regions.

Figure G.1. Two staircases L and U, where U has 3-steps and L has 4-steps. The two yellow colored regions are closed rectilinear regions bounded by a staircase L to the bottom, and a staircase U to the top. The xiL,yiL (resp., xiU,yiU) are the x-, y-coordinates of corner points of the staircase L (resp., U).

y0 x1 y1 x2 y2 … xn yn  --------------------------------      (1)

where x1 < x2 < … < xn for x-coordinates of vertical line segments, and y0 < y1 < … < yn for y-coordinates of horizontal line segments of an increasing staircase or y0 > y1 > … > yn for a decreasing staircase.

For example, given a 4-step staircase L represented with

6 2 9 11 11 15 16 21 19

and a 3-step staircase U represented with

3 6 12 10 14 18 17

the number of bounded rectilinear regions is 2 and the total area of the regions is 32 (see figure G.1).

Given two unbounded staircases L and U that all x-coordinates represented in (1) of corner points of both L and U are unique, and all y-coordinates represented in (1) of corner points of both L and U are unique, compute the total area of bounded rectilinear regions that bounded by L to the bottom of the regions and by U to the top of the regions.

输入

Your program is to read from standard input. The first line contains two positive integers n and m, respectively, representing the number of steps of unbounded staircases L and U, where 1 ≤ n,m ≤ 25,000    25,000. The second (resp., third) line contains 2n + 1 (resp., 2m + 1) integers representing the x-, y-coordinates of corner points of the staircase L (resp., U), and the integers are sequenced in the order of the notation (1). The coordinates are represented with non-negative integers less than or equal to 50,000.

输出

Your program is to write to standard output. The first line should contain two integers k and w, where k represents the number of closed rectilinear regions and w represents the total area of those regions. If there is no such regions, then your program should write 0 for both k and w.

样例输入

4 3
6 2 9 11 11 15 16 21 19
3 6 12 10 14 18 17

样例输出

2 32

[题意]

给 定 L  和 U 线 的   起点 Ly0  Uy1

然后 依次输入 定点坐标,  求 U 线在L 线  上面 形成的  区域面积 和 块数  ( 黄色区域)

[思路]

 起初 以为是线段树扫描线...

其实并不是.

细节处理:    一  :  考虑 两个线的单调性.  如果不同  那么 0 0 

二 :   如果  单调递减 那么 全部  反处理.

三 : 可能一块区域是由多个小块组成 , 那么 需要处理  .

所有点放在一起.

按照x 坐标排序,  从左边扫到右边.  扫过去  处理..

[代码 code]

#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)

typedef long long ll;
const int maxn = 2e5+10;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
using namespace std;
struct Li
{
	int x,y;
	int id;
}L[maxn],U[maxn];
std::vector<Li> v;
int cmp(Li a, Li b)
{
	return a.x < b.x;
}

int main(int argc, char const *argv[])
{
	int n,m;
	scanf("%d %d",&n,&m);
	int Ly0,Uy1;
	scanf("%d",&Ly0);
	rep(i,1,n)
	{
		scanf("%d%d",&L[i].x,&L[i].y);
		L[i].id = 1;
		v.push_back(L[i]);
	}
	scanf("%d",&Uy1);
	rep(i,1,m)
	{
		scanf("%d%d",&U[i].x,&U[i].y);
		U[i].id = 0;
		v.push_back(U[i]);
	}
	int j1 = L[1].y > Ly0;
	int j2 = U[1].y > Uy1;
	if (j1 !=j2 )
	{
		printf("0 0\n");
		return 0;
	}
	if( !j1 )
	{
		Ly0 = -Ly0;
		Uy1 = -Uy1;
		swap(Ly0,Uy1);
		rep(i,0,v.size()-1)
		{
			v[i].y = -v[i].y;
			v[i].id = !v[i].id;
		}
	}
	sort(v.begin(), v.end(),cmp);

	ll ans = 0;
	bool flag = Ly0 < Uy1;
	int pre = -1;
	int cot = 0;
	ll temp = 0;
	rep(i,0,v.size()-1)
	{
		if( v[i].id )// L 
		{
			if( pre!=-1)
			{
				temp += 1ll*(v[i].x - pre )*(Uy1-Ly0); //  U > L;
				if( v[i].y < Uy1) // 未完成.
				{
					pre = v[i].x;
				}
				else // update
				{
					pre = -1;
					ans += temp;
					cot++;
					temp = 0;
				}
			}
			Ly0 = v[i].y; // 更新 Ly0
			if( Ly0 >= Uy1)
				flag = 0;
		}
		else  // U
		{
			if( pre!=-1)
			{
				temp += 1ll* (v[i].x - pre )*( Uy1-Ly0 ); // U > L
				pre = v[i].x;
			}
			Uy1 = v[i].y;
			if( !flag && pre==-1 && Uy1 > Ly0)
				pre = v[i].x;
		}
		
	}
	printf("%d %lld\n",cot,ans );
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/82953902