Professional Documents
Culture Documents
Segment Tree
Segment Tree
const ll N=1e6+7;
ll seg[4*N];
ll lazy[4*N];
bool marked[4*N];
struct seg{
ll sum,pref,suff,ans;
};
const ll N=1e6+10;
seg t[4*N];
DSU
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
struct Query{
int l,r,idx;
bool operator<(Query other)const{
return r<other.r;
}
};
int main(){
IOS
int n;
cin>>n;
vector<int> a(n+1);
for(int i=1;i<=n;++i){
cin>>a[i];
}
int q;
cin>>q;
vector<Query> Q(q+1);
for(int i=1;i<=q;++i){
int l,r;
cin>>l>>r;
Q[i].l=l;
Q[i].r=r;
Q[i].idx=i;
}
sort(Q.begin(),Q.end());
map<int,int> last;
vector<int> res(q+1);
int j=0;
for(int i=1;i<=q;++i){
while(j<Q[i].r){
j++;
update(1,0,n,j,a[j]);
update(1,0,n,last[a[j]],0);
last[a[j]]=j;
}
res[Q[i].idx]=query(1,0,n,Q[i].l,Q[i].r);
}
for(int i=1;i<=q;++i){
cout<<res[i]<<"\n";
}
return 0;
}