# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
201494 | awlintqaa | Poklon (COCI17_poklon) | C++14 | 2617 ms | 47752 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.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 340
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll mod=1e9+7;
const ll inf=1e18;
const ld pai=acos(-1);
int n,q;
int a[500009];
//seg tree
int tree[2000009][2];
void upd(int node,int l,int r,int id,int x,int t){
if(l==r){
tree[node][t]+=x;
return ;
}
if(id<=mid)upd(node*2,l,mid,id,x,t);
else upd(node*2+1,mid+1,r,id,x,t);
tree[node][t]=tree[node*2][t]+tree[node*2+1][t];
}
int query(int node,int l,int r,int s,int e,int t){
if(s>r || e<l)return 0;
if(s<=l && e>=r)return tree[node][t];
return query(node*2,l,mid,s,e,t)+query(node*2+1,mid+1,r,s,e,t);
}
//filling last1 & last2
map<int,int>mp;
int last[500009][2];
void fill(){
for(int i=1;i<=n;i++){
last[i][0]=mp[a[i]];
last[i][1]=last[last[i][0]][0];
mp[a[i]]=i;
}
}
//calc ans
vpi v[500009];
map<int,int>bef;
int ans[500009];
void solve(){
for(int i=1;i<=n;i++){
upd(1,0,n,last[i][0],1,0);
upd(1,0,n,last[i][1],1,1);
if(bef[a[i]]){
int id=bef[a[i]];
upd(1,0,n,last[id][0],-1,0);
upd(1,0,n,last[id][1],-1,1);
}
bef[a[i]]=i;
for(auto u:v[i]){
int l=u.fi;
int id=u.se;
ans[id]=query(1,0,n,0,l-1,1)-query(1,0,n,0,l-1,0);
}
}
}
int main(){
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>a[i];
fill();
for(int i=0;i<q;i++){
int l,r;
cin>>l>>r;
v[r].pb({l,i});
}
solve();
for(int i=0;i<q;i++)cout<<ans[i]<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |