看起来似乎是被 n=2×105,x=1 的数据卡了...
但是问题出在哪里呢 /dk
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>
#define ai_jiang signed main()
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define drep(a, b, c) for (int a = b; a >= c; a--)
#define all(a) a.begin(), a.end()
using namespace std;
using ll = long long;
using poly = vector<ll>;
namespace ai
{
constexpr int N = 2e5 + 5, M = 1e9;
ll t, n, x, a[N << 1];
unordered_map<ll, ll> xx;
set<ll> v;
}
using namespace ai;
ai_jiang
{
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(0);
cin >> t;
while (t--)
{
cin >> n >> x;
xx.clear();
v.clear();
rep(i, 1, n) cin >> a[i], xx[a[i]]++, v.insert(a[i]);
for (auto i : v)
{
if (xx[i] > 1)
{
if (!xx[i + x])
v.insert(i + x);
xx[i + x] += xx[i] - 1, xx[i] = 1;
}
}
if (*v.begin() != 0)
cout << 0 << '\n';
else
{
auto it = v.begin();
int f = 0;
rep(i, 0, (int)v.size() - 2)
{
auto k = it;
++k;
int val = *it, vl = *k;
if (val + 1 < vl)
{
cout << val + 1 << '\n';
f = 1;
break;
}
it++;
}
if(!f) cout<<(*(--v.end()))+1<<'\n';
}
}
return 0;
}