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 ...
随机推荐
- shell脚本学习(一):shell脚本开发的基本规范和习惯
1.脚本第一行指定脚本解释器 #!/bin/bash 或 #!/bin/sh 2.脚本开头增加作者.脚本作用描述等信息 1 #!/bin/bash 2 #Author: iskylite 3 #Blo ...
- R学习笔记 第四篇:函数,分支和循环
变量用于临时存储数据,而函数用于操作数据,实现代码的重复使用.在R中,函数只是另一种数据类型的变量,可以被分配,操作,甚至把函数作为参数传递给其他函数.分支控制和循环控制,和通用编程语言的风格很相似, ...
- Matrices and Vectors
Matrices and Vectors Matrices are 2-dimensional arrays: A vector is a matrix with one column and man ...
- Power Strings(KMP)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 45008 Accepted: 18794 D ...
- 实例理解mapreduce任务的串行运行过程
一.准备: eclipse,hadoop集群 注意:为了方便测试和修改,我用的是 windows 连接hadoop集群,这样在windows 下直接就能够执行 mapreduce 任务,方便程序调试. ...
- Log4j – Log4j 2 API
Overview The Log4j 2 API provides the interface that applications should code to and provides the ad ...
- CSS3 动画 animation和@keyframes
CSS3 @keyframes 规则 如需在 CSS3 中创建动画,您需要学习 @keyframes 规则. @keyframes 规则用于创建动画.在 @keyframes 中规定某项 CSS 样式 ...
- mac链接linux终端,shell脚本发布代码
项目的业务需求:从mac端直接连上linux服务终端,并发布相关的代码 一.使用ssh链接上linux服务端 1.cd ~/.ssh 2.vi config,按照下面的内容配置config文件,然后: ...
- JavaScript系列----数据类型以及传值和传引用
1.简单数据类型 在JavaScript中简单数据类型分为5种.分别为 Undefined, Null,Boolean,Number,String. Undefined类型Undefined类型只有一 ...
- 【机器学习】TensorFlow学习(一)
感谢中国人民大学胡鹤老师,课讲得非常好~ 首先,何谓tensor?即高维向量,例如矩阵是二维,tensor是更广义意义上的n维向量(有type+shape) TensorFlow执行过程为定义图,其中 ...