和上道差不多,离线处理,然后扫一遍序列,扫到就数先不增加,上个相同的数+1,上上个数-1,用BIT维护前缀和

静态差错要多练练。。。

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<iostream>
 7 #define inc(i,l,r) for(int i=l;i<=r;i++)
 8 #define dec(i,l,r) for(int i=l;i>=r;i--)
 9 #define link(x) for(edge *j=h[x];j;j=j->next)
10 #define mem(a) memset(a,0,sizeof(a))
11 #define ll long long
12 #define succ(x) (1<<x)
13 #define lowbit(x) (x&(-x))
14 #define NM 1000000+5
15 using namespace std;
16 int read(){
17     int x=0,f=1;char ch=getchar();
18     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
19     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
20     return x*f;
21 }
22 int ans[NM],tmp[NM],l[NM],r[NM],next[NM],h[NM],_x,n,m,p,a[NM],t=0;
23 bool cmp(int x,int y){
24     return r[x]<r[y];
25 }
26 void upd(int x,int t){
27     for(;x<=n;x+=lowbit(x))a[x]+=t;
28 }
29 int sum(int x){
30     int s=0;
31     for(;x;x-=lowbit(x))s+=a[x];
32     return s;
33 }
34 int main(){
35     freopen("data.in","r",stdin);
36     n=read();p=read();m=read();
37     inc(i,1,n){
38         _x=read();next[i]=h[_x];h[_x]=i;
39     }
40     inc(i,1,m){
41         l[i]=read();r[i]=read();tmp[i]=i;
42     }
43     sort(tmp+1,tmp+1+m,cmp);
44     inc(i,1,m){
45         for(;t<=r[tmp[i]];t++){
46             if(next[t])upd(next[t],1);
47             if(next[next[t]])upd(next[next[t]],-1);
48         }
49         ans[tmp[i]]=sum(r[tmp[i]])-sum(l[tmp[i]]-1);
50     }
51     inc(i,1,m)printf("%d\n",ans[i]);
52     return 0;
53 }
View Code

 

posted on 2016-02-13 16:10  onlyRP  阅读(185)  评论(0编辑  收藏  举报