#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define int ll
#define s second
#define f first
using namespace std;
const int N = 5000005,Bl = 350;
int a[N],res,ans[N];
pair<pair<int,int>,int> d[N];
map <int,int> cnt;
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.f/Bl < b.f.f/Bl;
return a.f.s < b.f.s;
}
void add(int idx){
cnt[a[idx]]++;
if (cnt[a[idx]] == 2) res++;
if (cnt[a[idx]] == 3) res--;
}
void del(int idx){
cnt[a[idx]]--;
if (cnt[a[idx]] == 1) res--;
if (cnt[a[idx]] == 2) res++;
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,q;
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;
d[i] = {{l,r},i};
}
sort(d+1,d+q+1,cmp);
int L=0,R = -1;
for (int i = 1; i <= q; i++){
int l = d[i].f.f,r = d[i].f.s,id = d[i].s;
while(L > l) add(--L);
while(R < r) add(++R);
while(L < l) del(L++);
while(R > r) del(R--);
ans[id] = res;
}
for (int i=1;i<=q;i++)
cout<<ans[i]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
9 ms |
340 KB |
Output is correct |
4 |
Correct |
42 ms |
536 KB |
Output is correct |
5 |
Correct |
2344 ms |
4396 KB |
Output is correct |
6 |
Correct |
2951 ms |
4376 KB |
Output is correct |
7 |
Execution timed out |
5056 ms |
8144 KB |
Time limit exceeded |
8 |
Execution timed out |
5018 ms |
12432 KB |
Time limit exceeded |
9 |
Execution timed out |
5057 ms |
16120 KB |
Time limit exceeded |
10 |
Execution timed out |
5088 ms |
19948 KB |
Time limit exceeded |