转载请注明出处: 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
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

HINT

 

Source

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

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

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

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

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
#define w(i) T[i].w
#define ls(i) T[i].ls
#define rs(i) T[i].rs
#define MAXN 100010
int p[MAXN],a[MAXN],b[MAXN],root[MAXN];
struct node{
int ls,rs,w;
node(){ls=rs=w=;}
}T[MAXN*];
int tot=;
void Insert(int &i,int l,int r,int x){
T[++tot]=T[i];
i=tot;
w(i)++;
if(l==r)return;
int mid=(l+r)>>;
if(x<=mid)Insert(ls(i),l,mid,x);
else Insert(rs(i),mid+,r,x);
}
int query(int lx,int rx,int l,int r,int k){
if(l==r)return l;
int ret=w(ls(rx))-w(ls(lx));
int mid=(l+r)>>;
if(ret>=k)return query(ls(lx),ls(rx),l,mid,k);
else return query(rs(lx),rs(rx),mid+,r,k-ret);
}
bool cmp(int i,int j){
return a[i]<a[j];
}
int main()
{
ios::sync_with_stdio(false);
tot=;
int n,m;
int t;
int cas=;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",a+i);
p[i]=i;
}
tot=;
root[]=;
sort(p+,p+n+,cmp);
for(int i=;i<=n;i++)b[p[i]]=i;
for(int i=;i<=n;i++){
root[i]=root[i-];
Insert(root[i],,n,b[i]);
}
printf("Case #%d:\n",cas++);
while(m--){
int l,r,x,y;
scanf("%d%d%d%d",&l,&r,&x,&y);
int lx=,rx=r-l+;
int fx=-;
int ans=;
int flag =;
int tmpx;
while(lx<=rx){
int mid = (lx+rx)>>;
tmpx=a[p[query(root[l-],root[r],,n,mid)]];
if(tmpx<=y){
fx=mid;
lx=mid+;
}else rx=mid-;
}
if(fx==-)flag=;
else ans+=fx;
lx=,rx=r-l+;
fx=-;
while(lx<=rx){
int mid = (lx+rx)>>;
tmpx=a[p[query(root[l-],root[r],,n,mid)]];
if(tmpx>=x){
fx=mid;
rx=mid-;
}else lx=mid+;
}
if(fx==-)flag=;
else ans=ans-fx+;
if(flag)ans=;
printf("%d\n",ans);
}
}
return ;
}

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. Shiro 权限框架使用总结

    我们首先了解下什么是shiro ,Shiro 是 JAVA 世界中新近出现的权限框架,较之 JAAS 和 Spring Security,Shiro 在保持强大功能的同时,还在简单性和灵活性方面拥有巨 ...

  2. pygame实现的黑白块游戏

    运行时需要pygame库. 下载地址:http://files.cnblogs.com/files/zzrom/white.zip 程序截图:

  3. 安装laravel

    # 安装laravel 安装composer #安装 curl -sS https://getcomposer.org/installer | php #添加到PATH sudo mv compose ...

  4. sql 字段先计算后再拿比对的字段进行比对 效率提升100倍

    关于日期索引的使用,不要计算后再对比,否则使用不了索引例如:以下执行不了索引,耗时很大 dywl=# explain analyze SELECT car_bill.billno,car_bill.b ...

  5. 栈应用之中缀表达式计算 MFC实现(计算器核心)

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 支持小数.阶乘.乘方.加减乘除.括号优先级运算,美化输出结果(显示结果末尾没有多余的0) void ...

  6. stm32的FSMC

    之前用的stm32f103rbt6,它是100引脚以内的,不带FSMC.驱动液晶屏或者SRAM要自己写时序方面的程序,比较麻烦.后来换成stm32f103zet6,带有FSMC.不过在学习FSMC的时 ...

  7. 【Android】Android布局中实现圆角边框

    设置corners_bg.xml 设置边框圆角可以在drawable-mdpi目录里定义一个xml: <?xml version="1.0" encoding="u ...

  8. 【转】将 Linux 应用程序移植到 64 位系统上

    原文网址:http://www.ibm.com/developerworks/cn/linux/l-port64.html 随着 64 位体系结构的普及,针对 64 位系统准备好您的 Linux® 软 ...

  9. 做一个有理想的IT人

    前段时间一直以来都在思考生命的价值的问题,一直在想人的一生的追求是什么.在这个物欲横流的社会,对人的价值的定义只是在财富积累的多少,这个是大多数人所认为的.但人的一生顶多百年,百年之后这些虚荣划归为尘 ...

  10. Longest Palindromic Substring 解答

    Question Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...