#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
int n, q, cn, a[N + 5], ans[N + 5];
struct Node
{
int l, r, pos;
};
vector<Node> qu;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen ("file.inp","r",stdin);
// freopen ("file.out","w",stdout);
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= q; i++)
{
int l, r;
cin >> l >> r;
qu.push_back({l, r, i});
}
cn = sqrt(n);
sort(qu.begin(), qu.end(), [&](Node a, Node b)
{ return a.l / cn < b.l / cn || (a.l / cn == b.l / cn && a.r < b.r); });
int l = 1, r = 0, now = 0;
unordered_map<int, int> mp;
for (int i = 0; i < q; i++)
{
while (l < qu[i].l)
{
int x = --mp[a[l]];
if (x == 2)
now++;
else if (x == 1)
now--;
++l;
}
while (l > qu[i].l)
{
--l;
int x = ++mp[a[l]];
if (x == 2)
now++;
else if (x == 3)
now--;
}
while (r > qu[i].r)
{
int x = --mp[a[r]];
if (x == 2)
now++;
else if (x == 1)
now--;
r--;
}
while (r < qu[i].r)
{
r++;
int x = ++mp[a[r]];
if (x == 2)
now++;
else if (x == 3)
now--;
}
ans[qu[i].pos] = now;
}
for (int i = 1; i <= q; i++)
cout << ans[i] << "\n";
return 0;
}
// hay
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
4 ms |
2648 KB |
Output is correct |
5 |
Correct |
208 ms |
5560 KB |
Output is correct |
6 |
Correct |
214 ms |
5580 KB |
Output is correct |
7 |
Correct |
602 ms |
9924 KB |
Output is correct |
8 |
Correct |
1154 ms |
15296 KB |
Output is correct |
9 |
Correct |
1676 ms |
16332 KB |
Output is correct |
10 |
Correct |
2197 ms |
19636 KB |
Output is correct |