题目链接: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(树状数组 离散化)的更多相关文章

  1. HDU 4325 Flowers 树状数组+离散化

    Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...

  2. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  6. HDU4325 树状数组+离散化

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others)   ...

  7. BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...

  8. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  9. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

随机推荐

  1. 【BZOJ】【3004】吊灯

    思路题 要将整棵树分成大小相等的连通块,那么首先我们可以肯定的是每块大小x一定是n的约数,且恰好分成$\frac{n}{x}$块,所以我有了这样一个思路:向下深搜,如果一个节点的size=x,就把这个 ...

  2. HttpWatch 安装后在IE上打开

    启动浏览器, 在空白地方左键,  显示出菜单栏 菜单栏中选择"查看">"浏览器栏">"HttpWatch"启动HttpWatch ...

  3. HDU1014Uniform Generator

    Uniform Generator Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   ...

  4. Nginx的accept_mutex配置分析

    让我们看看accept_mutex的意义:当一个新连接到达时,如果激活了accept_mutex,那么多个Worker将以串行方式来处理,其中有一个Worker会被唤醒,其他的Worker继续保持休眠 ...

  5. Tomcat漏洞说明与安全加固

    Tomcat是Apache软件基金会的一个免费的.开放源码的WEB应用服务器,可以运行在Linux和Windows等多个平台上,由于其性能稳定.扩展性好.免费等特点深受广大用户的喜爱.目前,互联网上绝 ...

  6. Xamarin 中开发Android实现全屏或者不显示标题栏的方法-宋兴柱

    using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; ...

  7. 7 天玩转 ASP.NET MVC — 第 5 天

    目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 欢迎来到第五天的学习.希望第一天到第四天的学习,你都是开心的. 1. Lab 22 - 增加 Fo ...

  8. 查看w3wp进程占用的内存及.NET内存泄露,死锁分析--转载

    一 基础知识 在分析之前,先上一张图: 从上面可以看到,这个w3wp进程占用了376M内存,启动了54个线程. 在使用windbg查看之前,看到的进程含有 *32 字样,意思是在64位机器上已32位方 ...

  9. iOS-xib(自定义UITableViewCell)

    1.创建一个自定义的xib

  10. 在Vista或Windows 7系统上安装Sharepoint 2007

    在Vista或Windows 7系统上安装Sharepoint 2007 2010-03-05 18:53:19|  分类: 技术文章|字号 订阅     SharePoint 2007 不能直接安装 ...