#include<bits/stdc++.h>
#define el cout<<"\n"
#define f0(i,n) for(int i=0;i<n;++i)
#define f1(i,n) for(int i=1;i<=n;++i)
#define maxn 500005
using namespace std;
int n, m, block, res;
int a[maxn], b[maxn], d[maxn], ans[maxn];
struct query
{
int l, r, id;
}q[maxn];
bool cmp (query q1, query q2)
{
if (q1.l/block == q2.l/block) return q1.r < q2.r;
return q1.l < q2.l;
}
void sub(int x)
{
d[a[x]]--;
if (d[a[x]] == 1) res--;
if (d[a[x]] == 2) res++;
}
void add(int x)
{
d[a[x]]++;
if (d[a[x]] == 2) res++;
if (d[a[x]] == 3) res--;
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
f1 (i, n) cin >> a[i], b[i] = a[i];
sort(b+1, b+n+1);
f1 (i, n) a[i] = lower_bound(b+1, b+n+1, a[i]) - b;
f1 (i, m)
{
int l, r;
cin >> l >> r;
q[i] = {l, r, i};
}
block = sqrt(n);
sort(q+1, q+m+1, cmp);
int L = 0, R = 0;
f1 (i, m)
{
while (L<q[i].l) sub(L++);
while (L>q[i].l) add(--L);
while (R<q[i].r) add(++R);
while (R>q[i].r) sub(R--);
ans[q[i].id] = res;
}
f1 (i, m) cout << ans[i] << '\n';
}
Compilation message
poklon.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
30 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
472 KB |
Output is correct |
5 |
Correct |
189 ms |
4440 KB |
Output is correct |
6 |
Correct |
192 ms |
4648 KB |
Output is correct |
7 |
Correct |
502 ms |
9376 KB |
Output is correct |
8 |
Correct |
918 ms |
14108 KB |
Output is correct |
9 |
Correct |
1322 ms |
18880 KB |
Output is correct |
10 |
Correct |
1809 ms |
23408 KB |
Output is correct |