80分求助啊,模仿区间合并做的,不知道哪里出现了问题!!
查看原帖
80分求助啊,模仿区间合并做的,不知道哪里出现了问题!!
819682
Exile_Code楼主2023/5/23 21:39
#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
bool mycmp(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first) {
        return b.second < a.second;
    }
    return a.first < b.first;
}

int main()
{

	int count = 0;
	int l;
	set<pair<int, int>>sort;
	int m;
	cin >> l >> m;
	int x1, x2;
	for (int i = 0; i < m; i++)
	{
		cin >> x1 >> x2;
		sort.insert(make_pair(x1,1));
		sort.insert(make_pair(x2,-1));
	}
	int begin, end;
	int result = 0;
	for (auto e : sort)
	{
		if (count == 0)
			begin = e.first;

		if (e.second == -1)
			end = e.first;

		count +=e.second;
		if (count == 0)
			result += end - begin + 1;

	}

	cout << l+1-result << endl;
	return 0;
}
2023/5/23 21:39
加载中...