HDU 5200 Trees(线段树 离线应用)经典
来源:互联网 发布:yungou 源码 编辑:程序博客网 时间:2024/03/28 19:01
Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1117 Accepted Submission(s): 353
Problem Description
Today CodeFamer is going to cut trees.There are N trees standing in a line. They are numbered from 1 to N . The tree numbered i has height hi . We say that two uncutted trees whose numbers are x and y are in the same block if and only if they are fitting in one of blow rules:
1)x+1=y or y+1=x;
2)there exists an uncutted tree which is numberedz , and x is in the same block with z , while y is also in the same block with z .
Now CodeFamer want to cut some trees whose height is not larger than some value, after those trees are cut, how many tree blocks are there?
1)x+1=y or y+1=x;
2)there exists an uncutted tree which is numbered
Now CodeFamer want to cut some trees whose height is not larger than some value, after those trees are cut, how many tree blocks are there?
Input
Multi test cases (about 15 ).
For each case, first line contains two integersN and Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries.
In the followingN lines, there will appear h[1],h[2],h[3],…,h[N] which indicates the height of the trees.
In the followingQ lines, there will appear q[1],q[2],q[3],…,q[Q] which indicates CodeFamer’s queries.
Please process to the end of file.
[Technical Specification]
1≤N,Q≤50000
0≤h[i]≤1000000000(109)
0≤q[i]≤1000000000(109)
For each case, first line contains two integers
In the following
In the following
Please process to the end of file.
[Technical Specification]
Output
For each q[i] , output the number of tree block after CodeFamer cut the trees whose height are not larger thanq[i] .
Sample Input
3 252362
Sample Output
02HintIn this test case, there are 3 trees whose heights are 5 2 3.For the query 6, if CodeFamer cuts the tree whose height is not large than 6, the height form of left trees are -1 -1 -1(-1 means this tree was cut). Thus there is 0 block.For the query 2, if CodeFamer cuts the tree whose height is not large than 2, the height form of left trees are 5 -1 3(-1 means this tree was cut). Thus there are 2 blocks.
Source
BestCoder Round #36 ($)
题意:有n棵树排成一行,都有一个树的高度,再在有 m 次询问,每次给一个高度h,问有多少个连续块树的高度是 > h 。
解题:先用结构体node[1....N]记录每棵树的高度与位置并且记录所在的位置 ,再用结构体 q[1....m] 记录 m 次询问的高度h且是第几次的询问,再以高度从小到大把node[] 和 q[] 排序,再以高度从小到大的询问,第次把树的高度<= 询问的高度h 的那些树一个一个的删掉,用线段树维护剩下来树的连续块,这样一来,每棵树只删一次,所以时间复杂度O( m*log(n) ) 。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int N = 50010 ;#define lson l,m,k<<1#define rson m+1,r,k<<1|1struct NODE{ int h , id , ans;}node[N] , q[N];struct Tree{ int ans ; bool bl , br ;}root[N<<3];void builde(int l,int r,int k){ root[k].ans = 1; root[k].bl = root[k].br = 1; if(l==r) return ; int m=(l+r)>>1; builde(lson); builde(rson);}void update(int l,int r,int k,int id){ if(l==r){ root[k].bl=root[k].br=0; root[k].ans = 0; return ; } int m = (l+r)>>1; if(id<=m) update(lson , id); else update(rson , id) ; root[k].ans = root[k<<1].ans + root[k<<1|1].ans ; root[k].bl = root[k<<1].bl ; root[k].br = root[k<<1|1].br ; if( root[k<<1].br && root[k<<1|1].bl ) root[k].ans--;}bool cmp1(NODE aa , NODE bb){ return aa.h < bb.h ;}bool cmp2(NODE aa , NODE bb){ return aa.id < bb.id ;}int main(){ int n , m ; while(scanf("%d%d",&n,&m)>0) { builde(1 , n , 1 ) ; for(int i=1; i <= n ; i++) { scanf("%d",&node[i].h); node[i].id = i; } for(int i=1; i <= m ; i++) { scanf("%d",&q[i].h); q[i].id = i ; } sort(node+1 , node+1+n , cmp1); sort(q+1 , q+1+m , cmp1); for(int i=1 , j=1 ; i<=m; i++) { while(j<=n&&node[j].h<=q[i].h) { update(1 , n , 1 , node[j].id); j++ ; } q[i].ans = root[1].ans ; } sort(q+1 , q+1+m , cmp2); for(int i=1; i <=m; i++) printf("%d\n",q[i].ans); }}
0 0
- HDU 5200 Trees(线段树 离线应用)经典
- Hdu 5200 Trees (离线线段树)
- HDU 5200 - Trees (离线 + 思维)
- hdu 5200 trees(离线处理)
- HDU 3333 Turing Tree (离线线段树,经典)
- hdu 5200 Trees(离线处理,排序)
- HDU 5200 Trees 离线+链表维护.
- HDU 4417 离线线段树
- HDU 3333 离线线段树
- HDU 3874 离线线段树
- HDU 3015 Disharmony Trees (线段树)
- hdu 4417 线段树离线处理
- hdu 4348 线段树离线处理
- HDU 3333 & 3874 (线段树+离线询问)
- hdu 4638 Group(离线线段树)
- HDU 4417 Super Mario(离线线段树)
- HDU 4902 Nice boat 线段树+离线
- HDU 4031 Attack(离线+线段树)
- Tomcat体系结构
- leetcode--Product of Array Except Self
- CSS基础知识整理四
- 控件的一些基本操作,设置颜色,自动连接,插入图片,padding,ellipsize。
- Android应用程序组件(四)
- HDU 5200 Trees(线段树 离线应用)经典
- 并发编程网
- Pascal数组递推
- leetcode 162 —— Find Peak Element
- BFS解迷宫问题
- 一、数据结构 读书笔记(一)基本概念与算法
- WebView 用法
- 杭电ACM1372——Knight Moves~~BFS
- poj1995 Raising Modulo Numbers【快速幂】