ACM-ICPC 2018 徐州赛区网络预赛 G


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(n50000).

The next nn lines,each contains two numbers xx yy ,( 0 < x0<x , y \le 10000000y10000000 ),the ii-th line means the ii-th second there comes a wave of ( xx , yy ), it's guaranteed that when 1 \le i1i , j \le njn ,x_i \le x_jxixj and y_i \le y_jyiyj 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

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

 

思路:

既然新加的点会覆盖之前比他短的点,那么我们可以对平行于x轴与y轴的线按倒序维护。

复杂度O( nlogn )

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
typedef long long ll;
ll height(std::vector<int> v){
    int sz=v.size();
    set<int>st;
    ll ans=0;
    for (int i=sz-1; i>=0; --i){
        set<int>::iterator it = st.lower_bound(v[i]);
        if(it==st.begin())  ans+=v[i];
        else {
            it--;
            ans+=v[i]-(*it);
        }
        st.insert(v[i]);
    }
    return ans;
}
int main(){
    // freopen("in.txt","r",stdin);
    int x,y,n;
    std::vector<int> xx,yy;
    while(scanf("%d",&n)==1){
        xx.clear(),yy.clear();
        while(n--){
            scanf("%d%d",&x,&y);
            xx.push_back(x);
            yy.push_back(y);
        }
        printf("%lld\n",height(xx)+height(yy));
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/acerkoo/p/9622450.html
今日推荐