第四届山东省赛 J Boring Counting [主席树]【数据结构】

前端之家收集整理的这篇文章主要介绍了第四届山东省赛 J Boring Counting [主席树]【数据结构】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

题目链接 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2054
——————————————————————————————————————————
Boring Counting
Time Limit: 3000 MS Memory Limit: 32768 K
Total Submit: 70(22 users) Total Accepted: 6(6 users) Rating: Special Judge: No

Description
In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries,for each query,please tell us among [L,R],how many Pi is not less than A and not greater than B( L<= i <= R). In other words,your task is to count the number of Pi (L <= i <= R,A <= Pi <= B).

Input
In the first line there is an integer T (1 < T <= 50),indicates the number of test cases.

For each case,the first line contains two numbers N and M (1 <= N,M <= 50000),the size of sequence P,the number of queries. The second line contains N numbers Pi(1 <= Pi <= 10^9),the number sequence P. Then there are M lines,each line contains four number L,R,A,B(1 <= L,R <= n,1 <= A,B <= 10^9)

Output
For each case,at first output a line ‘Case #c:’,c is the case number start from 1. Then for each query output a line contains the answer.

Sample Input
1
13 5
6 9 5 2 3 6 8 7 3 2 5 1 4
1 13 1 10
1 13 3 6
3 6 3 6
2 8 2 8
1 9 1 9

Sample Output
Case #1:
13
7
3
6
9

——————————————————————————————————————————

解题思路:

其实就是一个简单的主席树入门,奈何练习赛的时候刚学主席树不到2天,还没理解主席树.于是GG了

其实仔细想想啊,其实 和 SPOJ DQUERY 一样,而且更简单一点,
我们不需要删除操作,只需要保存所有的历史版本,然后找 @H_403_51@rt[l]rt[r] 之间在 [A,B] 区间的数的个数就行了

只需要离散化后,一次向树上更新即可,

但要注意查询的时候,
离散化A是**大于等于**A的第一个元素
离散化B是**小于等于**B的最后一个元素

附本题代码
————————————————————————————————————————————

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. inline int read(){
  6. int x=0,f=1;char ch=getchar();
  7. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  8. while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
  9. return x*f;
  10. }
  11.  
  12. /************************************************/
  13. const int N = 5e4+7;
  14.  
  15. int a[N],b[N],sz;
  16.  
  17. int rt[N],ls[N*40],rs[N*40],sum[N*40],tot;
  18. void build(int &rt,int l,int r){
  19. rt=++tot;
  20. sum[rt]=0;
  21. if(l>=r)return;
  22. int m=((r-l)>>1)+l;
  23. build(ls[rt],l,m);
  24. build(rs[rt],m+1,r);
  25. }
  26.  
  27. void update(int &rt,int r,int last,int pos){
  28. rt=++tot;
  29. ls[rt]=ls[last];
  30. rs[rt]=rs[last];
  31. sum[rt]=sum[last]+1;
  32. if(l>=r)return;
  33. int m=((r-l)>>1)+l;
  34. if(pos<=m) update(ls[rt],m,ls[last],pos);
  35. else update(rs[rt],r,rs[last],pos);
  36. }
  37.  
  38. int query(int rt,int L,int R){
  39. if(L>R)return 0;
  40. if(L<=l&&r<=R)return sum[rt]-sum[last];
  41. int m=((r-l)>>1)+l;
  42. int ans=0;
  43. if(L<=m) ans+=query(ls[rt],L,R);
  44. if(R> m) ans+=query(rs[rt],R);
  45. return ans;
  46. }
  47.  
  48. int lb(int x){
  49. return lower_bound(b+1,b+sz+1,x)-b;
  50. }
  51. map<int,int>mmp;
  52. int main(){
  53. int _ = 1,kcase = 0,flag;
  54. scanf("%d",&_);
  55. while(_--){
  56. tot=0;mmp.clear();
  57.  
  58. int n=read(),m=read();
  59. for(int i=1;i<=n;i++) b[i]=a[i]=read(),mmp[a[i]]=1;
  60.  
  61. sort(b+1,b+n+1);
  62. sz = unique(b+1,b+n+1)-(b+1);
  63. // printf("sz = %d\n",sz);
  64. // for(int i=1;i<=sz;i++) printf("b[%d]=%d ",i,b[i]);puts("");
  65.  
  66. build(rt[0],1,sz);
  67.  
  68. for(int i=1;i<=n;i++)a[i]=lb(a[i]);
  69. for(int i=1;i<=n;i++)update(rt[i],sz,rt[i-1],a[i]);
  70. // printf("%d\n",lb(0));
  71. printf("Case #%d:\n",++kcase);
  72. while(m--){
  73. l=read(),r=read(),L=read(),R=read();
  74. if(L>R){puts("0");continue; }
  75. flag=0;
  76. if(!mmp[R])flag=1;
  77. if(R<b[1]) R=-1; else R = lb(R)-flag;
  78. if(L>b[sz])L=1e9+77;else L = lb(L);
  79.  
  80. // printf("%d %d\n",R);
  81. printf("%d\n",query(rt[r],rt[l-1],R));
  82. }
  83. }
  84. return 0;
  85. }

猜你在找的数据结构相关文章