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. LeetCode 78. Subsets(子集合)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  2. TiDB 作为 MySQL Slave 实现实时数据同步

    由于 TiDB 本身兼容绝大多数的 MySQL 语法,所以对于绝大多数业务来说,最安全的切换数据库方式就是将 TiDB 作为现有数据库的从库接在主 MySQL 库的后方,这样对业务方实现完全没有侵入性 ...

  3. 读《你不知道的JavaScript(上卷)》后感-作用域闭包(二)

    github原文 一. 序言 最近我在读一本书:<你不知道的JavaScript>,这书分为上中卷,内容非常丰富,认真细读,能学到非常多JavaScript的知识点,希望广大的前端同胞们, ...

  4. Fibonacci Check-up

    Fibonacci Check-up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  5. Linux替换命令

    :s/^.*$/\L&/100 ##将100行内的小写转换成大写 vi/vim 中可以使用 :s 命令来替换字符串. :s/vivian/sky/ 替换当前行第一个 vivian 为 sky ...

  6. eclipse运行中出错:unknown protocol: hdfs

    出现这个错误因为你没有把core-site.xml和hdfs-site.xml放到项目下 程序运行开始就要调用这两个配置文件,这两个文件就是配置Hadoop时候的配置文件,只需要把至两个文件copy到 ...

  7. easyui dialog 中嵌入html页面

    最近使用easyui比较多,这个插件确实很好用.在使用时也遇到了大大小小的问题,好在都一一解决了. 记录一下今天遇到的问题. 目的:用easyui的dialog嵌入一个html页面(html中仍有要执 ...

  8. springmvc+quartz简单实现定时调度

    一.简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十 ...

  9. (二): 基于ZeroMQ的实时通讯平台

    基于ZeroMQ的实时通讯平台 上篇:C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework)----(一):整体介绍 通讯平台作为 ...

  10. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...