对于20%的数据,N ≤ 100,M ≤ 1000;
对于40%的数据,N ≤ 3000,M ≤ 200000;
对于100%的数据,N ≤ 50000,M ≤ 200000。
Day2
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int num[50003],n,m,i,j; int next[50003]; int point[1000003]; int p[50003],b[50003]; int ans[200003]; int c[50003]; struct data { int l,r,x; };data q[200003]; int cmp(data x,data y) { return x.r<y.r; } int lowbit(int x) { return x&(-x); } int sum(int n) { int sum1=0; while(n>0) { sum1+=c[n]; n=n-lowbit(n); } return sum1; } void change(int l,int x) { while (l<=n) { c[l]=c[l]+x; l=l+lowbit(l); } } int main() { scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&num[i]); next[i]=point[num[i]]; if (next[i]==0) { change(i,1); b[i]=1; } point[num[i]]=i; } scanf("%d",&m); for (j=1;j<=m;j++) { scanf("%d%d",&q[j].l,&q[j].r); q[j].x=j; p[q[j].r]=1; } j=0; sort(q+1,q+m+1,cmp); for (i=1;i<=n;i++) { if (b[i]==0) { change(next[i],-1); change(i,1); b[next[i]]=0; b[i]=1; } if (p[i]==1) { while (q[j+1].r==i) { j++; ans[q[j].x]=sum(q[j].r)-sum(q[j].l-1); } } if (j==m) break; } for (i=1;i<=m;i++) printf("%d\n",ans[i]); return 0; }