【暴力】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int arr[1000000];
int main()
{
int n, m;
int a, b;
cin >> n >> m;
for (int i = 0; i < m; i ++ )
{
cin >> a >> b;
for (int j = a; j <= b; j ++ ) arr[j] ++ ;
}
for (int i = 1; i <= n; i ++ )
if (!arr[i] || arr[i] > 1)//要么没浇,要么浇多了
{
cout << i << ' ' << arr[i];
return 0;
}
puts("OK");
return 0;
}
【差分】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, m;
int b[N];
//b[i]记录第i天的浇水次数
int main()
{
scanf("%d%d", &n, &m);
while (m -- )
{
int l, r;
scanf("%d%d", &l, &r);
b[l] ++, b[r + 1] -- ;//差分
}
for (int i = 1; i <= n; i ++ )
{
b[i] += b[i - 1];
if (b[i] != 1)//不是浇一次水
{
printf("%d %d\n", i, b[i]);
return 0;
}
}
puts("OK");
return 0;
}