比赛 |
20160407 |
评测结果 |
AAAAAAAAAA |
题目名称 |
HH的项链 |
最终得分 |
100 |
用户昵称 |
一個人的雨 |
运行时间 |
2.084 s |
代码语言 |
C++ |
内存使用 |
27.02 MiB |
提交时间 |
2016-04-07 11:26:30 |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=1000000+10;
int Ans[maxn];
struct A{
int l,r,id;
int block;
bool operator < (const A &a) const{
if (block==a.block) return r<a.r;
return block<a.block;
}
void read(int i){
scanf("%d%d",&l,&r);
id=i;
}
}ask[maxn];
int cnt[maxn],a[maxn],n,m,blo;
int main(){
freopen("diff.in","r",stdin);
freopen("diff.out","w",stdout);
scanf("%d",&n); blo=sqrt(n);
for (int i=1;i<=n;++i) scanf("%d",&a[i]);
scanf("%d",&m);
for (int i=1;i<=m;++i){
ask[i].read(i);
ask[i].block=(ask[i].l-1)/blo+1;
}
sort(ask+1,ask+m+1);
cnt[a[1]]++;
for (int i=1,l=1,r=1,ans=1;i<=m;++i){
while (r>ask[i].r){
cnt[a[r]]--;
if (!cnt[a[r]]) ans--;
r--;
}
while (r<ask[i].r){
r++;
cnt[a[r]]++;
if (cnt[a[r]]==1) ans++;
}
while (l>ask[i].l){
l--;
cnt[a[l]]++;
if (cnt[a[l]]==1) ans++;
}
while (l<ask[i].l){
cnt[a[l]]--;
if (cnt[a[l]]==0) ans--;
l++;
}
Ans[ask[i].id]=ans;
}
for (int i=1;i<=m;++i) printf("%d\n",Ans[i]);
return 0;
}