转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

2224: Boring Counting

Time Limit: 3 Sec  Memory Limit: 128 MB

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. 1
  2. 13 5
  3. 6 9 5 2 3 6 8 7 3 2 5 1 4
  4. 1 13 1 10
  5. 1 13 3 6
  6. 3 6 3 6
  7. 2 8 2 8
  8. 1 9 1 9

Sample Output

  1. Case #1:
  2. 13
  3. 7
  4. 3
  5. 6
  6. 9

HINT

 

Source

题意:求静态的区间[l,r]介于[A,B]的数的个数

这题其实是一道离线树状数组的水题,估计是因为我并不是很具备离线的思维吧,一般的离线都想不到。

好在主席树也能够完成这个操作,并且也只花了20分钟不到就1A了。

二分k的值,然后判断利用第k大来判断出A,B分别是第几大。然后随便搞。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <vector>
  8. using namespace std;
  9. #define w(i) T[i].w
  10. #define ls(i) T[i].ls
  11. #define rs(i) T[i].rs
  12. #define MAXN 100010
  13. int p[MAXN],a[MAXN],b[MAXN],root[MAXN];
  14. struct node{
  15. int ls,rs,w;
  16. node(){ls=rs=w=;}
  17. }T[MAXN*];
  18. int tot=;
  19. void Insert(int &i,int l,int r,int x){
  20. T[++tot]=T[i];
  21. i=tot;
  22. w(i)++;
  23. if(l==r)return;
  24. int mid=(l+r)>>;
  25. if(x<=mid)Insert(ls(i),l,mid,x);
  26. else Insert(rs(i),mid+,r,x);
  27. }
  28. int query(int lx,int rx,int l,int r,int k){
  29. if(l==r)return l;
  30. int ret=w(ls(rx))-w(ls(lx));
  31. int mid=(l+r)>>;
  32. if(ret>=k)return query(ls(lx),ls(rx),l,mid,k);
  33. else return query(rs(lx),rs(rx),mid+,r,k-ret);
  34. }
  35. bool cmp(int i,int j){
  36. return a[i]<a[j];
  37. }
  38. int main()
  39. {
  40. ios::sync_with_stdio(false);
  41. tot=;
  42. int n,m;
  43. int t;
  44. int cas=;
  45. scanf("%d",&t);
  46. while(t--){
  47. scanf("%d%d",&n,&m);
  48. for(int i=;i<=n;i++){
  49. scanf("%d",a+i);
  50. p[i]=i;
  51. }
  52. tot=;
  53. root[]=;
  54. sort(p+,p+n+,cmp);
  55. for(int i=;i<=n;i++)b[p[i]]=i;
  56. for(int i=;i<=n;i++){
  57. root[i]=root[i-];
  58. Insert(root[i],,n,b[i]);
  59. }
  60. printf("Case #%d:\n",cas++);
  61. while(m--){
  62. int l,r,x,y;
  63. scanf("%d%d%d%d",&l,&r,&x,&y);
  64. int lx=,rx=r-l+;
  65. int fx=-;
  66. int ans=;
  67. int flag =;
  68. int tmpx;
  69. while(lx<=rx){
  70. int mid = (lx+rx)>>;
  71. tmpx=a[p[query(root[l-],root[r],,n,mid)]];
  72. if(tmpx<=y){
  73. fx=mid;
  74. lx=mid+;
  75. }else rx=mid-;
  76. }
  77. if(fx==-)flag=;
  78. else ans+=fx;
  79. lx=,rx=r-l+;
  80. fx=-;
  81. while(lx<=rx){
  82. int mid = (lx+rx)>>;
  83. tmpx=a[p[query(root[l-],root[r],,n,mid)]];
  84. if(tmpx>=x){
  85. fx=mid;
  86. rx=mid-;
  87. }else lx=mid+;
  88. }
  89. if(fx==-)flag=;
  90. else ans=ans-fx+;
  91. if(flag)ans=;
  92. printf("%d\n",ans);
  93. }
  94. }
  95. return ;
  96. }

13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)的更多相关文章

  1. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...

  2. BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树

    [题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...

  3. BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树

    [题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...

  4. BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树

    BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...

  5. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解

    题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...

  6. P2617 Dynamic Rankings(树状数组套主席树)

    P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...

  7. [COGS257]动态排名系统 树状数组套主席树

    257. 动态排名系统 时间限制:5 s   内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...

  8. BZOJ 2141 排队(树状数组套主席树)

    解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...

  9. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

随机推荐

  1. CODEVS 1066/洛谷 P1514引水入城

    1066 引水入城 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 在一个遥远的国 ...

  2. VB6基本数据库应用(四):数据的提取,新增和修改

    列的第四篇,上一篇在:http://blog.csdn.net/jiluoxingren/article/details/9474661 数据的提取,新增和修改 由于在写第三章的时候没有充分考虑这一章 ...

  3. 【0】Laravel 5.1 简介

    1.简介 Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以 ...

  4. Ubuntu 修改 Apache2 用户组 方法

    检查/etc/apache2/envvars文件,发现其中需要使用/etc/apache2/envvars文件中的以下几个环境变量 export APACHE_RUN_USER=www-data ex ...

  5. js+jquery+html实现在三种不通的情况下,点击图片放大的效果

    js+jquery+html实现在三种不通的情况下,点击图片放大的效果. 三种情况分别是:图片的父元素宽高固定;  图片的宽高固定;  图片的父元素宽固定,高度不固定 第一种情况:图片的父元素宽高固定 ...

  6. 升级10.11后使用CocoaPod出现-bash: pod: command not found 解决办法-备

    升级10.11后,运行pod命令出现: -bash: pod: command not found 解决办法: sudo gem install -n /usr/local/bin cocoapods ...

  7. 163k地方门户网站系统团购定时结束限量控制

    #coding=utf8 #!/usr/bin/env python # 网站自动审核系统 import pymssql import re import sys import datetime im ...

  8. ORA-07217: sltln: environment variable cannot be evaluated及RMAN-06059

    备份脚本: RMAN> run { allocate channel c1 device type disk format '$BACKUP_HOME/level0/level0_%d_%s_% ...

  9. elasticsearh 中每个节点中需要有相同的插件

    elasticsearh 中每个节点中需要有相同的插件 [2016-09-13 19:25:24,049][INFO ][discovery.zen ] [node02] failed to send ...

  10. flex渐变色制作圆角橙色按钮

    <?xml version="1.0" encoding="utf-8"?> <s:SparkButtonSkin xmlns:fx=&quo ...