#include <stdio.h> #include <stdlib.h> #define M_MAX 100 + 2 struct Area { int start; int end; }; int CompareArea(const void *elem1, const void *elem2) { return ((Area*)elem1)->start - ((Area*)elem2)->start; } int main() { Area area[M_MAX], temp; int L = 0; int M = 0; int count = 0; scanf("%d%d", &L, &M); for (int i = 0; i < M; ++i) { scanf("%d%d", &area[i].start, &area[i].end); } qsort(area, M, sizeof(Area), CompareArea);
temp = area[0]; for (int i = 1; i < M; ++i) { if (area[i].start <= temp.end) { if (area[i].end > temp.end) { temp.end = area[i].end; } } else { count += temp.end - temp.start + 1; temp = area[i]; } } count += temp.end - temp.start + 1;