标签:
对于20%的数据,N ≤ 100,M ≤ 1000;
对于40%的数据,N ≤ 3000,M ≤ 200000;
对于100%的数据,N ≤ 50000,M ≤ 200000。
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 #ifdef WIN32 14 #define OT "%I64d" 15 #else 16 #define OT "%lld" 17 #endif 18 using namespace std; 19 typedef long long LL; 20 const int MAXN = 50011; 21 const int MAXS = 1000011; 22 const int MAXM = 200011; 23 int num[MAXN],pre[MAXS],shu[MAXS]; 24 int n,m,ans; 25 26 struct wen{ 27 int l,r,ans,jilu; 28 }q[MAXM]; 29 30 inline int getint() 31 { 32 int w=0,q=0; 33 char c=getchar(); 34 while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); 35 if (c==‘-‘) q=1, c=getchar(); 36 while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); 37 return q ? -w : w; 38 } 39 40 inline bool cmp(wen q,wen qq){ return q.r<qq.r;} 41 42 inline bool comp(wen q,wen qq){ return q.jilu<qq.jilu; } 43 44 inline void add(int x,int val){ 45 while(x<=n){ 46 shu[x]+=val; 47 x+=x&(-x); 48 } 49 } 50 51 inline int query(int x){ 52 int total=0; 53 while(x>0) { 54 total+=shu[x]; 55 x-=x&(-x); 56 } 57 return total; 58 } 59 60 inline void solve(){ 61 n=getint(); for(int i=1;i<=n;i++) num[i]=getint(); 62 m=getint(); for(int i=1;i<=m;i++) q[i].l=getint(),q[i].r=getint(),q[i].jilu=i; 63 sort(q+1,q+m+1,cmp); 64 int now=1; 65 for(int i=1;i<=n;i++) { 66 if(pre[num[i]]) add(pre[num[i]],-1); 67 add(i,1); 68 pre[num[i]]=i; 69 if(q[now].r==i) { 70 ans=query(i); 71 } 72 while(q[now].r==i) { q[now].ans=ans-query(q[now].l-1); now++; } 73 } 74 sort(q+1,q+m+1,comp); 75 for(int i=1;i<=m;i++) printf("%d\n",q[i].ans); 76 } 77 78 int main() 79 { 80 solve(); 81 return 0; 82 }
标签:
原文地址:http://www.cnblogs.com/ljh2000-jump/p/5607110.html