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 ...
随机推荐
- 【python】异常处理
转自:http://www.cnblogs.com/IPrograming/p/Python_error_handler.html 1. 抛出异常和自定义异常 Python用异常对象(exceptio ...
- javascript首尾反转字符
var my_str="Welcome to www.sharejs.com" var i=my_str.length; i=i-1; for (var x = i; x > ...
- C++动态内存管理之shared_ptr、unique_ptr
C++中的动态内存管理是通过new和delete两个操作符来完成的.new操作符,为对象分配内存并调用对象所属类的构造函数,返回一个指向该对象的指针.delete调用时,销毁对象,并释放对象所在的内存 ...
- Linux下循环、选择、顺序操作
1.Shell脚本数组操作小结 http://www.jb51.net/article/52382.htm Linux Shell在编程方面比Windows批处理强大很多,无论是在循环.运算. bas ...
- HDU 4343 贪心
D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...
- matlab中的ishghandle
ishghandle True for Handle Graphics object handles. ishghandle(H) returns an array that contains 1's ...
- Source insight怎样恢复默认界面布局 窗口嵌入
先关闭,然后在c盘的文档的Source Insight\Settings目录里面,将CF3文件剪切到别的地方,再打开 不过有些其它设置也会丢失,需要重新设置,参见Source_Insight破解版下载 ...
- Xamarin Android设置界面提示类型错误
Xamarin Android设置界面提示类型错误 错误信息:Integer types not allow (at ‘padding’ with value ’10’)Android界面属性的长度和 ...
- Logback常用配置详解
logback是一套日志框架,由log4j的优化版,由同一个作者开发,在速度和性能上都超过其他日志框架,再结合slf4j,已成为当前最流行的日志框架. Logback最常用就是在classpath定义 ...
- ural 1283. Dwarf
1283. Dwarf Time limit: 1.0 secondMemory limit: 64 MB Venus dwarfs are rather unpleasant creatures: ...