1878: [SDOI2009]HH的项链
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=1878Description
HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此, 他的项链变得越来越长。有一天,他突然提出了一个问题:某一段贝壳中,包含了多少种不同 的贝壳?这个问题很难回答。。。因为项链实在是太长了。于是,他只好求助睿智的你,来解 决这个问题。
Input
第一行:一个整数N,表示项链的长度。 第二行:N个整数,表示依次表示项链中贝壳的编号(编号为0到1000000之间的整数)。 第三行:一个整数M,表示HH询问的个数。 接下来M行:每行两个整数,L和R(1 ≤ L ≤ R ≤ N),表示询问的区间。
Output
M行,每行一个整数,依次表示询问对应的答案。
Sample Input
6
1 2 3 4 3 5
3
1 2
3 5
2 6
Sample Output
2
2
4
HINT
对于20%的数据,N ≤ 100,M ≤ 1000;
对于40%的数据,N ≤ 3000,M ≤ 200000;
对于100%的数据,N ≤ 50000,M ≤ 200000。
题意
题解:
在线用主席树,离线就树状数组就好了
排序之后,可以保证一个区间内的数,最多出现一次
代码:
//qscqesze #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 1000006 #define mod 1000000007 #define eps 1e-9 #define PI acos(-1) const double EP = 1E-10 ; int Num; //const int inf=0x7fffffff; const ll inf=999999999; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* struct node { int x,y; int id,ans; }; bool cmp1(node a,node b){if(a.x==b.x)return a.y<b.y;return a.x<b.x;} bool cmp2(node a,node b){return a.id<b.id;} int Mx,a[maxn],d[maxn],p[maxn],next[maxn]; node q[maxn]; int n; int lowbit(int x){return x&(-x);} void updata(int x,int v) { while(x<=n) { d[x]+=v; x+=lowbit(x); } } int ask(int x) { int ans = 0; while(x) { ans+=d[x]; x-=lowbit(x); } return ans; } int main() { n=read(); for(int i=1;i<=n;i++) a[i]=read(),Mx=max(Mx,a[i]); for(int i=n;i>=1;i--) next[i]=p[a[i]],p[a[i]]=i; for(int i=1;i<=Mx;i++) if(p[i])updata(p[i],1); int m=read(); for(int i=1;i<=m;i++) { q[i].x=read(); q[i].y=read(); q[i].id = i; } sort(q+1,q+1+m,cmp1); int l = 1; for(int i=1;i<=m;i++) { while(l<q[i].x) { if(next[l])updata(next[l],1); l++; } q[i].ans = ask(q[i].y)-ask(q[i].x-1); } sort(q+1,q+1+m,cmp2); for(int i=1;i<=m;i++) printf("%d\n",q[i].ans); }