思路有问题吗?
用 fi 记录最近的相同的数字,如果在区间内有 fi 记录的数字就跳过去,然后下一个,代码如下,但为什么有问题呢?
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1.5 * 1e4;
int t, n, q, a[N], l, r, f[N];
signed main ()
{
ios::sync_with_stdio (0), cin.tie (0), cout.tie (0);
cin >> t;
++ t;
while (-- t)
{
cin >> n;
memset (f, 0, sizeof (f));
map <int, int> v;
for (int i = 1; i <= n; ++ i)
{
cin >> a[i];
if (v[a[i]])f[v[a[i]]] = i, v[a[i]] = 0;
else v[a[i]] = i;
}
cin >> q;
++ q;
while (-- q)
{
int c = 0;
cin >> l >> r;
for (int i = l; i <= r; ++ i)
{
if (f[i] == 0 || f[i] > r)++ c;
else i = f[i];
}
cout << c << '\n';
}
}
return 0;
}