G Trace
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.
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.
An Integer stands for the answer.
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
題目來源
題意很好理解就是有矩形的海浪,衝過來有先後順序,後過來的海浪會將之前的海浪給覆蓋掉,求最後的剩餘的海浪的長度,
這一道題目我當時的思路是太麻煩了導致最後也沒有a掉,但是後來問了一下別人的思路真是神的一匹,我當時排了一個順序
然後出現了兩個數值都與我們要求的數值有關就是二分裏面再寫一個log(n)的時間複雜度,顯然這樣是沒有什麼比較好的解決方案,我看了一下別人的解決思路就感覺但是自己的那個排序就是敗筆,,,
我們先看y
正解是這樣的我們可以按照海浪來的順序從後往前看,到了i個海浪時,後面的海浪如果沖刷掉這個海浪,這個海浪的y掉後面海浪中y最大的那個就可以了,就是當前剩下的,我們可以用set來維護一下順序,用二分來查找比當前海浪(y)小的最大的哪一個y就是可以了
x的話類似
對於這一道題目,我感覺如果是出現了需要二分後在優化時間的可能是自己的排序出現了問題,前面的思維也可以就是有點麻煩,隊友使用二分加上RMQ過掉的;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ; #define rep(i,s,n) for(int i=s;i<=n;i++) #define per(i,n,s) for(int i=n;i>=s;i--) const int Max = 2e5+10; ll x[Max],y[Max]; int n; ll d(ll a[]){ set <ll> q; set <ll> :: iterator it; ll sum=0; per(i,n,1){ it=q.lower_bound(a[i]); if(it==q.begin()) sum+=a[i]; else { it--; sum+=a[i]-*it; } q.insert(a[i]); } return sum; } int main(){ scanf("%d",&n); rep(i,1,n) scanf("%lld %lld",&x[i],&y[i]); ll ans=d(x)+d(y); printf("%lld\n",ans); return 0; }