#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 5, maxsz = 1e6 + 5, b = 500;
ll n, q, a[maxn], res = 0;
ll cnt[maxsz], ans[maxn];
struct Query
{
ll l, r, id;
} qu[maxn];
void add(ll x) {
res -= (cnt[x] == 2);
cnt[x]++;
res += (cnt[x] == 2);
}
void del(ll x) {
res -= (cnt[x] == 2);
cnt[x]--;
res += (cnt[x] == 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
}
for (ll i = 1; i <= q; i++) {
cin >> qu[i].l >> qu[i].r;
qu[i].id = i;
}
sort(qu + 1, qu + q + 1, [](Query p, Query q) {
ll x = ((p.l - 1) / b + 1), y = (q.l - 1) / b + 1;
return (x < y || (x == y && p.r < q.r));
});
ll l = 1, r = 0;
for (ll i = 1; i <= q; i++) {
while(l < qu[i].l) {
del(a[l]);
l++;
}
while(l > qu[i].l) {
l--;
add(a[l]);
}
while(r < qu[i].r) {
r++;
add(a[r]);
}
while(r > qu[i].r) {
del(a[r]);
r--;
}
ans[qu[i].id] = res;
}
for (ll i = 1; i <= q; i++) {
cout << ans[i] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
5 ms |
2652 KB |
Output is correct |
5 |
Correct |
80 ms |
9548 KB |
Output is correct |
6 |
Correct |
80 ms |
9308 KB |
Output is correct |
7 |
Correct |
240 ms |
13516 KB |
Output is correct |
8 |
Incorrect |
11 ms |
4444 KB |
Output isn't correct |
9 |
Incorrect |
11 ms |
4320 KB |
Output isn't correct |
10 |
Incorrect |
11 ms |
4444 KB |
Output isn't correct |