[HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325
关于离散化的简介:http://blog.csdn.net/gokou_ruri/article/details/7723378
假如数据太大,无法作为数组下标来保存对应的属性而采取的一种方法。只需要关注相对大小即可。
我们记下所有的时间数据,再排序。通过二分查找快速定位元素的位置,然后在线段树或者树状数组上仅仅更新这个映射过后的下标位置。因为不需要从线段树或树状数组上直接获取数据(单纯的线段树或树状数组本身是没有意义的,需要搭配相应的数据操作才可以使其称之为线段树或树状数组),也就是说我们更新和查询都是要通过这样一次定位。这样可以解决空间不足的问题。排序后要去重,否则更新到时候可能会更新两次。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef struct Node {
int s;
int t;
}Node; const int maxn = ;
int n, m, cnt, tot;
int wt[maxn<<];
int bit[maxn];
int ask[maxn];
Node f[maxn]; int lowbit(int x) {
return x & (-x);
} void add(int i, int x) {
while(i <= cnt) {
bit[i] += x;
i += lowbit(i);
}
} int sum(int i) {
int s = ;
while(i > ) {
s += bit[i];
i -= lowbit(i);
}
return s;
} int bs(int v) {
int ll = , rr = cnt - ;
int mm;
while(ll <= rr) {
mm = (ll + rr) >> ;
if(wt[mm] == v) return mm;
if(wt[mm] > v) rr = mm - ;
if(wt[mm] < v) ll = mm + ;
}
return -;
} inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
scan_d(T);
while(T--) {
tot = ;
memset(bit, , sizeof(bit));
scan_d(n); scan_d(m);
for(int i = ; i <= n; i++) {
scan_d(f[i].s); scan_d(f[i].t);
wt[tot++] = f[i].s; wt[tot++] = f[i].t;
}
for(int i = ; i <= m; i++) {
scan_d(ask[i]);
wt[tot++] = ask[i];
}
sort(wt, wt+tot);
// int tot = unique(wt,wt+tot) - wt;
cnt = ;
for(int i = ; i < tot; i++) {
if(wt[i] != wt[i-]) wt[cnt++] = wt[i];
}
for(int i = ; i <= n; i++) {
int x = bs(f[i].s) + ;
int y = bs(f[i].t) + ;
add(x, );
add(y+, -);
}
printf("Case #%d:\n", _++);
for(int i = ; i <= m; i++) {
int ans = bs(ask[i]) + ;
printf("%d\n", sum(ans));
}
}
return ;
}
[HDOJ4325]Flowers(树状数组 离散化)的更多相关文章
- HDU 4325 Flowers 树状数组+离散化
Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- HDU4325 树状数组+离散化
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others) ...
- BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6321 Solved: 2628[Su ...
随机推荐
- [百度空间] [转] 四元数(Quaternions)
转:四元数(Quaternions) 好吧,我必须承认到目前为止我还没有完全理解四元数,我一度把四元数理解为轴.角表示的4维向量,也就在下午我才从和同事的争辩中理解了四元数不完全是角.轴这么简单,为此 ...
- 简单shell脚本
简单shell脚本备忘 #!/bin/sh num= ] do table_num=`printf %03d ${num}` echo album_info_${table_num} #mys ...
- HDOJ 1062 Text Reverse
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Sublime Text3激活 破解
Sublime Text 是一个复杂的文本.代码编辑器.出色用户界面,非凡的功能和惊人的性能. Sublime Text 3 官方网站 http://www.sublimetext.com/ 点击菜单 ...
- Winform设置相关
>> Winform查找根目录 1) AppDomain.CurrentDomain.BaseDirectory 地址为: d:\MyProject\Bin\ Application. ...
- React Native 简介:用 JavaScript 搭建 iOS 应用(2)
[编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...
- CF444C DZY Loves Colors
考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了.A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了.然后就在不停地YY B ...
- DevExpress Form那些事儿
1:设置子窗体依附父窗体 首先将父窗体的属性中 IsMdiContainer 设置为 True , 就是将窗体设置为 MDI窗体.子窗体和父窗体都是继承自RibbonForm的. 代码如下 : ...
- Linux下安装Scim-googlepinyin输入法和设置Sublime Text中文输入
1.安装git sudo apt-get install git http://www.cnblogs.com/perseus/archive/2012/01/06/2314069.html 2.获取 ...
- 优化DB2缓冲页的大小
零部件日结无法进行下去,建议配置C:\Program Files\SQLLIB目录下的db2cli.ini文件,加入此节:[DMSCNDB]CLIPkg=5 并重启DB2试试 另外,可以在命令行处理器 ...