There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xxx , yyy ) means the wave is a rectangle whose vertexes are ( 000 , 000 ), ( xxx , 000 ), ( 000 , yyy ), ( xxx , yyy ). Every time the wave will wash out the trace of former wave in its range and remain its own trace of ( xxx , 000 ) -> ( xxx , yyy ) and ( 000 , yyy ) -> ( xxx , yyy ). 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≤50000)n(n \le 50000)n(n≤50000).
The next nnn lines,each contains two numbers xxx yyy ,( 0<x0 < x0<x , y≤10000000y \le 10000000y≤10000000 ),the iii-th line means the iii-th second there comes a wave of ( xxx , yyy ), it's guaranteed that when 1≤i1 \le i1≤i , j≤nj \le nj≤n ,xi≤xjx_i \le x_jxi≤xj and yi≤yjy_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=103+3+1+1+1+1=10
样例输入
3
1 4
4 1
3 3
样例输出
10
题目来源
stl炫酷的技法学到了。
思路就是分别求x,y轴方向上的和。然后倒着找,找最接近它的并且小于他的,加上这段差值就好了。
开始想用树状数组最值来写,后来发现跟求和的那种不太一样,就不知道怎么写。然后看了一种stl。
说真的,set挺简单的,但是自己用的少就没怎么用过。这一篇这样写代码又短又好看。比用线段树维护短多了。
学习了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n;
LL Love(vector<int>fuck){
LL ans=0;
set<int>s;
for(int i=n-1;i>=0;i--){
set<int>::iterator it=s.lower_bound(fuck[i]);
if(it==s.begin())///比当前列表中的所有数字都小
ans+=fuck[i];
else
ans+=fuck[i]-*(--it);
s.insert(fuck[i]);
}
return ans;
}
int main()
{
int x,y;
scanf("%d",&n);
vector<int>a,b;
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
a.push_back(x);
b.push_back(y);
}
/// cout<<"lovea"<<Love(a)<<" "<<"b:"<<Love(b)<<endl;
LL haha=Love(a)+Love(b);
printf("%lld\n",haha);
}