http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237

Problem H:Boring Counting

Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 8  Solved: 4 [Submit][Status][Discuss]

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

山东省第四届ACM程序设计大赛2013.6.9

【题解】:

  题目大意:求[L,R]区间内的pi在满足[A<=pi<=B]的个数

   一开始想得是:直接用线段树记录区间的最大最小值,当最大最小值满足[A<=pi<=B]时就加上整个区间的长度,可是这样做会超时,因为不满足这个条件的情况太多了,基本上不满足的话每次都要遍历到最底层,所以TLE在所难免。

  后面觉得还是划分树靠谱,不过比赛的时候也没时间写了,划分树是求区间的第K大值,我们要转换一下,就是A和B在区间的分别是第几大,然后用后者减掉前者就是要求的解,那么拿A来说明一下,A在区间中到底是第几大呢,刚说过划分树是求区间的第K大数是谁的,而我们要求的是知道这个数是谁要求他在区间是第几大,有点绕口哈,这里,我们用到二分枚举,枚举区间第K大的数,然后与A进行比较,如果大于等于A,继续向下枚举,直到等于A的最后一个A为止,对于B同理:

时间复杂度,划分树的时间复杂度为O(n*log(n)) 二分枚举为O(logn)  整体时间复杂度为O(n*log(n)*log(n))

【code】:

 /**
Judge Status:Accepted Memory:13260 KB
Time:2500 ms Language:C++
Code Lenght:2824 B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm> #define N 50050
using namespace std; int sorted[N]; //排序完的数组
int toleft[][N]; //toleft[i][j]表示第i层从1到k有多少个数分入左边
int tree[][N]; //表示每层每个位置的值
int n; void building(int l,int r,int dep)
{
if(l==r) return;
int mid = (l+r)>>;
int temp = sorted[mid];
int i,sum=mid-l+; //表示等于中间值而且被分入左边的个数
for(i=l;i<=r;i++)
{
if(tree[dep][i]<temp) sum--;
}
int leftpos = l;
int rightpos = mid+;
for(i=l;i<=r;i++)
{
if(tree[dep][i]<temp) //比中间的数小,分入左边
{
tree[dep+][leftpos++]=tree[dep][i];
}
else if(tree[dep][i]==temp&&sum>) //等于中间的数值,分入左边,直到sum==0后分到右边
{
tree[dep+][leftpos++]=tree[dep][i];
sum--;
}
else //右边
{
tree[dep+][rightpos++]=tree[dep][i];
}
toleft[dep][i] = toleft[dep][l-] + leftpos - l; //从1到i放左边的个数
}
building(l,mid,dep+);
building(mid+,r,dep+);
} //查询区间第k大的数,[L,R]是大区间,[l,r]是要查询的小区间
int query(int L,int R,int l,int r,int dep,int k)
{
if(l==r) return tree[dep][l];
int mid = (L+R)>>;
int cnt = toleft[dep][r] - toleft[dep][l-]; //[l,r]中位于左边的个数
if(cnt>=k)
{
int newl = L + toleft[dep][l-] - toleft[dep][L-]; //L+要查询的区间前被放在左边的个数
int newr = newl + cnt - ; //左端点加上查询区间会被放在左边的个数
return query(L,mid,newl,newr,dep+,k);
}
else
{
int newr = r + (toleft[dep][R] - toleft[dep][r]);
int newl = newr - (r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);
}
} int bilibili(int L,int R,int l,int r,int a) //二分枚举
{
int ans=-;
while(l<=r)
{
int mid = (l+r)>>;
int res = query(,n,L,R,,mid);
if(res>=a) //直到找到最左边的那个等于a的结果
{
r = mid - ;
ans = mid;
}
else
{
l = mid + ;
}
}
return ans;
} int bulobulo(int L,int R,int l,int r,int b)
{
int ans=;
while(l<=r)
{
int mid = (l+r)>>;
int res = query(,n,L,R,,mid);
if(res>b) //直到找到最后边的大于b的结果
{
r = mid - ;
ans = mid;
}
else
{
l = mid + ;
}
}
if(!ans) return r;
return ans-;
} int main()
{
int t,cas = ;
scanf("%d",&t);
while(t--)
{
int m;
scanf("%d%d",&n,&m);
int i;
for(i=;i<=n;i++)
{
scanf("%d",&tree[][i]);
sorted[i] = tree[][i];
}
sort(sorted+,sorted++n);
building(,n,);
int l,r,a,b;
printf("Case #%d:\n",cas++);
while(m--)
{
scanf("%d%d%d%d",&l,&r,&a,&b);
int x = ;
int y = r-l+;
int cnt1 = bilibili(l,r,x,y,a);
int cnt2 = bulobulo(l,r,x,y,b);
if(cnt1==-)
{
printf("0\n");
continue;
}
printf("%d\n",cnt2-cnt1+);
}
}
return ;
}

SDIBT 3237 Boring Counting( 划分树+二分枚举 )的更多相关文章

  1. sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

    Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you a ...

  2. HDU-4417 Super Mario,划分树+二分!

    Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...

  3. 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  4. 山东第四届省赛: Boring Counting 线段树

    http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec  ...

  5. hdu4417 划分树+二分

    //Accepted 14796 KB 453 ms //划分树 //把查询的次数m打成n,也是醉了一晚上!!! //二分l--r区间第k大的数和h比较 #include <cstdio> ...

  6. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  7. UPC 2224 / “浪潮杯”山东省第四届ACM大学生程序设计竞赛 1008 Boring Counting 主席树

    Problem H:Boring Counting Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/ ...

  8. HDU 4417 划分树+二分

    题意:有n个数.m个询问(l,r,k),问在区间[l,r] 有多少个数小于等于k. 划分树--查找区间第k大的数.... 利用划分树的性质.二分查找在区间[l,r]小于等于k的个数. 假设在区间第 i ...

  9. 【划分树+二分】HDU 4417 Super Mario

    第一次 耍划分树.. . 模板是找第k小的 #include <stdio.h> #include <string.h> #include <stdlib.h> # ...

随机推荐

  1. 直接查数据导出xls

    直接查数据导出xls/usr/bin/mysql --default-character-set=utf8 -uroot -p"123456" -e"select * f ...

  2. IP地址计算和划分

    一.      B类地址 范围从128-191(第一串8位二进制10000000~10111111),如172.168.1.1,第一和第二段号码为网络号码,剩下的2段号码为本地计算机的号码.转换为2进 ...

  3. spring分布式事务学习笔记

    最近项目中使用了分布式事务,本文及接下来两篇文章总结一下在项目中学到的知识. 分布式事务对性能有一定的影响,所以不是最佳的解决方案,能通过设计避免最好尽量避免. 分布式事务(Distributed t ...

  4. MVC下用户登录状态校验的问题以及解决方案--------------Action全局过滤器的使用

    前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...

  5. kettle学习-day1-介绍、下载、安装

    QQ群:306059317\ 技术支持:荷露叮咚网络学苑http://www.heludd.com/kettle 下载最新版,获取视频教程 kettle安装: 1).下载需要安装的kettle版本,官 ...

  6. 第十一篇、RxSwift

    三. 什么是函数式编程? 什么是函数式编程呢? 函数式编程其实是一种编程思想, 代码写出来只是它的表现形式. 在面向对象的编程思想中, 我们将要解决的一个个问题, 抽象成一个个类, 通过给类定义属性和 ...

  7. Mac下安装Twig模版引擎的方法

    本人收集了几种方法: 安装前: twig需要依赖PHP 5.2.7 才能运行. 1 通过composer安装(2种) 方法一: 首先确保你已经安装了composer,如果没有安装,可以参考https: ...

  8. Cursor--游标

    游标--cursor['kɜːsə]   概念:                         在执行SQL语句时,Oracle服务器将分配一个内存区域,不仅存储这个语句,还存储语句的结果 — 称为 ...

  9. 仿春雨医生 安卓app(android)

    仿春雨医生 安卓app(android) 目前APP处与开发完善中,可过程序自下载更新,如有BUG报错,请联系QQ 131 065 1206 支持安卓(android) .IOS(IPHONE),PA ...

  10. DTCMS插件配置;给所有会员发信息

    插件配置  :http://www.dtcms.net/help/10.aspx 给所有人发信息 a)     将收件人设置为0即发送给所有人 b)     Templates\main\userme ...