hihocoder 1305 区间求差

You are given two interval collections A and B. Collection A has N intervals [ A1A2 ], [ A3A4 ], ..., [ A2N-1A2N ] and collection B has M intervals [ B1B2 ], [ B3B4 ], ..., [ B2M-1B2M ]. Your task is to calculate the length of A - B.

For example if A = {[2, 5], [4, 10], [14, 18]} and B = {[1, 3], [8, 15]}, the length of A - B ({(3, 8), (15, 18]}) is 8.

Input

Line 1: Two integers N and M (1 ≤ NM ≤ 100000).

Line 2: 2*N integers, A1A2, ..., A2N (1 ≤ Ai ≤ 100000000).

Line 3: 2*M integers, B1B2, ..., B2M (1 ≤= Bi ≤ 100000000).

Output

The length of A - B.

Sample Input
3 2
2 5 4 10 14 18
1 3 8 15
Sample Output
8


滔滔说是sui题 但是我真的看了题解才会写

感觉他代码写的好巧妙 【捂脸

求一个区间有没有被覆盖就是看左端点数有没有多于右端点数

#include<stdio.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<limits>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f

using namespace std;

int n, m;
const int maxn = 100005;
typedef pair<int, pair<int, int> > point;
vector <point> vec;
int cnt[2];

int main()
{
    while(scanf("%d%d",&n,&m) != EOF){
        for(int i = 0; i < n; i++){
            int a, b;
            scanf("%d%d",&a,&b);
            point p = make_pair(a, make_pair(0, 1));
            point q = make_pair(b, make_pair(0, -1));
            vec.push_back(p);
            vec.push_back(q);
        }
        for(int i = 0; i < m; i++){
            int a, b;
            scanf("%d%d",&a, &b);
            point p = make_pair(a, make_pair(1, 1));
            point q = make_pair(b, make_pair(1, -1));
            vec.push_back(p);
            vec.push_back(q);
        }

        sort(vec.begin(), vec.end());
        cnt[0] = cnt[1] = 0;
        int ans = 0;
        for(int i = 0; i < vec.size() - 1; i++){
            cnt[vec[i].second.first] += vec[i].second.second;
            if(cnt[0] > 0 && cnt[1] == 0){
                ans += vec[i + 1].first - vec[i].first;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
真的是水题啊我好菜啊
posted @ 2017-10-17 20:09  wyboooo  阅读(108)  评论(0编辑  收藏  举报