//
// Created by gint on 2025/7/11.
//
#include <solution/3169.h>
#include <stdio.h>
#include <stdlib.h>
int cmpMeetings(const void *a, const void *b)
{
    int *ma = *(int **)a;
    int *mb = *(int **)b;
    if (ma[0] != mb[0])
        return ma[0] - mb[0];
    return ma[1] - mb[1];
}

void mergeMeeting(int *meetingA, int *meetingB)
{
    meetingA[0] = meetingA[0] < meetingB[0] ? meetingA[0] : meetingB[0];
    meetingA[1] = meetingA[1] > meetingB[1] ? meetingA[1] : meetingB[1];
}

int countDays(int days, int **meetings, int meetingsSize, int *meetingsColSize)
{
    int count = 0;
    qsort(meetings, meetingsSize, sizeof(int *), cmpMeetings);

    for (int i = 1; i < meetingsSize; i++)
        if (meetings[i - 1][1] >= meetings[i][0])
            mergeMeeting(meetings[i], meetings[i - 1]);

    for (int i = 1; i < meetingsSize; i++)
        if (meetings[i - 1][0] != meetings[i][0])
            count += meetings[i][0] - meetings[i - 1][1] - 1;

    if (meetings[0][0] != 1)
        count += meetings[0][0] - 1;

    if (meetings[meetingsSize - 1][1] < days)
        return count + days - meetings[meetingsSize - 1][1];

    return count;
}
