hdu 4325 Flowers
2015-11-14 19:21
323 查看
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
Sample Output
题目大意很简单,每组数据给你n,m;n代表n段种花范围;m代表m次询问,每次询问有一个数字,问当前位置花的多少;
思路很简单,离散化加线段树;
需要注意的是把询问的位置也要加入离散化,做的时候没加,一直错,,,,
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
题目大意很简单,每组数据给你n,m;n代表n段种花范围;m代表m次询问,每次询问有一个数字,问当前位置花的多少;
思路很简单,离散化加线段树;
需要注意的是把询问的位置也要加入离散化,做的时候没加,一直错,,,,
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct point { int x,y; }; struct SegmentTree { int l,r,x; }; point a[100005]; SegmentTree tree[1200005]; int p[300005],f[100005]; void update_down(int k) { tree[k<<1].x += tree[k].x ; tree[k<<1|1].x += tree[k].x; tree[k].x = 0; } void build(int l,int r, int k) { tree[k].l=l, tree[k].r=r, tree[k].x=0; if(l==r) return; int mid =(l+r)>>1; build(l,mid,k<<1); build(mid+1,r,k<<1|1); } void add(int l,int r,int k,int x) { if(tree[k].l>=l&&tree[k].r<=r) { tree[k].x += x; return ; } if(tree[k].x) update_down(k); if(tree[k<<1].r>=l) add(l,r,k<<1,x); if(tree[k<<1|1].l<=r) add(l,r,k<<1|1,x); } int query(int l,int r, int k) { if(tree[k].l>=l&&tree[k].r<=r) { return tree[k].x; } if(tree[k].x) update_down(k); int ans=0; if(tree[k<<1].r>=l) ans += query(l,r,k<<1); if(tree[k<<1|1].l<=r) ans += query(l,r,k<<1|1); return ans; } int main() { int t; scanf("%d",&t); for(int c=1; c<=t; c++) { int n,m,s=0; scanf("%d%d",&n,&m); build(1,2*n+m,1); for(int i=0; i<n; i++) { scanf("%d%d",&a[i].x,&a[i].y); p[s++] = a[i].x; p[s++] = a[i].y; } for(int i=0; i<m; i++) { scanf("%d",&f[i]); p[s++] = f[i]; } sort(p,p+s); for(int i=0; i<n; i++) { a[i].x = lower_bound(p,p+s,a[i].x) - p + 1; a[i].y = lower_bound(p,p+s,a[i].y) - p + 1; } for(int i=0; i<n; i++) add(a[i].x, a[i].y,1,1); printf("Case #%d:\n",c); for(int i=0; i<m; i++) { int k = lower_bound(p,p+s,f[i]) - p + 1; printf("%d\n",query(k,k,1)); } } return 0; }
相关文章推荐
- 在公司中怎样使用SVN、Git
- ARM汇编伪指令介绍(全集)
- 老王的故事
- 在c++中怎么实现把char实现int形输出
- 11.晃动事件
- 01 CSDN心语
- 在PHP中使用AES加密算法加密数据
- Shell 中参数变量$0,$?,$!,$#等的含义
- 项目中遇到的问题总结
- Android开发手记(26) Java多线程的实现
- C++如何向函数传递数组
- Some Laws in IT
- 访问修饰符
- Android IBinder机制简单介绍
- MySQL常用命令
- 动态库的创建和使用
- POJ 3764 The xor-longest Path (01字典树 + DFS)
- 你知道属性(字段)与构造函数谁先执行吗?
- kafka模拟客户端发送、接受消息
- SQL内置函数