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 ...
随机推荐
- Java基础-运算符(03)
概念: 运算符:就是对于常量和变量进行操作的符号. 表达式:用运算符连接起来的符合java语法的式子,不同的运算符连接的表达式是不同类型的表达式. 运算符分类: 算数运算符(+ - * / % ...
- python内置方法总结
abs() #求绝对值 >>> abs(-19) 19 all() #里面的元素全部为真才是真 >>> all([1,2,3,'',2]) False any #只 ...
- C#设计模式之十一外观模式(Facade)【结构型】
一.引言 快12点半了,要开始今天的写作了.很快,转眼设计模式已经写了十个了,今天我们要讲[结构型]设计模式的第五个模式,该模式是[外观模式],英文名称是:Facade Pattern.我们先从名字上 ...
- 如何线上部署node.js项目
来源:http://blog.csdn.net/chenlinIT/article/details/73343793 前言 最近工作不是很忙,在空闲时间学习用node+express搭建自己的个人博客 ...
- PyQt5安装目录中找不到designer.exe与pyrcc5.exe
我安装的是PyQt5的5.9版本,在安装目录下找不到designer.exe文件.在摸索一段后发现5.9版本对库文件和相关的开发工具是分开发布的.QtDesigner是在pyqt5-tools的包里. ...
- django 实现同一个ip十分钟内只能注册一次(redis版本)
上一篇文章,django 实现同一个ip十分钟内只能注册一次 的时候,我们在注册的时候选择使用的使我们的数据库来报错我们的注册的ip信息,可是如果数据量大,用户多的时候,单单靠我们的数据库 来储存我们 ...
- Android 开发笔记___DateUtil——Time
package com.example.alimjan.hello_world; /** * Created by alimjan on 6/30/2017. */ import java.text. ...
- Linq Take和Skip详解
Take()方法的作用就是:从查询结果中提取前n个结果. Skip()方法正好是Take()方法的反面,它可以跳过前n个结果,返回剩余的结果. 例如:查找年龄最大的3个人 表Student的数据是 N ...
- javascript中原型链与instanceof 原理
instanceof:用来判断实例是否是属于某个对象,这个判断依据是什么呢? 首先,了解一下javascript中的原型继承的基础知识: javascript中的对象都有一个__proto__属性,这 ...
- float 浮动
浮动最开始的目的是为了让文字环绕图片(一个图片和多行文字对齐) 1.包裹性:元素添加 float 属性之后 自动变成 inline-block 元素,能设置 宽高 2.破坏性:破坏自身高度,还会使 ...