#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=5e5+5,mod=1e9+7;
int t,n,q,a[N],tree[N],bef[N],ans[N];
map<int,int>pos;
string s;
vector<pair<int,int> >V[N];
void update(int ind,int val){
for(ind;ind<=n;ind+=ind&(-ind)) tree[ind]+=val;
}
int getans(int ind){
int pas=0;
for(ind;ind>=1;ind-=ind&(-ind)) pas+=tree[ind];
return pas;
}
main(){
// t=1;
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;
V[l].push_back({r,i});
}
for(int i=n;i>=1;i--){
bef[i]=pos[a[i]];
if(bef[bef[i]] ) update(bef[bef[i]],-2);
if(bef[bef[bef[i]]]) update(bef[bef[bef[i]]],1);
if(bef[i])update(bef[i],1);
for(int j=0;j<V[i].size();j++){
ans[V[i][j].s] = getans(V[i][j].f);
}
pos[a[i]] = i;
}
for(int i=1;i<=q;i++) cout<<ans[i]<<endl;
}
Compilation message
poklon.cpp: In function 'void update(long long int, long long int)':
poklon.cpp:12:6: warning: statement has no effect [-Wunused-value]
12 | for(ind;ind<=n;ind+=ind&(-ind)) tree[ind]+=val;
| ^~~
poklon.cpp: In function 'long long int getans(long long int)':
poklon.cpp:16:6: warning: statement has no effect [-Wunused-value]
16 | for(ind;ind>=1;ind-=ind&(-ind)) pas+=tree[ind];
| ^~~
poklon.cpp: At global scope:
poklon.cpp:19:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
19 | main(){
| ^~~~
poklon.cpp: In function 'int main()':
poklon.cpp:36:16: 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]
36 | for(int j=0;j<V[i].size();j++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
11980 KB |
Output is correct |
2 |
Correct |
9 ms |
11980 KB |
Output is correct |
3 |
Correct |
11 ms |
12108 KB |
Output is correct |
4 |
Correct |
20 ms |
12360 KB |
Output is correct |
5 |
Correct |
273 ms |
17984 KB |
Output is correct |
6 |
Correct |
336 ms |
18144 KB |
Output is correct |
7 |
Correct |
591 ms |
23924 KB |
Output is correct |
8 |
Correct |
935 ms |
29980 KB |
Output is correct |
9 |
Correct |
1258 ms |
35780 KB |
Output is correct |
10 |
Correct |
1653 ms |
41740 KB |
Output is correct |