#include <bits/stdc++.h>
//#include <iostream>
//#include <vector>
#define ll long long
#define pb push_back
#define int ll
#define s second
#define f first
using namespace std;
const int N = 500005,Bl = 770;
int a[N],res,ans[N],cnt[N];
pair<pair<int,int>,int> d[N];
vector <pair<int,int> > v;
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],v.pb({a[i],i});
sort(v.begin(),v.end());
a[v[0].s] = 1;
for (int i = 1; i<v.size();i++){
a[v[i].s] = a[v[i - 1].s];
if (v[i].f > v[i-1].f) a[v[i].s]++;
}
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;
while(L > l) add(--L);
while(R < r) add(++R);
while(L < l) del(L++);
while(R > r) del(R--);
ans[d[i].s] = res;
}
for (int i=1;i<=q;i++)
cout<<ans[i]<<endl;
}
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:39:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 1; i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
340 KB |
Output is correct |
4 |
Correct |
18 ms |
772 KB |
Output is correct |
5 |
Correct |
318 ms |
6152 KB |
Output is correct |
6 |
Correct |
346 ms |
6092 KB |
Output is correct |
7 |
Correct |
750 ms |
11784 KB |
Output is correct |
8 |
Correct |
1225 ms |
17460 KB |
Output is correct |
9 |
Correct |
1717 ms |
23168 KB |
Output is correct |
10 |
Correct |
2477 ms |
28828 KB |
Output is correct |