SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)
Boring Counting
Problem Description
Input
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
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(离散化+主席树区间内的区间求和)的更多相关文章
- sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)
		Boring Counting Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 In this problem you a ... 
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
		DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ... 
- sdutoj 2610 Boring Counting
		http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610 Boring Counting Time ... 
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
		转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ... 
- PAT (Basic Level) Practise 1045 快速排序(离散化+主席树区间内的区间求和)
		1045. 快速排序(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 著名的快速排序算法里有一个经典的划分 ... 
- 主席树总结(经典区间第k小问题)(主席树,线段树)
		接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ... 
- 线段树专题2-(加强版线段树-可持续化线段树)主席树  orz!  ------用于解决区间第k大的问题----xdoj-1216
		poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #incl ... 
- 【树状数组套主席树】带修改区间K大数
		P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ... 
- 【bzoj3932】[CQOI2015]任务查询系统  离散化+主席树
		题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第Ei ... 
随机推荐
- 数列(codevs 1141)
			题目描述 Description 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13 ... 
- JQuery的AJAX封装加例子
			将json字符串转换为javascript对象有两种方法:var strs = eval("(" + data + ")");var strs = $.pars ... 
- JUC系列回顾之-CountDownLatch底层原理和示例
			CountDownLatch 是一个同步工具类,允许一个线程或者多个线程等待其他线程完成操作,再执行. CountDownLatch(int count) 构造一个用给定计数初始化的 CountDow ... 
- oracle的关闭过程(各个模式关闭)
			关闭数据库与实例 与数据库启动一下,关闭数据库与实例也分为3步:关闭数据库-->实例卸载数据库--->终止实例. 1.Nomal(正常关闭方式) 命令:shutdown nomal 讲解: ... 
- h5 canvas 画图
			h5 canvas 画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ... 
- 从C# 到 OC
			字符串 声明: C#: string name = “lwme.cnblogs.com”; Objective-C: NSString *name = @”lwme.cnblogs.com”; 字符串 ... 
- 【web  JSP  basePath】basePath的含义
			问题1:WEB-INF的问题 今天新创建项目,在JSP中引入外部的JS文件和CSS文件,但是路径一直显示错误: 其中JSP页面引入这几个文件是这么写的: <link rel="styl ... 
- Enter直接登录
			2.2 按Enter键调用登录按钮 [实例描述] 为了方便用户操作,在登录邮箱或论坛时,如果用户输入了用户名和密码,按Enter键时,都会自动调用登录按钮.本例学习如何实现此功能. [实现代码] & ... 
- 织梦按栏目id读取banner图
			<div class="pic"> <div class="banner"> {dede:type} <div><a ... 
- Hark的数据结构与算法练习之快速排序
			前言 快速排序是最常见,也是面试中最容易考的排序方法,这里做一下总结 算法说明 其实这里说的很清楚了:http://blog.csdn.net/morewindows/article/details/ ... 
