sdut2610--Boring Counting(二分+划分树)

2024-08-25 00:58

本文主要是介绍sdut2610--Boring Counting(二分+划分树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Boring Counting

Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

    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).

输入

     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)

输出

    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.

示例输入

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

示例输出

Case #1:
13
7
3
6
9

 

给出n个数,问范围在[l,r]内,值在[A,B]内的数有多少个

转化为在[l,r]的范围内A是第几大的数,B是第几大的树,这样相减就可以得到结果,但是我们只能求第k大的数,不能找出数是第几个,二分k,用二分找出上下界。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define maxn 50005
int a[maxn] , a_sort[maxn] ;
int tree[20][maxn] ;
int sum[20][maxn] ;
int n , m ;
void build(int c,int l,int r)
{
if( l == r ) return ;
int mid = (l+r)/2 , m = mid-l+1 ;
int pl = l , pr = mid+1 , i ;
for(i = l ; i <= mid ; i++)
{
if( a_sort[i] < a_sort[mid] )
m-- ;
}
for(i = l ; i <= r ; i++)
{
if( i == l ) sum[c][i] = 0 ;
else sum[c][i] = sum[c][i-1] ;
if( tree[c][i] == a_sort[mid] )
{
if( m )
{
m-- ;
tree[c+1][pl++] = tree[c][i] ;
sum[c][i]++ ;
}
else
tree[c+1][pr++] = tree[c][i] ;
}
else if( tree[c][i] < a_sort[mid] )
{
tree[c+1][pl++] = tree[c][i] ;
sum[c][i]++ ;
}
else
tree[c+1][pr++] = tree[c][i] ;
}
build(c+1,l,mid) ;
build(c+1,mid+1,r) ;
return ;
}
int query(int c,int l,int r,int ql,int qr,int k) {
if( l == r ) return tree[c][l] ;
int mid = (l+r)/2 , num1 , num2 ;
if( l == ql ) {
num1 = 0 ;
num2 = sum[c][qr] ;
}
else {
num1  = sum[c][ql-1] ;
num2 = sum[c][qr] - num1 ;
}
if( k <= num2 )
query(c+1,l,mid,l+num1,l+num1+num2-1,k) ;
else
query(c+1,mid+1,r,mid+1+(ql-l-num1),mid+1+(qr-l-num1-num2),k-num2) ;
}
void solve(int l,int r,int A,int B) {
int low , mid , high , last , x ;
int ans = 0 , flag = 0 ;
low = 1 ; high = r-l+1 ; last = -1 ;
while( low <= high ) {
mid = (low+high)/2 ;
x = query(0,1,n,l,r,mid) ;
if( x <= B ) {
last = mid ;
low = mid + 1 ;
}
else
high = mid - 1 ;
}
if( last == -1 )
flag = 1 ;
else
ans += last ;
low = 1 ; high = r-l+1 ; last = -1 ;
while( low <= high ) {
mid = (low+high)/2 ;
x = query(0,1,n,l,r,mid) ;
if( x >= A ) {
last = mid ;
high = mid - 1 ;
}
else
low = mid + 1 ;
}
if( last == -1 )
flag = 1 ;
else
ans = ans - last+1 ;
if( flag ) ans = 0 ;
printf("%d\n", ans) ;
}
int main()
{
int t , step = 0 ;
int l , r , A , B ;
int i ;
scanf("%d", &t) ;
while( t-- )
{
scanf("%d %d", &n, &m) ;
for(i = 1 ; i <= n ; i++)
{
scanf("%d", &a[i]) ;
tree[0][i] = a_sort[i] = a[i] ;
}
sort(a_sort+1,a_sort+1+n) ;
build(0,1,n) ;
printf("Case #%d:\n", ++step) ;
while( m-- ) {
scanf("%d %d %d %d", &l, &r, &A, &B) ;
solve(l,r,A,B) ;
}
}
return 0 ;
}


 

这篇关于sdut2610--Boring Counting(二分+划分树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104107

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

poj 3258 二分最小值最大

题意: 有一些石头排成一条线,第一个和最后一个不能去掉。 其余的共可以去掉m块,要使去掉后石头间距的最小值最大。 解析: 二分石头,最小值最大。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <c

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al

poj 2594 二分图最大独立集

题意: 求一张图的最大独立集,这题不同的地方在于,间接相邻的点也可以有一条边,所以用floyd来把间接相邻的边也连起来。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <sta

poj 3692 二分图最大独立集

题意: 幼儿园里,有G个女生和B个男生。 他们中间有女生和女生认识,男生男生认识,也有男生和女生认识的。 现在要选出一些人,使得这里面的人都认识,问最多能选多少人。 解析: 反过来建边,将不认识的男生和女生相连,然后求一个二分图的最大独立集就行了。 下图很直观: 点击打开链接 原图: 现图: 、 代码: #pragma comment(

poj 2112 网络流+二分

题意: k台挤奶机,c头牛,每台挤奶机可以挤m头牛。 现在给出每只牛到挤奶机的距离矩阵,求最小化牛的最大路程。 解析: 最大值最小化,最小值最大化,用二分来做。 先求出两点之间的最短距离。 然后二分匹配牛到挤奶机的最大路程,匹配中的判断是在这个最大路程下,是否牛的数量达到c只。 如何求牛的数量呢,用网络流来做。 从源点到牛引一条容量为1的边,然后挤奶机到汇点引一条容量为m的边

二分最大匹配总结

HDU 2444  黑白染色 ,二分图判定 const int maxn = 208 ;vector<int> g[maxn] ;int n ;bool vis[maxn] ;int match[maxn] ;;int color[maxn] ;int setcolor(int u , int c){color[u] = c ;for(vector<int>::iter