Trace-----ACM-ICPC 2018 徐州赛区网络预赛
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<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define mem(a,b) memset(b,sizeof(a),b)
#define ll long long
#define inf 0x3f3f3f
using namespace std;
const int maxn=100115;
struct node{
ll x,y;
}d[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
for(int i=n-1;i>=0;i--){
scanf("%lld%lld",&d[i].x,&d[i].y);
}
set<ll>s1;
set<ll>s2;
set<ll>::iterator it1,it2;
s1.clear();s2.clear();
ll sum=0;
for(int i=0;i<n;i++){
it1=s1.lower_bound(d[i].x);
it2=s2.lower_bound(d[i].y);
if(it1==s1.begin())sum+=d[i].x;
else{
it1--;
sum+=(d[i].x-*it1);
}
if(it2==s2.begin())sum+=d[i].y;
else{
it2--;
sum+=(d[i].y-*it2);
}
s1.insert(d[i].x);
s2.insert(d[i].y);
}
printf("%lld\n",sum);
}
}
推荐阅读
-
【计蒜客】2018ICPC徐州赛区网络赛H Ryuji doesn't want to study(树状数组)
-
ACM-ICPC 2018 徐州赛区网络预赛 H - Ryuji doesn't want to study
-
ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(线段树区间求和)
-
ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study—— 树状数组
-
ACM-ICPC 2018 徐州赛区网络预赛 H Ryuji doesn't want to study(线段树 两种做法)
-
【ACM-ICPC 2018 徐州赛区网络预赛】H题 Features Track ---- 树状数组
-
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (线段树维护)
-
计蒜客 Trace(2018 ICPC亚洲区域赛网络赛 徐州 G)(线段树)
-
2018 沈阳赛区网络预赛 F. Fantastic Graph 有上下界可行流
-
ACM-ICPC 2018 徐州赛区网络预赛 Trace