Flowers(树状数组+区间更新+单点查询+区间更新单点查询模板)
来源:互联网 发布:国内咨询公司 知乎 编辑:程序博客网 时间:2024/05/16 09:15
Problem I
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 30 Accepted Submission(s) : 18
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 S[sub]i[/sub] and T[sub]i[/sub] (1 <= S[sub]i[/sub] <= T[sub]i[/sub] <= 10^9), means i-th flower will be blooming at time [S[sub]i[/sub], T[sub]i[/sub]]. In the next M lines, each line contains an integer T[sub]i[/sub], 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
21 15 1042 31 44 8146
Sample Output
Case #1:0Case #2:121
题意:
每种花有开放的时间段,然后给你某一个时间,求这一时间内的开花数量。
思路:
简单的树状数组,只需要区间更新单点查询即可,套模板就行
代码:
#include <iostream>#include <cstring>#include <stdio.h>#include <algorithm>const int maxn=1e5+10;using namespace std;int a[maxn];int n,m;int lowbit(int i){return i&-i;}int sum(int i){int summ=0;while(i>0){summ+=a[i];i-=lowbit(i);}return summ;}void add(int i,int x){while(i<=100005){a[i]+=x;i+=lowbit(i);}}int main(){int t;int i,j;int s,e;scanf("%d",&t);for(int ss=1;ss<=t;ss++){memset(a,0,sizeof(a));printf("Case #%d:\n",ss);scanf("%d%d",&n,&m);while(n--){scanf("%d%d",&s,&e);add(s,1);add(e+1,-1);}while(m--){scanf("%d",&j);printf("%d\n",sum(j));}}return 0;}
阅读全文
0 0
- Flowers(树状数组+区间更新+单点查询+区间更新单点查询模板)
- 树状数组模板(区间更新单点查询)
- hdu1166 树状数组模板:单点更新,区间求和(区间查询)
- poj2155树状数组 区间更新 单点查询
- 树状数组的单点更新,区间查询。
- 树状数组区间更新+区间查询+单点查询
- <模板>区间更新,单点查询
- hdu - 4325- Flowers - 区间更新,单点查询
- hdu 4533(树状数组区间更新+单点查询)
- 三维树状数组(区间更新,单点查询)POJ
- 树状数组单点更新和区间更新,二维数组poj2155(区间更新,单点查询)(已加入区间修改区间查询)
- 树状数组:HDU1166敌兵布阵 【单点更新,区间查询】
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- POJ 2155 - Matrix 二维树状数组..区间更新..单点查询
- poj 2155 二维树状数组/区间更新单点查询
- [HDU 4031]Attack[树状数组区间更新单点查询]
- hdu2642二维树状数组单点更新+区间查询
- 【HDOJ 3584】 Cube(树状数组<区间更新,单点查询>)
- poj3237(树链剖分边维护+线段树区间更新)
- 排序算法之选择排序
- git 常用命令
- bzoj1867: [Noi1999]钉子和小球
- OpenGL绘图过程中,平移后再次旋转,旋转中心为什么变了?
- Flowers(树状数组+区间更新+单点查询+区间更新单点查询模板)
- RSA非对称加密(java版)
- ceph bluestore bcache 磁盘对齐对于性能影响
- Java对MongoDB的基础操做
- 单例
- wagger
- 一款RESTFUL接口的文档在线自动生成+功能测试功能软件——Swagger简介
- python2数据聚类实例
- iptables 如何防止 syn ddos ping 攻击