2018 ACM-ICPC徐州网络赛 G.Trace 傻逼检测题

G.Trace

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_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

傻逼题,傻逼做不出来的题。。。。。 QAQ
浓缩题意:不断给(0,0)位置覆盖长x宽y的矩形纸片,问最后露出来的边长长度之和是多少(x、y坐标轴不算)
重点是:保证没有一个矩形会被完全覆盖,也就是说,有几个矩形,就会露出几个“尖尖”,如图

那问题就很简单了(但我还是想不到):
1.从最后一块矩形开始从后向前遍历
2.横边和竖边分别算最后相加
3.对于横边:找到已遍历的边中距离当前边最近的一条(二分查找),给sum加上它们的高度差。竖边同理,给sum加上长度差

AC代码:
#include <iostream>
#include <set>
#include <algorithm>
#include <cstdio>
using namespace std;

int x[50001], y[50001];
set<int> rx, ry;

int main(){
    int n, a, b, i, cnt = 0;
    long long sum = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++){
        scanf("%d%d", &a, &b);
        x[cnt] = a;
        y[cnt++] = b;
    }
    for (i = n-1; i >= 0; i--){
        set<int>::iterator t = ry.upper_bound(y[i]);
        if (t == ry.begin()) sum += y[i];
        else sum += y[i] - *(--t);
        ry.insert(y[i]);
    }
    for (i = n-1; i >= 0; i--){
        set<int>::iterator t = rx.upper_bound(x[i]);
        if (t == rx.begin()) sum += x[i];
        else sum += x[i] - *(--t);
        rx.insert(x[i]);
    }
    cout << sum;
    return 0;
}

 

 
posted @ 2018-09-11 02:01  Shepard  阅读(358)  评论(0编辑  收藏  举报