HDU 4325 Flowers(树状数组)
2016-09-16 21:23
288 查看
Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3150 Accepted Submission(s): 1549
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
Author BJTU
Source 2012 Multi-University Training Contest 3
/* 给你每一朵话的开花时间段,询问你某一时刻的开花数量 */ /* 重新定义树状数组的意义,不再是前i个数的和,而是第i个位置的数值 */ /* 明显数据会爆的,我去.....数据太水了 */ #include<iostream> #include<string.h> #include<stdio.h> #define N 100010 using namespace std; int c ,T ; int t; int n,m; int lowbit(int x) { return x&(-x); } void update(int x,int val) { while(x<=N) { c[x]+=val; x+=lowbit(x); } } int getsum(int x) { int s=0; while(x>0) { s+=c[x]; x-=lowbit(x); } return s; } int main() { //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin); scanf("%d",&t); for(int Case=1;Case<=t;Case++) { memset(c,0,sizeof c); scanf("%d%d",&n,&m); //cout<<"n="<<n<<" "<<"m="<<m<<endl; int si,ti; for(int i=0;i<n;i++) { scanf("%d%d",&si,&ti); //cout<<si<<" "<<ti<<endl; update(si,1); // for(int j=si;j<=ti;j++) // update(j,1); update(ti+1,-1); } for(int i=0;i<m;i++) scanf("%d",&T[i]); printf("Case #%d:\n",Case); // for(int i=1;i<10;i++) // printf("%d\n",getsum(i)); for(int i=0;i<m;i++) { //cout<<"T[i]="<<T[i]<<" "<<"T[i]-1="<<T[i]-1<<endl; printf("%d\n",getsum(T[i])); } } }
相关文章推荐
- 95. Unique Binary Search Trees II
- Autoconf中AC_CHECK_LIB依赖第三方库时FAIL
- Oracle 12c安装方法及一些使用问题
- 单例模式
- bzoj2049(lct)
- 从尾到头打印链表
- R.java文件介绍
- Android系统结构、Android与ios的区别
- Android添加快捷方式
- 面向对象
- Python操作redis
- bzoj 2142 礼物
- Linux调度时机
- 对类内静态变量的分析
- HttpClient学习笔记
- 两个数互换
- [置顶] 【NuGet】NuGet发布原创源程序
- 素数 筛选获取
- Android应用闪屏页延迟跳转的三种写法
- 接口