离散化问题整理

ProblemP1496 火烧赤壁


给定多条线段,求所有线段的覆盖的长度和 给定多条线段,求所有线段的覆盖的长度和 给定多条线段,求所有线段的覆盖的长度和

对于全部的测试点,保证 1 ≤ n ≤ 2 × 1 0 4 , − 2 31 ≤ a ≤ b < 2 31 , − 2 31 ≤ a ≤ b < 2 31 ,且答案小于 2 31 . 对于全部的测试点,保证 1 \leq n \leq 2 \times 10^4,-2^{31} \leq a \leq b \lt 2^{31},−2^{31}≤a≤b<2^{31},且答案小于 2^{31}. 对于全部的测试点,保证1n2×104,231ab<231,231ab<231,且答案小于231.

Input
3
-1 1
5 11
2 9

Output
11

貌似没有用到离散化

一开始没有思路,看了大佬的思路,注意到将给定的点打乱匹配方式后,对答案没有影响,分别对左端点和右端点进行排序,然后暴力累加答案即可。

/*Love coding and thinking!*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back 
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;


typedef long long ll;
const int N=2e4+10;
int n,m,_;
/*
观察到了,改成左右端点的匹配方式,最后的答案也不会改变
*/
int a[N],b[N];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		scanf("%d",&b[i]);
	}
	
	sort(a,a+n);
	sort(b,b+n);
	ll res=0;
	for(int i=0;i<n;i++)
	{
		res+=b[i]-a[i];
		if(i&&a[i]<b[i-1])
		{
			res-=b[i-1]-a[i];
		}
	}
	cout<<res;
	return 0;
}

ProblemP1884 [USACO12FEB]Overplanting S


给定 N 个矩形 , 在笛卡尔坐标系 ( X 轴向右是正方向 , Y 轴向上是正方向 ) 求矩形的面积覆盖 ( 被重复覆盖区域面积只算一次 ) 给定N个矩形,在笛卡尔坐标系(X轴向右是正方向,Y轴向上是正方向)求矩形的面积覆盖(被重复覆盖区域面积只算一次) 给定N个矩形,在笛卡尔坐标系(X轴向右是正方向,Y轴向上是正方向)求矩形的面积覆盖(被重复覆盖区域面积只算一次)

很明显   N < = 1000 , 这题是扫描线模板题弱数据版本 扫描线   n < = 1 e 5 , 0 < = x 1 < x 2 < 1 0 9 , 0 < = y 1 < y 2 < = 1 0 9 。 很明显\,N<=1000,这题是扫描线模板题弱数据版本\\扫描线\, n<=1e5,0<=x1<x2<10^9,0<=y1<y2<=10^9。 很明显N<=1000,这题是扫描线模板题弱数据版本扫描线n<=1e5,0<=x1<x2<109,0<=y1<y2<=109


写了一个二维的火烧赤壁,自己写的10分的代码,离谱,确实写的不对,当之后的长方形和前边的长方形有面积并的时候没有处理,先咕着

/*Love coding and thinking!*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back 
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;


typedef long long ll;
const int N=2e4+10;
int n,m,_;

struct Node
{
	int x1,y1,x2,y2;
	ll area;
	bool operator <(const Node &a)const 
	{
		return x1<a.x1;
	}
}node[N];

int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d%d",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2);
		node[i].area=(node[i].x2-node[i].x1)*(node[i].y1-node[i].y2);
	}
		
	ll res=0;
	
	for(int i=0;i<n;i++)
	{
		if(node[i].x1>node[i-1].x2)continue;
		else if(node[i].y1<node[i-1].y2) continue;
		else if(node[i].y2<node[i-1].y2)
		{
			ll tx=node[i-1].x2-node[i].x1;
			ll ty=node[i].y1-node[i-1].y2;
			// debug(tx*ty);
			res-=tx*ty;
		}
		else
		{
			ll tx=node[i-1].x2-node[i].x1;
			ll ty=node[i].y1-node[i].y2;	
			// debug(tx*ty);
			res-=tx*ty;
		}
		res+=node[i].area;
		// debug(res);
	}
	cout<<res;	
	return 0;
}

ProblemP5490 【模板】扫描线


求 n 个矩形的面积并 求n个矩形的面积并 n个矩形的面积并

(咕着先)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值