[差分]It Rains Again

我果然是个初学者级别的five
但是今天level++啦(滑稽)

Description

Suppose that in a Cartesian coordinate system on an infinite plane, the x-axis represents the ground and there are n rainscreens (We don't consider their thickness.) in the sky.

Every rainscreen can be described as a segment which starts from theand (x1,y1)ends at (x2.y2). Now if it starts to rain from the infinite high sky, I want you to tell me how many units of the x-axis will not get rained on.

To simplify the problem,the rain can only move vertically whenever it falls.

Note that two rainscreens can overlap and intersect with each other, and there is no rainscreen which is placed vertically.

这个其实是差分
差分数组的前缀和是到这里总量
我们在div[x1]++,div[x2]--
这样每一段不受别的影响
那么如何处理最后的结果呢?
如果目前大于零(没有被雨淋上)那就ans++

Input

The first line contains one positive integer n (1 \leq n \leq 100000)n(1≤n≤100000).

Each of the next n lines contains four positive integers x_1, y_1, x_2, y_2 (1 < x_1 < x_2 < 100000, 1 < y_1,y_2 < 100000), representing a rainscreen which starts from the (x_1, y_1)and ends at (x_2, y_2).

Output

The only one integer - the number of units of the x-axis which will not get rained on.

Example

Input
5
1 2 2 1
1 1 2 2
3 3 4 3
5 1 6 3
6 3 7 2
Output
4

Note

Consider the example, we can draw a picture like below:

[差分]It Rains Again

It's easy to calculate the answer is 4.

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main(){
    int n,div[100050];
    cin>>n;
    memset(div,0,sizeof(div));
    for(int i=1;i<=n;i++){
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        div[a]++;
        div[c]--;
    }
    int cnt=0,ans=0;
    for(int i=0;i<=100000;i++){
        cnt+=div[i];
        if(cnt>0) ans++;
    }
    cout<<ans<<endl;
    return 0;
}
上一篇:javascript倒计时功能


下一篇:c#入门