Flowers

Problem 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 Si and Ti (1 <= Si <= Ti <= 10^9), means i-th flower will be blooming at time [Si, Ti].
In the next M lines, each line contains an integer Ti, 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

分析

  我觉得你只要不傻,应该都能看出来这是个树状数组区间修改单点查询的板子题,但数据好像有点大,开数组是肯定开不下的,而最多就1e5朵花,所以在1e9之间有很多数字都被浪费掉了,所以使用离散化,离散化时记得将相同的数标记成一样的数字,还有询问也要离散化。

  然鹅这道题我很好奇的试了试,没有离散化直接让开1e5的数组然后跑树状数组,让我非常吃惊的是,它A了! HDU数据好水 出题人的意思肯定是让你用离散化,所以……看代码吧

  

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int N=1e5+;
  6. int tr[N+],a[N+];
  7. struct Node{
  8. int id,x;
  9. bool operator <(const Node &A)const {
  10. return x<A.x;//方便处理相等的情况
  11. }
  12. }p[N+];
  13. //树状数组开始
  14. int lowbit(int x){
  15. return x&(-x);
  16. }
  17. void Add(int x,int w){
  18. while(x<=N){
  19. tr[x]+=w;
  20. x+=lowbit(x);
  21. }
  22. }
  23. int Sum(int x){
  24. int ans=;
  25. while(x){
  26. ans+=tr[x];
  27. x-=lowbit(x);
  28. }
  29. return ans;
  30. }
  31. //树状数组结束
  32. int main(){
  33. int n,cas=;
  34. scanf("%d",&n);
  35. while(n--){
  36. printf("Case #%d:\n",++cas);
  37. int totn,totm;
  38. scanf("%d%d",&totn,&totm);
  39. totn*=;
  40. totm+=totn;
  41. for(int i=;i<=totm;i++){
  42. scanf("%d",&p[i].x);
  43. p[i].id=i;
  44. //所有数据一次读完,并记录i的值,因为结构体排序后顺序被打乱
  45. }
  46. sort(p+,p+totm+);
  47. a[p[].id]=;
  48. int k=;
  49. for(int i=;i<=totm;i++){
  50. if(p[i].x==p[i-].x)a[p[i].id]=k;//相等的值肯定也要离散成一样的呀
  51. else a[p[i].id]=++k;
  52. }
  53. memset(tr,,sizeof(tr));
  54. for(int i=;i<=totn;i+=){
  55. Add(a[i],);
  56. Add(a[i+]+,-);
  57. }
  58. for(int i=totn+;i<=totm;i++){
  59. printf("%d\n",Sum(a[i]));
  60. }
  61. }
  62. return ;
  63. }

HDU 4325 Flowers 树状数组+离散化的更多相关文章

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

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

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

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

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

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

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

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

  5. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  6. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  7. HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...

  8. 利用id来进行树状数组,而不是离散化以后的val HDU 4417 离线+树状数组

    题目大意:给你一个长度为n的数组,问[L,R]之间<=val的个数 思路:就像标题说的那样就行了.树状数组不一定是离散化以后的区间,而可以是id //看看会不会爆int!数组会不会少了一维! / ...

  9. HDU 5792 World is Exploding(树状数组+离散化)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...

随机推荐

  1. 关于Newtonsoft.Json引用报错

    自己运行的vs版本是2012,然后同事用了2017的,我把代码发给他后运行发现报以下错误: {未能加载文件或程序集"Newtonsoft.Json, Version=4.5.0.0, Cul ...

  2. 达拉草201771010105《面向对象程序设计(java)》第九周学习总结

    达拉草201771010105<面向对象程序设计(java)>第九周学习总结 实验九异常.断言与日志 实验时间 2018-10-25 1.实验目的与要求 (1) 掌握java异常处理技术: ...

  3. python xlwings Excel 内容截图

    import xlwings as xw from PIL import ImageGrab def excel_save_img(path, sheet=0, img_name="1&qu ...

  4. Python开发(三):字符编码,文件操作,函数

    一:三级菜单 If len(choice) == continue # 判断输入的是否为空,为空就跳出这次循环进行下次循环, exit(“bye”) :退出程序显示,bye 二:编码 最早的编码是as ...

  5. SDWebImage -- 封装 (网络状态检测,是否打开手机网络下下载高清图设置)

    对SDWebImage 进行封装,为了更好的节省用户手机流量,并保证在移动网络下也展示高清图,对使用SDWebImage 下载图片之前进行逻辑处理,根据本地缓存中是否有缓存原始的图片,用户是否打开移动 ...

  6. 压力测试(三)-自定义变量和CSV可变参数实操

    1.Jmeter用户自定义变量实战 简介:什么是用户自定义变量,怎样使用 为什么使用:很多变量在全局中都有使用,或者测试数据更改,可以在一处定义,四处使用 比如服务器地址 1.线程组->add ...

  7. paillier加密算法原理详解

    paillier加密算法是一种公钥加密算法,基于复合剩余类的困难问题.满足加法同态,即密文相乘等于明文相加:D(E(m1)·E(m2))=m1+m2.这里详细介绍其加密解密是如何推导的,需要具备数论. ...

  8. 记一次苹果APP从账号续费到发布成功的历程

    一.一波三折的续费      最近公司开发的苹果APP的SSL证书到期了,计划重新发布一下该APP,已替换即将到期的SSL证书.近几年随着钉钉.企业微信等在线办公软件超级平台的出现,各企业都会选择其中 ...

  9. Python中的BeautifulSoup库简要总结

    一.基本元素 BeautifulSoup库是解析.遍历.维护“标签树”的功能库. 引用 from bs4 import BeautifulSoup import bs4 html文档-标签树-Beau ...

  10. 学h5前端开发前必知的三大流行趋势

    学h5前端开发前必知的三大流行趋势 随着互联网时代的飞速发展,各种互联网的Web应用程序层出不穷,很多人对于HTML5前端开发的过程充满了好奇,但是却没有了解到前端开发的未来发展趋势.下面,云慧学院专 ...