#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
using namespace std;
const int Bl = 707,N = 500005;
pair<pair<int,int>,int> d[N];
pair<int,int> b[N];
int ans[N],res,a[N],cnt[N];
bool cmp(pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){
if (a.f.f/Bl == b.f.f/Bl) return a.f.s < b.f.s;
return a.f.f/Bl < b.f.f/Bl;
}
void add(int ind){
if (cnt[a[ind]] == 2) res--;
cnt[a[ind]]++;
if(cnt[a[ind]] == 2) res++;
}
void remove(int ind){
if (cnt[a[ind]] == 2) res--;
cnt[a[ind]]--;
if(cnt[a[ind]] == 2) res++;
}
main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,m;
cin>>n>>m;
for (int i=1;i<=n;i++){
cin>>b[i].f,b[i].s = i;
}
sort(b+1,b+n+1);
a[b[1].s] = 1;
for(int i =2;i<=n;i++){
a[b[i].s] = a[b[i - 1].s];
if (b[i].f > b[i - 1].f) a[b[i].s]++;
}
for (int i=1;i<=m;i++){
int l,r;
cin>>l>>r;
d[i] = {{l,r},i};
}
sort(d+1,d+m+1,cmp);
int L = 0,R = -1;
for (int i=1;i<=m;i++){
int l = d[i].f.f,r = d[i].f.s,idx = d[i].s;
while (L > l) add(--L);
while (R < r) add(++R);
while (L < l) remove(L++);
while (R > r) remove(R--);
ans[idx] = res;
}
for (int i=1;i<=m;i++)
cout<<ans[i]<<"\n";
}
Compilation message
poklon.cpp:24:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
24 | main (){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
8 ms |
468 KB |
Output is correct |
5 |
Correct |
184 ms |
3192 KB |
Output is correct |
6 |
Correct |
185 ms |
3168 KB |
Output is correct |
7 |
Correct |
487 ms |
6116 KB |
Output is correct |
8 |
Correct |
870 ms |
9068 KB |
Output is correct |
9 |
Correct |
1325 ms |
12104 KB |
Output is correct |
10 |
Correct |
1862 ms |
15076 KB |
Output is correct |