#include <bits/stdc++.h>
#define suiii ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define co cout<<
#pragma GCC optimize("O3,Ofast,unroll-loops")
#pragma GCC target("avx2,sse3,sse4,avx")
using namespace std;
//stuff
int n,q;
struct meh{
int a,b,c;
};
int num;
bool cmp(meh a,meh b){
if(a.a/num<b.a/num) return 1;
else if(a.a/num>b.a/num) return 0;
else{
if(a.b<b.b) return 1;
else if(a.b>b.b) return 0;
else return 1;
}
}
int arr[1000001];
unordered_map<int,int>mp;
int ans=0;
meh qu[1000001];
void add(int val){
mp[val]++;
if(mp[val]==3) ans--;
if(mp[val]==2) ans++;
}
void remo(int val){
mp[val]--;
if(mp[val]==1) ans--;
if(mp[val]==2) ans++;
}
int anss[1000001];
void solve(){
cin>>n>>q;
num=sqrt(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<q;i++){
cin>>qu[i].a>>qu[i].b;
qu[i].a--,qu[i].b--;
qu[i].c=i;
}
sort(qu,qu+q,cmp);
int l,r;
l=0;
r=-1;
for(int i=0;i<q;i++){
while(l<qu[i].a){
remo(arr[l]);
l++;
}
while(l>qu[i].a){
l--;
add(arr[l]);
}
while(r<qu[i].b){
r++;
add(arr[r]);
}
while(r>qu[i].b){
remo(arr[r]);
r--;
}
anss[qu[i].c]=ans;
}
for(int i=0;i<q;i++){
co anss[i]<<'\n';
}
}
int main()
{
// suiii
ll t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
8 ms |
348 KB |
Output is correct |
5 |
Correct |
460 ms |
2388 KB |
Output is correct |
6 |
Correct |
463 ms |
2388 KB |
Output is correct |
7 |
Correct |
1284 ms |
4692 KB |
Output is correct |
8 |
Correct |
2469 ms |
6864 KB |
Output is correct |
9 |
Correct |
3578 ms |
8892 KB |
Output is correct |
10 |
Correct |
4842 ms |
11084 KB |
Output is correct |