ACM/ICPC 徐州网络赛 G Trace set+vector

https://www.jisuanke.com/contest/1557?view=challenges

There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ) means the wave is a rectangle whose vertexes are ( 00 , 00 ), ( xx , 00 ), ( 00 , yy ), ( xx , yy ). Every time the wave will wash out the trace of former wave in its range and remain its own trace of ( xx , 00 ) -> ( xx , yy ) and ( 00 , yy ) -> ( xx , yy ). Now the toad on the coast wants to know the total length of trace on the coast after n waves. It's guaranteed that a wave will not cover the other completely.

Input

The first line is the number of waves n(n \le 50000)n(n≤50000).

The next nn lines,each contains two numbers xx yy ,( 0 < x0<x , y \le 10000000y≤10000000 ),the ii-th line means the ii-th second there comes a wave of ( xx , yy ), it's guaranteed that when 1 \le i1≤i , j \le nj≤n ,x_i \le x_jxi​≤xj​ and y_i \le y_jyi​≤yj​ don't set up at the same time.

Output

An Integer stands for the answer.

Hint:

As for the sample input, the answer is 3+3+1+1+1+1=103+3+1+1+1+1=10

样例输入复制

3
1 4
4 1
3 3

样例输出复制

10

 

题意:后个海浪会覆盖前个海浪的一部分,所求轨迹长度就是矩形边缘线的长度(矩形左上角到右上角的长度+矩形右下角到右上角的长度)。

题解:对x坐标和y坐标分别计算。用vector分别记录x,y坐标,从最后一波海浪往前计算,如果在set中能找到第一个比这波海浪x值小的,那么ans就加上二者差值,否则ans就加上当前海浪的x值,y同样如此

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector> 
using namespace std;
#define inf 0x3f3f3f
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;
typedef long long ll;
const int maxn = 1e5+5;
int n,q;
set<int>st;
vector<int>x,y;
int main(){
	while(~scanf("%d",&n)){
		st.clear();
		x.clear(),y.clear();
		int l,r;
		for(int i = 1; i <= n; i++){
			scanf("%d%d",&l,&r);
			x.push_back(l);
			y.push_back(r);
		}
		ll ans = 0;
		for(int i = x.size()-1; i >= 0; i--){
			auto it = st.lower_bound(x[i]);
			if(it != st.begin()){ //找到第一个比x[i]大的数 
				it--;  //减后就是第一个比x[i]小的数 
				ans += (x[i] - *it);
			}else{
				ans += x[i]; 
			}
			st.insert(x[i]);
		}
		st.clear();
		for(int i = y.size()-1; i >= 0; i--){
			auto it = st.lower_bound(y[i]);
			if(it != st.begin()){
			it--;
			ans += (y[i] - *it); 
		}else{
			ans += y[i];
		}
		st.insert(y[i]);
	}
	cout<<ans<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值