#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define ll long long
#define int long long
#define left (node<<1),l,((l+r)>>1)
#define right ((node<<1)|1),((l+r)>>1) + 1,r
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: In function 'bool cmp(std::pair<std::pair<long long int, long long int>, long long int>, std::pair<std::pair<long long int, long long int>, long long int>)':
poklon.cpp:16:28: warning: comparison of constant '707' with boolean expression is always true [-Wbool-compare]
16 | return a.f.f/Bl < b.f.f<Bl;
| ~~~~~~~~~~~~~~~~^~~
poklon.cpp:16:21: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
16 | return a.f.f/Bl < b.f.f<Bl;
| ^
poklon.cpp: At global scope:
poklon.cpp:28:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main (){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Runtime error |
156 ms |
63912 KB |
Execution killed with signal 11 |
4 |
Runtime error |
11 ms |
972 KB |
Execution killed with signal 11 |
5 |
Runtime error |
54 ms |
9932 KB |
Execution killed with signal 11 |
6 |
Runtime error |
48 ms |
9920 KB |
Execution killed with signal 11 |
7 |
Runtime error |
96 ms |
19480 KB |
Execution killed with signal 11 |
8 |
Runtime error |
136 ms |
28944 KB |
Execution killed with signal 11 |
9 |
Runtime error |
199 ms |
38596 KB |
Execution killed with signal 11 |
10 |
Runtime error |
246 ms |
48228 KB |
Execution killed with signal 11 |