1. 程式人生 > >Trace 2018徐州icpc網絡賽 思維+二分

Trace 2018徐州icpc網絡賽 思維+二分

text max mar sam res rom orm cpc was

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 xxyy ,( 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_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

題目來源

ACM-ICPC 2018 徐州賽區網絡預賽

題意:有n次海浪,每次海浪會產生兩段軌跡(0,y)到(x,y)和(x,0)到(x,y),後面的海浪會將前面的在(0,0)到(x,y)之間區域的海浪軌跡覆蓋掉,問最後剩余的軌跡長度

分析:每段軌跡有效的貢獻是在此軌跡被接下來所有軌跡覆蓋一次後剩余的軌跡長度

  所以我們考慮直接從後面往前面枚舉

  最後的軌跡長度肯定是可以直接加上的,沒有其余的海浪能覆蓋掉他,然後將其的軌跡點放進集合

  接下來遍歷到的點看集合只要看集合裏比他小的最大的數就是能覆蓋掉他多少長度,減去這個長度就行

AC代碼:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5+10;
const ll mod = 2e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll f( vector<ll> e ) {
    ll sz = e.size(), ans = 0;
    set<ll> s;
    for( ll i = sz-1; i >= 0; i -- ) {
        set<ll>::iterator it = s.lower_bound(e[i]);
        if( it == s.begin() ) {
            ans += e[i];
        } else {
            it --;
            ans += e[i] - *it;
        }
        s.insert(e[i]);
    }
    return ans;
}
int main() {
    ll x, y, n;
    vector<ll> e1, e2;
    scanf("%lld",&n);
    while( n -- ) {
        scanf("%lld%lld",&x,&y);
        e1.push_back(x), e2.push_back(y);
    }
    printf("%lld\n",f(e1)+f(e2));
    return 0;
}

  

Trace 2018徐州icpc網絡賽 思維+二分