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. HDU 5510 Bazinga (字符串匹配)

    题目:传送门. 题意:t 组数据,每组 n 个串,对于第 i 个串如果存在 1 到 i-1 中的某个串不是 i 的子串,那么这个第 i 个串符合题意,求 i 的最大值. 题解:KMP,AC自动机也可以 ...

  2. ios学习总结(1) -- 创建第一个ios项目

    原文地址 下载并打开xcode. 接着新建一个工程,如下图所示: 点击Create a new Xcode project,之后选择ios下的Application,点击Single View App ...

  3. -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.

    [root@DB ~]# mysqldump -uroot -p123 --flush-logs --all-databases >fullbackup_sunday_11_PM.sql -- ...

  4. SecureCRT乱码

    http://jingyan.baidu.com/article/948f59245be128d80ff5f9aa.html

  5. Android_adb shell am/pm使用

    转自:http://blog.sina.com.cn/s/blog_51335a0001017ux5.html   adb shell am instrument [options] <COMP ...

  6. 手动载入NT驱动

    运行 regedit.exeHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services ImagePath \??\G:\驱动教程\018_读出SSDT表 ...

  7. 使用 Docker 建立 Mysql 集群

    软件环境介绍操作系统:Ubuntu server 64bit 14.04.1Docker 版本 1.6.2数据库:Mariadb 10.10 (Mariadb 是 MySQL 之父在 MySQL 被 ...

  8. The number of divisors(约数) about Humble Numbers[HDU1492]

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  9. extjs 选项卡

    yufenghou extjs 选项卡 <%@ page language="java" import="java.util.*" pageEncodin ...

  10. 【原】storm源码之理解Storm中Worker、Executor、Task关系

    Storm在集群上运行一个Topology时,主要通过以下3个实体来完成Topology的执行工作:1. Worker(进程)2. Executor(线程)3. Task 下图简要描述了这3者之间的关 ...