HDU1828--Picture

Problem 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.

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

 

/*
请无视这段代码。直接看后面优化好的代码~~~
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
#define maxn 10008
int X[maxn],XX[maxn];
int Y[maxn],YY[maxn];//一个用来存原始的,一个用来存离散后的
//矩形的个数5000上限,横坐标上限个数10000个
map <int , int> collx;
map <int , int> colly;
int n;
int tree;
void lisanx(int * x)
{
	sort(x+1,x+2*n+1);
	tree=0;
	for(int i=1;i<=2*n;i++)
	{
		if(collx[x[i]]==0)
		{
			collx[x[i]]=++tree;
			XX[tree]=x[i];
		}
	}
}
void lisany(int * y)
{
	sort(y+1,y+2*n+1);
	tree=0;
	for(int i=1;i<=2*n;i++)
	{
		if(colly[y[i]]==0)
		{
			colly[y[i]]=++tree;
			YY[tree]=y[i];
		}
	}
}
struct Linex
{
	int x1,x2,y;
	bool vis;//用来标记是上边还是下边
}linex[maxn];
struct Liney
{
	int x,y1,y2;
	bool vis;
}liney[maxn];
bool cmp(Linex a,Linex b)
{
	return a.y<b.y;
}
bool cmp1(Liney a,Liney b)
{
	return a.x<b.x;
}
struct ST
{
	int l,r,add;
	int len;
}st[4*maxn];
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l==r||l+1==r)
	{
		st[id].add=0;
		st[id].len=0;
		return;
	}
	int mid=(l+r)>>1;
	buildtree(2*id,l,mid);
	buildtree(2*id+1,mid,r);
	st[id].len=st[id].add=0;
}
int query(int id,int l,int r)
{
	if(st[id].add>=1)
	{
		return XX[st[id].r]-XX[st[id].l];
	}
	if(l==r||l+1==r)return 0;
	if(st[id].add)
	{
		st[2*id].add+=st[id].add;
		st[2*id+1].add+=st[id].add;
		st[id].add=0;
	}
	if(st[2*id].r>=r)
	{
		return query(2*id,l,r);
	}
	if(st[2*id+1].l<=l)
	{
		return query(2*id+1,l,r);
	}
	return query(2*id,l,st[2*id].r)+query(2*id+1,st[2*id+1].l,st[2*id+1].r);
}
void update(int id,int l,int r,int ope)
{
	if(st[id].l==l && st[id].r==r)
	{
		st[id].add+=ope;
		st[id].len=query(id,l,r);
		return;
	}
	if(st[id].add!=0)
	{
		st[2*id].add+=st[id].add;
		st[2*id+1].add+=st[id].add;
		st[id].add=0;
	}
	if(st[2*id].r>=r)
	{
		update(2*id,l,r,ope);
		st[id].len=st[2*id].len+st[2*id+1].len;
		return;
	}
	if(st[2*id+1].l<=l)
	{
		update(2*id+1,l,r,ope);
		st[id].len=st[2*id].len+st[2*id+1].len;
		return;
	}
	update(2*id,l,st[2*id].r,ope);
	update(2*id+1,st[2*id+1].l,r,ope);
	st[id].len=st[2*id].len+st[2*id+1].len;
}
int query1(int id,int l,int r)
{
	if(st[id].add>=1)
	{
		return YY[st[id].r]-YY[st[id].l];
	}
	if(l==r||l+1==r)return 0;
	if(st[id].add)
	{
		st[2*id].add+=st[id].add;
		st[2*id+1].add+=st[id].add;
		st[id].add=0;
	}
	if(st[2*id].r>=r)
	{
		return query1(2*id,l,r);
	}
	if(st[2*id+1].l<=l)
	{
		return query1(2*id+1,l,r);
	}
	return query1(2*id,l,st[2*id].r)+query1(2*id+1,st[2*id+1].l,st[2*id+1].r);
}
void update1(int id,int l,int r,int ope)
{
	if(st[id].l==l && st[id].r==r)
	{
		st[id].add+=ope;
		st[id].len=query1(id,l,r);
		return;
	}
	if(st[id].add!=0)
	{
		st[2*id].add+=st[id].add;
		st[2*id+1].add+=st[id].add;
		st[id].add=0;
	}
	if(st[2*id].r>=r)
	{
		update1(2*id,l,r,ope);
		st[id].len=st[2*id].len+st[2*id+1].len;
		return;
	}
	if(st[2*id+1].l<=l)
	{
		update1(2*id+1,l,r,ope);
		st[id].len=st[2*id].len+st[2*id+1].len;
		return;
	}
	update1(2*id,l,st[2*id].r,ope);
	update1(2*id+1,st[2*id+1].l,r,ope);
	st[id].len=st[2*id].len+st[2*id+1].len;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		collx.clear();
		colly.clear();
		if(n==0)
		{
			printf("%d\n",0);
			continue;
		}
		int x1,y1,x2,y2;
		int kx=1,ky=1;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			X[kx]=x1;
			linex[kx].x1=x1;
			linex[kx].x2=x2;
			linex[kx].y=y1;
			linex[kx++].vis=1;//标记1就是下边
			X[kx]=x2;
			linex[kx].x1=x1;
			linex[kx].x2=x2;
			linex[kx].y=y2;
			linex[kx++].vis=0;//标记0就是上边
			Y[ky]=y1;
			liney[ky].y1=y1;
			liney[ky].y2=y2;
			liney[ky].x=x1;
			liney[ky++].vis=1;//标记1就是左边
			Y[ky]=y2;
			liney[ky].y1=y1;
			liney[ky].y2=y2;
			liney[ky].x=x2;
			liney[ky++].vis=0;//标记1就是左边
		}
		lisanx(X);
		buildtree(1,1,tree);
		sort(linex+1,linex+2*n+1,cmp);
		int sumlenx=0;
		int lastx=0;
		for(int i=1;i<=2*n;i++)
		{
			if(linex[i].vis)
			{
				update(1,collx[linex[i].x1],collx[linex[i].x2],1);
				sumlenx+=abs(query(1,1,tree)-lastx);
				lastx=query(1,1,tree);
			}
			else
			{
				update(1,collx[linex[i].x1],collx[linex[i].x2],-1);
				sumlenx+=abs(query(1,1,tree)-lastx);
				lastx=query(1,1,tree);
			}
		}
		lisany(Y);
		buildtree(1,1,tree);
		sort(liney+1,liney+2*n+1,cmp1);
		int sumleny=0;
		int lasty=0;
		for(int i=1;i<=2*n;i++)
		{
			if(liney[i].vis)
			{
				update1(1,colly[liney[i].y1],colly[liney[i].y2],1);
				sumleny+=abs(query1(1,1,tree)-lasty);
				lasty=query1(1,1,tree);
			}
			else
			{
				update1(1,colly[liney[i].y1],colly[liney[i].y2],-1);
				sumleny+=abs(query1(1,1,tree)-lasty);
				lasty=query1(1,1,tree);
			}
		}
		printf("%d\n",sumlenx+sumleny);
	}
	return 0;
}


 

/*
此题优化完毕。15MS。
只需多记录下该区间的线段数就可以只扫描一遍
也就是说此题可以分为区间合并+先扫描。
还有求线段长度也有很大改善,之前我用的是找到整段为0或者整段覆盖的方法。
其实不用,只需一个PushUp.线段经过排序,同一条线段肯定是先进后出。
进得话操作我想大家都会,出的话就看他是否为叶子,如果不是他的信息由他的
左右儿子决定。仔细揣摩PushUp.
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define maxn 5008//矩形个数
#define lson 2*id,l,mid
#define rson 2*id+1,mid,r
int X[2*maxn],kx,k;
int binary_search(int a)
{
	int l=1,r=kx-1;
	while(l < r)
	{
		int mid=(l+r) >> 1;
		if(X[mid] >= a)
		{
			r=mid;
		}
		else l=mid + 1;
	}
	return l;
}
struct Line
{
	int x1,x2,y;
	bool vis;
}line[2*maxn];
bool cmp(Line a,Line b)
{
	return a.y<b.y;
}
struct ST
{
	int l,r,len,add;
	int rcov,lcov,line;
}st[8*maxn];
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l+1==r)
	{
		st[id].add=st[id].len=st[id].rcov=st[id].lcov=st[id].line=0;
		return;
	}
	int mid=(l+r)>>1;
	buildtree(lson);
	buildtree(rson);
	st[id].add=st[id].len=st[id].rcov=st[id].lcov=st[id].line=0;
}
void PushUp(int id)
{
	if(st[id].add >= 1)
	{
		st[id].len = X[st[id].r] - X[st[id].l];
		st[id].lcov = st[id].rcov = st[id].line = 1;
	}
	else if(st[id].l+1 == st[id].r) 
	{
		st[id].len = 0;
		st[id].lcov = st[id].rcov = st[id].line = 0;
	}
	else 
	{
		st[id].len = st[2*id].len + st[2*id+1].len;
		st[id].line = st[2*id].line + st[2*id+1].line - (st[2*id].rcov && st[2*id+1].lcov);
		st[id].rcov = st[2*id+1].rcov;
		st[id].lcov = st[2*id].lcov;
	}
}
void update(int id,int l,int r,int ope)
{
	if(st[id].l == l && st[id].r == r)
	{
		st[id].add += ope;
		if(st[id].add >= 1)
		{
			st[id].line = st[id].rcov = st[id].lcov = 1;
			st[id].len = X[r]-X[l];
		}
		else if(l+1 == r)//////// 叶子
		{
			st[id].line = st[id].rcov = st[id].lcov = 0;
			st[id].len = 0;
		}
		else 
		{
			st[id].len = st[2*id].len + st[2*id+1].len;
			st[id].rcov = st[2*id+1].rcov;
			st[id].lcov = st[2*id].lcov;
			st[id].line = st[2*id].line + st[2*id+1].line - (st[2*id].rcov && st[2*id+1].lcov);
		}
		return;
	}
	if(st[2*id].r >= r)
	{
		update(2*id,l,r,ope);
		PushUp(id);
		return;
	}
	if(st[2*id+1].l <= l)
	{
		update(2*id+1,l,r,ope);
		PushUp(id);
		return;
	}
	update(2*id,l,st[2*id].r,ope);
	update(2*id+1,st[2*id+1].l,r,ope);
	PushUp(id);
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int x1,y1,x2,y2;
		kx=1;int k=1;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			X[kx++]=x1;
			X[kx++]=x2;
			line[k].x1=x1;
			line[k].x2=x2;
			line[k].y=y1;
			line[k++].vis=1;
			line[k].x1=x1;
			line[k].x2=x2;
			line[k].y=y2;
			line[k++].vis=0;
		}
		sort(X+1,X+kx);
		sort(line+1,line+k,cmp);
		buildtree(1,1,kx-1);
		int sum=0,lx=0,ly=line[1].y;
		for(int i=1;i<k;i++)
		{
			if(line[i].vis)
			{
				sum+=(line[i].y-ly)*st[1].line*2;
				update(1,binary_search(line[i].x1),binary_search(line[i].x2),1);
				sum+=abs(st[1].len-lx);
				ly=line[i].y;
				lx=st[1].len;
			}
			else
			{
				sum+=(line[i].y-ly)*st[1].line*2;
				update(1,binary_search(line[i].x1),binary_search(line[i].x2),-1);
				sum+=abs(st[1].len-lx);
				ly=line[i].y;
				lx=st[1].len;
			} 
		 }
		printf("%d\n",sum);
	}
	return 0;
}


### 使用 C++ 读取 MATLAB 的 `.m` 文件 为了实现这一目标,通常有两种主要的方法: #### 方法一:通过调用 MATLAB Engine API MATLAB 提供了一个名为 MATLAB Engine API 的接口,允许外部应用程序启动 MATLAB 并执行命令。对于 C++ 来说,可以利用 `engine.h` 头文件来加载并运行 `.m` 文件中的函数。 ```cpp #include "engine.h" #include <iostream> int main() { // 启动MATLAB引擎 Engine *ep; if (!(ep = engOpen(""))) { std::cerr << "无法连接到MATLAB引擎\n"; return EXIT_FAILURE; } // 将工作目录设置为包含.m文件的位置 engEvalString(ep, "cd('path_to_your_m_file_directory');"); // 调用.m文件内的函数 engEvalString(ep, "addpath(genpath('path_to_additional_toolboxes'));"); double a = 5.0, b = 3.0; mxArray* result; // 假设要调用的是 You 函数 engPutVariable(ep, "a", mxCreateDoubleScalar(a)); engPutVariable(ep, "b", mxCreateDoubleScalar(b)); engPutVariable(ep, "flag", mxCreateLogicalScalar(0)); engEvalString(ep, "[r,h] = You(a,b,flag);"); // 获取返回的结果 result = engGetVariable(ep, "r"); double output_r = mxGetPr(result)[0]; result = engGetVariable(ep, "h"); double output_h = mxGetPr(result)[0]; std::cout << "Result from MATLAB: r=" << output_r << ", h=" << output_h << "\n"; // 关闭MATLAB引擎 engClose(ep); return EXIT_SUCCESS; } ``` 这种方法依赖于安装有 MATLAB 运行环境,并且需要编译链接特定版本的库文件[^1]。 #### 方法二:将 M 文件转换成共享库 (DLL 或者其他形式) 另一种方式是先将`.m`文件打包成为动态链接库(DLL),之后再由C++程序去调用这个库里的功能。这涉及到创建一个独立的应用程序或组件,它可以在不打开完整的MATLAB界面的情况下被其它语言所使用。具体操作可以通过MATLAB Compiler工具箱完成,在构建过程中会自动生成所需的头文件和导入库[^2]。 这两种方案各有优缺点,前者更灵活但可能性能稍差;后者效率较高却增加了部署复杂度。选择哪种取决于实际应用场景的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值