给出两个由整数组成的集合A,
B,计算A
∪ 
B中包含多少个整数。
3
1
7 7
1
3 3
2
1 2 3 4
1
2 3
2
1 2 4 6
3
1 3 6 7 9 10
2
4
9
//可以把A,B,都当成多条线段的集合,然后,排序,去重,那么最后结果就是问所有的线段包含了多少个整数。通过结构体把a,b绑定起来,然后排序,先按左边小的排,在按右边大的排,这样是为了覆盖重复的。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=200005;
int n,m;
struct point
{
    int x,y;
}f[maxn];
bool mycomp(const point &a,const point &b)
{
    if(a.x!=b.x) return a.x<b.x;
    else return a.y>b.y;
}
void solve()
{
    int i,ans = 0;
    int start=0;
    for(i=0;i<n+m;i++)
    {
        if(f[i].x>start) start=f[i].x;
        if (f[i].y>=start)
        {
            ans+=f[i].y-start+1;
            start = f[i].y+1;
        }
    }
    printf("%d\n",ans);
}
int main()
{
    int t,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++) scanf("%d %d",&f[i].x,&f[i].y);
        scanf("%d",&m);
        for(i=n;i<m+n;i++) scanf("%d %d",&f[i].x,&f[i].y);
        sort(f,f+n+m,mycomp);
        solve();
    }
    return 0;
}
 
原文地址:http://blog.csdn.net/u012773338/article/details/39029981