2018徐州icpc网络赛B题

海浪覆盖问题,但不存在一个海浪被另一个海浪完全覆盖的问题,所以我们可以从最后一个海浪求,若最后一个海浪的宽度为x,然后以他为起点,可以用一个set来存储,宽度比set的首元素小的直接加上,比他大的加上他们的差值,没比较一个结果就加上set(关键就是没有完全被覆盖的)

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

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
#include<string>
#include<vector>
using namespace std;
long long gao(vector<int> vec) {
	int sz = vec.size();
	set<int>st;
	long long ans = 0;
	for (int i = sz - 1; i >= 0; i--) {
		set<int>::iterator it = st.lower_bound(vec[i]);
		if (it == st.begin()) {
			ans += vec[i];
		}
		else {
			it--;
			ans += vec[i] - *it;
		}
		st.insert(vec[i]);
	}
	return ans;
}
int main() {
	int n;
	while (scanf("%d", &n) == 1) {
		vector<int>vec1, vec2;
		int x, y;
		while (n--) {

			scanf("%d%d", &x, &y);
			vec1.push_back(x);
			vec2.push_back(y);
		}
		cout << gao(vec1) + gao(vec2) << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/yihanyifan/article/details/82595730
今日推荐