Submission #631245

# Submission time Handle Problem Language Result Execution time Memory
631245 2022-08-17T21:42:51 Z inksamurai Poklon (COCI17_poklon) C++17
140 / 140
910 ms 42204 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3phCa4T ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

template<class S,S (*op)(S,S),S (*e)()>
struct segtree{
	int n;
	vec(S) seg;
	segtree(int _n){
		init(_n);
	}
	void init(int _n){
		n=1;
		while(n<=_n){
			n*=2;
		}
		seg=vec(S)(2*n);
		n=_n;
	}
	S prod(int node,int l,int r,int _l,int _r){
		if(l>=_r or r<=_l){
			return e();
		}
		if(_l<=l and r<=_r){
			return seg[node];
		} 
		int m=(l+r)>>1;
		return op(prod(node*2,l,m,_l,_r),prod(node*2+1,m,r,_l,_r));
	}
	S prod(int l,int r){
		return prod(1,0,n,l,r);
	}
	void set(int node,int l,int r,int pos,const S&x){
		if(l==r-1){
			seg[node]=x;
			return;
		}
		int m=(l+r)/2;
		if(pos<m){
			set(node*2,l,m,pos,x);
		}else{
			set(node*2+1,m,r,pos,x);
		}
		seg[node]=op(seg[node*2],seg[node*2+1]);
	}
	void set(int pos,const S&x){
		set(1,0,n,pos,x);
	}
	S get(int pos){
		return prod(1,0,n,pos,pos+1);
	}
};

int op(int l,int r){return l+r;}
int e(){return 0;}

signed main(){
_3phCa4T;
	int n,q;
	cin>>n>>q;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	vec(vec(pii)) qry(n);
	rep(i,q){
		int l,r;
		cin>>l>>r;
		l-=1,r-=1;
		qry[l].pb(pii(r,i));
	}
	vi pns(q);
	std::map<int,int> _lst,__lst,___lst;
	rep(i,n){
		_lst[a[i]]=n;
		__lst[a[i]]=n;
		___lst[a[i]]=n;
	}
	segtree<int,op,e> seg(n+1);
	per(i,n){
		int v=a[i];
		int nnj=_lst[v];
		int nj=__lst[v];
		int j=___lst[v];
		seg.set(nnj,1);
		seg.set(nj,-1);
		seg.set(j,0);
		// if(i==0) print(nj);
		for(auto p:qry[i]){
			pns[p.se]=seg.prod(i,p.fi+1);
		}
		___lst[v]=nj;
		__lst[v]=nnj;
		_lst[v]=i;
	}
	rep(i,q){
		cout<<(pns[i])<<"\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 5 ms 724 KB Output is correct
5 Correct 119 ms 9172 KB Output is correct
6 Correct 120 ms 9096 KB Output is correct
7 Correct 321 ms 17876 KB Output is correct
8 Correct 528 ms 28860 KB Output is correct
9 Correct 711 ms 35536 KB Output is correct
10 Correct 910 ms 42204 KB Output is correct