题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325

Description

As is known to all, the blooming time and duration varies between different kinds of flowers. Now there is a garden planted full of flowers. The gardener wants to know how many flowers will bloom in the garden in a specific time. But there are too many flowers in the garden, so he wants you to help him.
 

Input

The first line contains a single integer t (1 <= t <= 10), the number of test cases. 
For each case, the first line contains two integer N and M, where N (1 <= N <= 10^5) is the number of flowers, and M (1 <= M <= 10^5) is the query times. 
In the next N lines, each line contains two integer S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, Ti]. 
In the next M lines, each line contains an integer T i, means the time of i-th query. 
 

Output

For each case, output the case number as shown and then print M lines. Each line contains an integer, meaning the number of blooming flowers. 
Sample outputs are available for more details. 
 

Sample Input

2
1 1
5 10
4
2 3
1 4
4 8
1
4
6
 

Sample Output

Case #1:
0
Case #2:
1
2
1
 

题意:有n种花,给了这n种花的花期时间,Si~Ti,求在某一时间t,有多少种花正在开放。

思路:这题给的花期时间数据为1~10^9,无法开辟这么大的数组,不能直接建树,必须先将数据进行离散化。离散化:在百科上看到一句很好的话:“离散化就是把连续的量,变成离散的量即变成一个一个的值”,例如区间(1,100)由于这是一个实数区间,其中间的值有无数个,如果我们能把它变为1到100内的整数,这样这些数就变成了有限个,即离散了;  ,我们将每个区间的端点都存到一个数组中,然后将这些端点,按照从小到大排列(之后要去除这个数组中重复的点),并建立为与其下标的映射,然后用其下标建树,因为我们只是使用了需要的空间,并没有在整个空间上建树,这样就大大节省了空间和时间,如题中第二个例子,我们将所有数据按照从小到大排列后为1 2 3 4 6 8 分别对应下标1 2 3 4 5 6,此题数据小我们看不出明显的差别,但是如果数据中有区间(1000,10000),那差别马上就出来了,比如我们把题中的区间(4,8)换做(1000,10000)那么如果采用离散化思想,我们还是先排列大小 1 2 3 6 1000 10000对应下标1 2 3 4 5 6,我们只需建一棵根为6的树即可,如果不用离散化,我们就需要建造根为10000的树,大大浪费了空间。

然而!!!我发现同学他们的代码没有使用离散化,而是开辟了150000的空间就过了,很明显测试数据没有很大,都在150000以内,唉!所以可以不用离散化。

方法一:使用树状数组,若花期为t1~t2,更新t1-1及t1-1以下的子树根节点都减一,更新t2及t2以下的子树根节点加一,若求x时刻有多少种花正开着,将x及上方一路节点均相加,最后的和即是结果。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define N 152005
using namespace std;
int c[N]; int Lowbit(int t)
{ ///设k为t末尾0的个数,则求得为2^k=t&(t^(t-1));
return t&(t^(t-));
}
void update(int x,int t)
{
while(x > )
{
c[x]+=t;
x -= Lowbit(x);
}
}
int sum(int li)
{
int sum=;
while(li<=N)
{
sum+=c[li];
li=li+Lowbit(li);
}
return sum;
} int main()
{
int T;
int n,M,t1,t2,x,Case=;
scanf("%d",&T);
while(T--)
{
memset(c,,sizeof(c));
scanf("%d%d",&n,&M);
while(n--)
{
scanf("%d%d",&t1,&t2);
update(t1-,-);
update(t2,);
}
printf("Case #%d:\n",Case++);
while(M--)
{
scanf("%d",&x);
printf("%d\n",sum(x));
}
}
return ;
}

方法二:使用线段树进行区间更新。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int Max=;
int N,M;
int c[Max]; struct Node
{
int l,r;
int cnt;
}node[*Max]; void build(int L,int R,int i)
{
node[i].l=L;
node[i].r=R;
node[i].cnt=;
if(L==R) return;
int mid=(L+R)/;
build(L,mid,*i);
build(mid+,R,*i+);
} int update(int t1,int t2,int i)
{
if(node[i].l==t1&&node[i].r==t2)
{
node[i].cnt++;
return ;
}
int mid=(node[i].l+node[i].r)/;
if(t2<=mid) update(t1,t2,*i);
else if(t1>mid) update(t1,t2,*i+);
else
{
update(t1,mid,*i);
update(mid+,t2,*i+);
}
} int chazhao(int x,int i)
{
int sum=;
if(node[i].l<=x&&node[i].r>=x)
sum+=node[i].cnt;
if(node[i].l>x) return ;
if(node[i].r<x) return ;
sum+=chazhao(x,*i);
sum+=chazhao(x,*i+);
return sum;
} int main()
{
int T,t1,t2,Case=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
build(,Max,);
for(int i=;i<=N;i++)
{
scanf("%d%d",&t1,&t2);
update(t1,t2,);
}
for(int i=;i<M;i++)
scanf("%d",&c[i]);
printf("Case #%d:\n",Case++);
for(int i=;i<M;i++)
{
printf("%d\n",chazhao(c[i],));
}
}
return ;
}

线段树或树状数组---Flowers的更多相关文章

  1. BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...

  2. 5.15 牛客挑战赛40 E 小V和gcd树 树链剖分 主席树 树状数组 根号分治

    LINK:小V和gcd树 时限是8s 所以当时好多nq的暴力都能跑过. 考虑每次询问暴力 跳父亲 这样是nq的 4e8左右 随便过. 不过每次跳到某个点的时候需要得到边权 如果直接暴力gcd的话 nq ...

  3. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  4. HDU 1556 线段树或树状数组,插段求点

    1.HDU 1556  Color the ball   区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...

  5. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  6. 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings

    谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...

  7. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  8. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  9. Turing Tree_线段树&树状数组

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

随机推荐

  1. POJ1226:Substrings(后缀数组)

    Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...

  2. git第一次提交代码到远程仓库

    博客搬家了,本文新地址:http://www.zicheng.net/article/4 感谢支持 本操作说明是先有代码,后来创建git仓库,然后把本地代码提交到远程仓库的操作步骤: 1.初始化 在当 ...

  3. wndows系统命令总结

    window8系统下 打开运行窗口----------鼠标放到任务栏的windows图标下,右击,弹出菜单中如上图或者 打开运行窗口---------按“WIN+R”键, cmd-------打开命令 ...

  4. Android开发在路上:少去踩坑,多走捷径

    转自:http://djt.qq.com/article/view/1193   最近一朋友提了几个Android问题让我帮忙写个小分享,我觉得对新人还是挺有帮助的,所以有了这个小分享. 1.目前, ...

  5. PowerDesigner 16.5对SQL Server 2012 生成数据库时"不支持扩展属性"问题

    团队合作设计一套系统数据模型,创建了PDM后,Table.View.Store Procedure等都创建好了,且创建了多个Schema方便管理这些数据库对象,但Table.view.Column等对 ...

  6. 爬网页?--Chrome帮你计算XPath

    最近用HtmlUnit/HtmlCleaner爬网页,这两个工具都使用XPath来定位html元素.发现chrome竟然有算出XPath的功能! 打开一个网页,F12,在弹出的小窗口中选中一个标签,右 ...

  7. CentOS下mysql安装和配置

    1.卸载原有mysql [root@iZ25ka6ra32Z /]# rpm -qa | grep mysql 查看该操作系统上是否已经安装了mysql数据库.有的话,我们就通过 rpm -e 命令 ...

  8. [Git] 快速签出与更新所有远程分支.md

    git-fetch 命令从远程仓库复制 heads 和 tags 信息到本地,保存在临时文件 .git/FETCH_HEAD 中以备 git-merge 命令使用. 你可以使用 git fetch 命 ...

  9. apache 500错误

    一直以为开了error_log,没想没有加,于是折腾了好久. 开启error_log后,发现是xdebug的max_nesting_level值太小了. 还一个原因是.htaccess文件中的 < ...

  10. Uvaoj 11248 Frequency Hopping(Dinic求最小割)

    题意:1到n节点(节点之间有一定的容量),需要流过C的流量,问是否可以?如果可以输出possible, 否则如果可以扩大任意一条边的容量 可以达到目的,那么输出possible option:接着输出 ...