Boring Counting

Time Limit: 3000MS Memory Limit: 65536KB

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

Example 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

Example Output

Case #1:
13
7
3
6
9

题目链接:SDUT 2610

裸的主席树区间查询,但是要注意一下就是离散化之后若输入的值域中有小于最小值的,即计算原位置后会出现0,因此建树若范围是1~pos.size()的话就会超时,建议改为0~pos.size();

若有大于最大值的,则离散化之后会自动替换被为最大值,然而此题这样做当然是错的,因此a不用找接近值代替而要把上限b要找接近值代替,这样一来若b大于最大值,则代替没有关系,若b等于a且均大于最大值,则会出现b比a小的情况,特判一下输出0,然后若b比最小值都要小,则直接让B等于0,也特判为输出0。

调试了一会儿终于过了,= =划分树什么的这题肯定木有这个主席树这么好用啦……再说数据结构还没学到这玩意儿……也就会个主席树了……。

最后把这组数据调通就过了……

1
13 8
1 2 3 4 5 6 7 8 9 10 11 12 13
1 13 14 14

答案应该是0,如果答案是1的话估计就是离散化位置计算出了点问题

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=50010;
struct seg
{
int lson,rson;
int cnt;
};
seg T[N*20];
int root[N],tot;
pii arr[N];
vector<int>pos;
void update(int &cur,int ori,int l,int r,int pos)
{
cur=++tot;
T[cur]=T[ori];
++T[cur].cnt;
if(l==r)
return ;
int mid=MID(l,r);
if(pos<=mid)
update(T[cur].lson,T[ori].lson,l,mid,pos);
else
update(T[cur].rson,T[ori].rson,mid+1,r,pos);
}
int query(int S,int E,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
return T[E].cnt-T[S].cnt;
else
{
int mid=MID(l,r);
if(R<=mid)
return query(T[S].lson,T[E].lson,l,mid,L,R);
else if(L>mid)
return query(T[S].rson,T[E].rson,mid+1,r,L,R);
else
return query(T[S].lson,T[E].lson,l,mid,L,mid)+query(T[S].rson,T[E].rson,mid+1,r,mid+1,R);
}
}
void init()
{
CLR(root,0);
tot=0;
T[0].cnt=T[0].lson=T[0].rson=0;
pos.clear();
}
int main(void)
{
int tcase,n,m,l,r,a,b,i;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
init();
scanf("%d%d",&n,&m);
for (i=1; i<=n; ++i)
{
scanf("%d",&arr[i].first);
pos.push_back(arr[i].first);
}
sort(pos.begin(),pos.end());
pos.erase(unique(pos.begin(),pos.end()),pos.end());
int SZ=pos.size();
for (i=1; i<=n; ++i)
{
arr[i].second=lower_bound(pos.begin(),pos.end(),arr[i].first)-pos.begin()+1;
update(root[i],root[i-1],0,SZ,arr[i].second);
}
printf("Case #%d:\n",q);
for (i=0; i<m; ++i)
{
scanf("%d%d%d%d",&l,&r,&a,&b);
int A,B;
A=lower_bound(pos.begin(),pos.end(),a)-pos.begin()+1;
B=lower_bound(pos.begin(),pos.end(),b)-pos.begin()+1;
if(pos[B-1]!=b)//上限B的位置要特别计算一下
--B;
if(!B||A>B)//特判
puts("0");
else
printf("%d\n",query(root[l-1],root[r],0,SZ,A,B));
}
}
return 0;
}

SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)的更多相关文章

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

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

  2. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  3. sdutoj 2610 Boring Counting

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610 Boring Counting Time ...

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

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

  5. PAT (Basic Level) Practise 1045 快速排序(离散化+主席树区间内的区间求和)

    1045. 快速排序(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 著名的快速排序算法里有一个经典的划分 ...

  6. 主席树总结(经典区间第k小问题)(主席树,线段树)

    接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...

  7. 线段树专题2-(加强版线段树-可持续化线段树)主席树 orz! ------用于解决区间第k大的问题----xdoj-1216

    poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  8. 【树状数组套主席树】带修改区间K大数

    P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ...

  9. 【bzoj3932】[CQOI2015]任务查询系统 离散化+主席树

    题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第Ei ...

随机推荐

  1. Java 复制文件的高效方法

    转载自:http://jingyan.baidu.com/article/ff4116259c2d7712e4823780.html 在Java编程中,复制文件的方法有很多,而且经常要用到.我以前一直 ...

  2. UTF8-GBK WideCharToMultiByte MultiByteToWideChar

    //MFC版本 CString UTF8ToGBK(const CString& strUTF8){ //确定转换为Unicode需要多少缓冲区(返回值也包含了最后一个NULL字符) int ...

  3. java equals 和 "==" 比较

    java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean   他们之间的比较,应用双等号(== ...

  4. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  5. js checkbox

    js checkbox <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  6. Sql语句里的递归查询(转)

    原文摘自:http://blog.csdn.net/pdn2000/article/details/6674243 Sql语句里的递归查询 SqlServer2005和Oracle 两个版本 以前使用 ...

  7. Session: 防止用户多次登陆

    在web开发时,有的系统要求同一个用户在同一时间只能登录一次,也就是如果一个用户已经登录了,在退出之前如果再次登录的话需要报错. 常见的处理方法是,在用户登录时,判断此用户是否已经在Applicati ...

  8. Jquery用途

    封装JS,开源,操作方便,提高开发效率. 轻量级,选择器强大,浏览器兼容.

  9. Codeforces Round #292 (Div. 2)

    A. Drazil and Date 无算法,判断(s - (a + b)) % 2是否为零,若零,表示在s步内还能走向其他的地方并且回来 否则,都是No #include <cstdio> ...

  10. Unity3D Built-in Shader详解一

    Unity3D内置了很多Shader,文档很详细,自己翻一下.便于加深印象. 首先先解释下Unity3D的Shader.Unity里面的Shaders是使用一种叫ShaderLab的语言编写的,它同微 ...