#include<bits/stdc++.h>
// time 10:13, 10:38
using namespace std;
#define NMAX 5000005
int n, q;
int a[NMAX];
vector<vector<int> > ind;
struct segtree{
vector<int> t, l;
int sz;
segtree(int n){
sz = n;
t.resize(4*n); l.resize(4*n);
}
inline void prop(int i, int s, int e){
if(l[i] == 0) return;
if(s != e){
l[i*2] += l[i];
l[i*2+1] += l[i];
}
t[i] += l[i];
l[i] = 0;
}
void upd(int l, int r, int val){
upd(1, 0, sz-1, l, r, val);
}
void upd(int i, int s, int e, int le, int r, int val){
prop(i, s, e);
if(s >= le && e <= r){
l[i] += val;
prop(i, s, e);
return;
}
if(s > r || e < le) return;
int m = (s + e)/2;
upd(i*2, s, m, le, r, val);
upd(i*2+1, m+1, e, le, r, val);
t[i] = t[i*2] + t[i*2+1];
}
int query(int indx){
return query(1, 0, sz-1, indx);
}
int query(int i, int s, int e, int indx){
prop(i, s, e);
if(s == e) return t[i];
int m = (s + e)/2;
if(indx <= m) return query(i*2, s, m, indx);
else return query(i*2+1, m+1, e, indx);
}
};
struct actions{
int x;
int t, b;
int type;
bool operator<(const actions& other){
if(x != other.x) return x < other.x;
return type < other.type;
}
};
int main(){
cin>>n>>q;
map<int, int> mp;
int cnt = 1;
for(int i = 0; i < n; i++){
cin>>a[i];
if(mp[a[i]] == 0){
mp[a[i]] = cnt;
a[i] = cnt++;
}else{
a[i] = mp[a[i]];
}
}
ind = vector<vector<int> > (cnt+1);
for(int i = 0; i < n; i++){
ind[a[i]].push_back(i);
}
mp.clear();
vector<actions> nodes;
for(int i = 0; i < q; i++){
int l, r; cin>>l>>r; l--, r--;
nodes.push_back({l, r, -1, i});
}
for(int i = 1; i < cnt; i++){
if(ind[i].size() <= 1) continue;
for(int j = 0; j < ind[i].size()-1; j++){
// left point is j, right point is j+2
int l = (j == 0?0:(ind[i][j-1]+1));
int r = (j == ind[i].size()-2)?n:(ind[i][j+2]-1);
nodes.push_back({l, r, ind[i][j+1], -1});
nodes.push_back({ind[i][j]+1, r, ind[i][j+1], -2});
}
}
sort(nodes.begin(), nodes.end());
int ans[q];
segtree st(cnt+2);
for(actions a: nodes){
if(a.type < 0){
st.upd(a.b, a.t, (a.type==-1)?1:-1);
}else{
ans[a.type] = st.query(a.t);
}
}
for(int i = 0; i < q; i++){
cout<<ans[i]<<endl;
}
}
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:97:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < ind[i].size()-1; j++){
~~^~~~~~~~~~~~~~~~~
poklon.cpp:100:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
int r = (j == ind[i].size()-2)?n:(ind[i][j+2]-1);
~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
248 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
488 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
688 KB |
Output isn't correct |
4 |
Incorrect |
21 ms |
1020 KB |
Output isn't correct |
5 |
Incorrect |
356 ms |
11528 KB |
Output isn't correct |
6 |
Incorrect |
390 ms |
13004 KB |
Output isn't correct |
7 |
Incorrect |
760 ms |
25400 KB |
Output isn't correct |
8 |
Incorrect |
1211 ms |
31768 KB |
Output isn't correct |
9 |
Incorrect |
1620 ms |
55972 KB |
Output isn't correct |
10 |
Incorrect |
1742 ms |
64880 KB |
Output isn't correct |