[SDOI2009]HH的项链
程序员文章站
2024-03-03 16:06:28
...
补档博客 木有题解(这次正经点了。。。)
Code:
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
using namespace std;
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T> void chkmax(T &x,T y){x=x>y?x:y;}
template<typename T> void read(T &num){
char c=getchar();T f=1;num=0;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
num*=f;
}
template<typename T> void qwq(T x){
if(x>9)qwq(x/10);
putchar(x%10+'0');
}
template<typename T> void write(T x){
if(x<0){x=-x;putchar('-');}
qwq(x);putchar('\n');
}
int n;
int co[500010];int tree[500010];
inline int lowbit(int x){
return x&(-x);
}
inline void change(int number,int x){
while(number<=n){
tree[number]+=x;
number+=lowbit(number);
}
return;
}
inline int query(int number){
int ans=0;
while(number){
ans+=tree[number];
number-=lowbit(number);
}
return ans;
}
int ans[500010];int hashs[1000010];
struct wzy{
int l,r,pos;
}que[500010];
inline bool cmp(wzy a,wzy b){
return a.r<b.r;
}
int main(){
read(n);
rep(i,1,n){read(co[i]);}
int m;read(m);
rep(i,1,m){read(que[i].l);read(que[i].r);que[i].pos=i;}
sort(que+1,que+m+1,cmp);
int evenbao=1;
rep(i,1,m){
for(int j=evenbao;j<=que[i].r;j++){
if(hashs[co[j]]){change(hashs[co[j]],-1);}
change(j,1);hashs[co[j]]=j;
}
evenbao=que[i].r+1;
ans[que[i].pos]=query(que[i].r)-query(que[i].l-1);
}
rep(i,1,m){write(ans[i]);}
return 0;
}