[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 ...
随机推荐
- Leetcode#87 Scramble String
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- 亚马逊 在线测试题目 amazon (变种的)三叉树的最近公共祖先问题
题目意思就是找一棵按上面链接所示的树对应的上面的两个点的最小公共祖先(LCP,Least Common Father),按照比较大小来依次返回自己的父亲节点就行了.具体看代码:getfather(a) ...
- JS范围
JS API-->DOM/PoneGap/Cordova/Android/NodeJS JS OOP
- Notepad++中常用的插件
Notepad++中常用的插件 Notepad++实用插件分享 otepad++前端开发常用插件介绍
- URL编码方法比较
javascript中存在几种对URL字符串进行编码的方法:escape(),encodeURI(),以及encodeURIComponent().这几种编码所起的作用各不相同. escape() 方 ...
- ErrorCode枚举类型返回错误码信息测试,手动抛出异常信息,在事务中根据错误码来回滚事务的思路。
ErrorCode.java 简单测试代码,具体应用思路:手动抛出异常信息,在事务中根据错误码来回滚事务的思路. public enum ErrorCode { //系统级 SUCCESS(" ...
- [排序] 快排 && 冒泡(自己写)
#include <iostream> using namespace std; /* 快速排序 通过一趟排序,以轴点为界 分割为两部分:左部分 <= 轴点 <= 右部分 再分 ...
- 【面试题041】和为s的两个数字VS和为s的连续正数序列
[面试题041]和为s的两个数字VS和为s的连续正数序列 题目一: 输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和等于s,输出任意一对即可. ...
- JSONObject 包的依赖
commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json- ...
- UVA 10780 Again Prime? No Time. 分解质因子
The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...