#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 1e6 + 5;
unordered_map<int, int> check;
struct node
{
int v, id;
} e[N];
bool cmp(node a, node b)
{
if (a.v == b.v)
return a.id < b.id;
return a.v < b.v;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int n, m, tot, top;
long long ans = 9e18;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> tot;
for (int j = 1; j <= tot; j++)
top++, cin >> e[top].v, e[top].id = i;
}
sort(e + 1, e + 1 + n, cmp);
int i = 1, j = 0, cnt = 0, p1 = 1, p2 = n;
while (i <= n)
{
while (cnt < m && j < n)
{
j++;
if (!check[e[j].id])
cnt++;
check[e[j].id]++;
}
if (ans > e[j].v - e[i].v && cnt == m)
ans = e[j].v - e[i].v, p1 = i, p2 = j;
check[e[i].id]--;
if (!check[e[i].id])
cnt--;
i++;
}
cout << e[p2].v - e[p1].v << endl;
}
双指针写法,在别处已经AC了,在这里全RE,并且在我的VScode里也是RE,什么原因呢?