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 线段树+离散化的更多相关文章

  1. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  2. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  3. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  4. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  5. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  6. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  7. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  8. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  9. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

随机推荐

  1. PHP常用配置

    Php配置文件:php.ini(使用‘;’表示注释) Php的配置项可以在配置文件中配置,也可以在脚本中使用ini_set()函数临时配置. 语言相关配置: 1. engine:设置PHP引擎是否可用 ...

  2. 关于laravel5.5控制器方法参数依赖注入原理深度解析及问题修复

    在laravel5.5中,可以根据控制器方法的参数类型,自动注入一个实例化对象,极大提升了编程的效率,但是相比较与Java的SpringMVC框架,功能还是有所欠缺,使用起来还是不太方便,主要体现在方 ...

  3. AngularJS学习篇(十五)

    AngularJS 模块 模块定义了一个应用程序. 模块是应用程序中不同部分的容器. 模块是应用控制器的容器. 控制器通常属于一个模块. 创建模块 你可以通过 AngularJS 的 angular. ...

  4. php header设置页面编码

    php header设置页面编码 未分类 PHP 页面编码声明方法详解(header或meta) http://www.jb51.net/article/22501.htm PHP 页面编码声明与用h ...

  5. javascript 备忘 细节 相关

    DOMContentLoaded事件触发时机,即dom tree完成但页面未必渲染完毕.   var a = [1,2,3,4]; var length = a.length; alert((leng ...

  6. Xcode修改个性化注释

    1.首先找到Xcode进入包内容 2.依次进入/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Develo ...

  7. CSS3中的属性介绍

    概念:给文字添加阴影 语法:text-shadow: length length length color 介绍:前两个参数为阴影离开文字的横方向位移距离与纵方向位移距离.使用时前两个参数必须指定,也 ...

  8. 一起写框架-Ioc内核容器的实现-基础功能-getBean(五)

    实现的功能 1. 启动程序时,将@ComponentScan加载的类,创建对象并放在容器里面.(查看上一篇文) 2. 通过ApplicatoinContext的getBean()方法获得容器里面的对象 ...

  9. Scrum Meeting Alpha - 9

    Scrum Meeting Alpha - 9 NewTeam 2017/11/03 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了登陆退出功能Pull Request ...

  10. Unity中的Mono & Linux上编译Mono的流程

    前段时间编译了一下Unity的Mono,看了很多相关的文章,也遇到很多新坑.所以来总结一下,加深自己对Mono的理解 为什么Unity可以跨平台运行呢 通常Unity的脚本有C#.JS.Boo.不过现 ...