# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
60127 | Adhyyan1252 | Poklon (COCI17_poklon) | C++11 | 1742 ms | 64880 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |