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 numbered z, 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?
 

Input
Multi test cases (about 15).

For each case, first line contains two integers N and Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries.

In the following N lines, there will appear h[1],h[2],h[3],,h[N] which indicates the height of the trees.

In the following Q 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]

1N,Q50000

0h[i]1000000000(109)

0q[i]1000000000(109)
 

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
02
Hint
In 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
原创粉丝点击