hdoj 4325 Flowers 线段树+离散化
hdoj 4325 Flowers
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4325
思路:
直接线段树,按照花的开放区间的大小建树,要注意虽然花的周期数据可能会达到1e9,这样的话线段树开四倍时不可能的。但是我们可以看到一共可能的数据时N行,那么每行两个数,再开4倍的区间。计算下来,在离散化的帮助下,我们只需要开8*N被的线段树即可。
另外询问的数据也需要放入离散化的范围,如果不这样做,有可能在询问时使用lower_bound函数会导致数据的改变,询问的原数据发生变化。
eg:1~3 7~10 询问6,结果应该时0,但因为lower_bound的原因询问时使用7,得到结果1。etc.
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int maxn = 1e5+5;
struct node {
int l,r,sum,lazy;
inline void update(int val) {
lazy+=val;
sum+=val;
}
} tr[maxn*8];
int a[maxn],b[maxn],c[maxn<<1],d[maxn];
inline void push_down(int s) {
int lazyval = tr[s].lazy;
if(!lazyval) return;
tr[s<<1].update(lazyval);
tr[s<<1|1].update(lazyval);
tr[s].lazy=0;
}
void build(int s, int l, int r) {
tr[s].l=l;tr[s].r=r;
tr[s].lazy=tr[s].sum=0;
if(l==r) return;
int mid = (l+r)>>1;
build(s<<1,l,mid);
build(s<<1|1,mid+1,r);
}
void update(int s, int l, int r) {
if(tr[s].l==l&&tr[s].r==r) {
tr[s].update(1);
return;
}
push_down(s);
int mid = (tr[s].l+tr[s].r)>>1;
if(r<=mid) update(s<<1,l,r);
else if(l>mid) update(s<<1|1,l,r);
else {
update(s<<1,l,mid);
update(s<<1|1,mid+1,r);
}
}
int query(int s, int l, int r) {
if(tr[s].l==l&&tr[s].r==r) {
return tr[s].sum;
}
push_down(s);
int mid=(tr[s].l+tr[s].r)>>1;
if(r<=mid) return query(s<<1,l,r);
else return query(s<<1|1,l,r);
}
int main() {
int t,n,m,tot;
scanf("%d",&t);
for(int j=1;j<=t;++j) {
scanf("%d %d",&n,&m);
tot=1;
for(int i=1;i<=n;++i) {
scanf("%d %d",&a[i],&b[i]);
c[tot++]=a[i];c[tot++]=b[i];
}
for(int i=1;i<=m;++i) {
scanf("%d",&d[i]);
c[tot++]=d[i];
}
sort(c+1,c+tot);
tot=unique(c+1,c+tot)-(c+1);
build(1,1,tot);
for(int i=1;i<=n;++i) {
a[i]=lower_bound(c+1,c+tot,a[i])-c;
b[i]=lower_bound(c+1,c+tot,b[i])-c;
update(1,a[i],b[i]);
}
printf("Case #%d:\n",j);
for(int i=1;i<=m;++i) {
d[i]=lower_bound(c+1,c+tot,d[i])-c;
printf("%d\n",query(1,d[i],d[i]));
}
}
return 0;
}
hdoj 4325 Flowers 线段树+离散化的更多相关文章
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
随机推荐
- Spring in action记录
最近一段时间重新学习了一遍SPRING,现在对这些笔记整理一下,一来算是对之前的学习有一个交代,二来当是重新学习一次,三来可以留下备份 这次学习中以SPRING IN ACTION 4这学习资料,整书 ...
- Leetcode题解(九)
28.Implement strStr()-------KMP算法(*) 题目 这道题目其实就是实现KMP算法,并且该算法也是比较经典的算法,需要很好的掌握: 贴上几个介绍字符串匹配的算法说明链接 h ...
- 计数排序(O(n+k)的排序算法,空间换时间)
计数排序就是利用空间换时间,时间复杂度O(n+k) n是元素个数,k是最大数的个数: 统计每个数比他小的有多少,比如比a[i]小的有x个,那么a[i]应该排在x+1的位置 代码: /* * @Auth ...
- Very Simple Problem
Very Simple Problem Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 入我新美大的Java后台开发面试题总结
静儿最近在总结一些面试题,那是因为做什么事情都要认真.面试也一样,静儿作为新美大金融部门的面试官,负责任的告诉大家,下面的问题回答不上来,面试是过不了的.不过以下绝不是原题,你会发现自己实力不过硬,最 ...
- 0_Simple__fp16ScalarProduct
使用cuda内置无符号整数结构(__half2)及其汇编函数,计算两个向量的内积. 源代码: #include <cstdio> #include <cstdlib> #inc ...
- 【经验分享】Trachtenberg system(特拉亨伯格速算系统)
二战期间,俄国的数学家Jakow Trachtenberg(1888-1953)被关进纳粹集中营,在狱中,他开发出了一套心算算法,这套算法后来被命名为Trachtenberg(特拉亨伯格)速算系统. ...
- Grunt打包seajs项目
在使用seajs时,常常将若干脚本分为多次require进来,这样开发中比较方便,但是,会增加http请求次数,在生产环境中需要进行打包合并.压缩等操作. 以Grunt构建工具为例,对一个seajs项 ...
- .NET读取Excel文件的三种方法的区别
ASP.NET读取Excel文件方法一:采用OleDB读取Excel文件: 把Excel文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(strin ...
- linux 内存 大于 jvm xmx
文章来源: http://www.cnblogs.com/guozp/p/7845605.html 1.虽然你jvm参数设置了-Xms6g -Xmx6g,但操作系统不并会马上分配6G的物理内存,而是确 ...