#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
ll n,q;
struct meh{
ll a,b,c;
};
ll 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;
}
}
ll arr[1000001];
unordered_map<ll,ll>mp;
ll ans=0;
meh qu[1000001];
void add(ll val){
mp[val]++;
if(mp[val]==3) ans--;
if(mp[val]==2) ans++;
}
void remo(ll val){
mp[val]--;
if(mp[val]==1) ans--;
if(mp[val]==2) ans++;
}
ll 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);
ll 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 |
604 KB |
Output is correct |
5 |
Correct |
554 ms |
5344 KB |
Output is correct |
6 |
Correct |
511 ms |
5400 KB |
Output is correct |
7 |
Correct |
1424 ms |
10332 KB |
Output is correct |
8 |
Correct |
2828 ms |
15556 KB |
Output is correct |
9 |
Correct |
3930 ms |
20476 KB |
Output is correct |
10 |
Execution timed out |
5046 ms |
24580 KB |
Time limit exceeded |