给出两个由整数组成的集合A, B,计算A ∪ B中包含多少个整数。
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1409
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
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct pos { int x, y; } a[500017]; bool cmp(pos a, pos b) { if(a.x == b.x) return a.y > b.y; return a.x < b.x; } int main() { int t; int n, m; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 0; i < n; i++) { scanf("%d%d",&a[i].x,&a[i].y); } scanf("%d",&m); for(int i = n; i < m+n; i++) { scanf("%d%d",&a[i].x,&a[i].y); } sort(a,a+n+m,cmp); int ans = 0; int pre = 0; for(int i = 0; i < n+m; i++) { if(a[i].x > pre) { pre = a[i].x; } if(pre <= a[i].y) { ans+=a[i].y-pre+1; pre = a[i].y+1; } } printf("%d\n",ans); } return 0; }
原文地址:http://blog.csdn.net/u012860063/article/details/41789741