随笔 - 87  文章 - 279  trackbacks - 0
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

潜心看书研究!

常用链接

留言簿(19)

随笔分类(81)

文章分类(89)

相册

ACM OJ

My friends

搜索

  •  

积分与排名

  • 积分 - 214376
  • 排名 - 116

最新评论

阅读排行榜

评论排行榜

Feed the dogs
Time Limit:6000MS  Memory Limit:65536K
Total Submit:1387 Accepted:222

Description
Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.

Your task is to help Jiajia calculate which dog ate the food after each feeding.

Input
The first line contains n and m, indicates the number of dogs and the number of feedings.

The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.

Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.

You can assume that n<100001 and m<50001.

Output
Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.

Sample Input

7 2
1 5 2 6 3 7 4
1 5 3
2 7 1

Sample Output

3
2

Source
POJ Monthly--2006.02.26,zgl & twb

#include  < iostream >
#include 
< algorithm >
using   namespace  std;

const   int  MAXN  =   100001 ;
const   int  MAXM  =   50001 ;

struct  PosData
{
    
int  b, e;
    
int  k;
    
int  index;
}
;

struct  Adata
{
    
int  index;
    
int  incode;
    
int  data;
}
;

Adata a[MAXN];
int  aa[MAXN];
PosData d[MAXM];
int  f[ 3 * (MAXN + 1 )]  =   { 0 } ;
int  n, m;
int   out [MAXM];

inline 
int  inorder(PosData x, PosData y)
{
    
return  x.b  <  y.b;
}


inline 
int  inorder2(Adata x, Adata y)
{
    
return  x.data  <  y.data;
}


inline 
int  inorder3(Adata x, Adata y)
{
    
return  x.index  <  y.index;
}


void  initTree()
{
    memset(f, 
0 sizeof (f));
}


void  insertTree( int  v)
{
    
int  l  =   1 , r  =  MAXN;
    
int  c  =   1 ;
    
int  mid;
    
while  (l  <  r)
    
{
        mid 
=  (l  +  r)  /   2 ;
        f[c]
++ ;
        
if  (v  >  mid)
        
{
            c 
=  c  *   2   +   1 ;
            l 
=  mid  +   1 ;
        }

        
else
        
{
            c 
=  c  *   2 ;
            r 
=  mid;
        }

    }

    f[c]
++ ;
}


void  delTree( int  v)
{
    
int  l  =   1 , r  =  MAXN;
    
int  c  =   1 ;
    
int  mid;
    
while  (l  <  r)
    
{
        mid 
=  (l  +  r)  /   2 ;
        f[c]
-- ;
        
if  (v  >  mid)
        
{
            c 
=  c  *   2   +   1 ;
            l 
=  mid  +   1 ;
        }

        
else
        
{
            c 
=  c  *   2 ;
            r 
=  mid;
        }

    }

    f[c]
-- ;
}


int  searchTree( int  k)
{
    
int  l  =   1 , r  =  MAXN;
    
int  c  =   1 ;
    
int  mid;
    
while  (l  <  r)
    
{
        mid 
=  (l  +  r)  /   2 ;
        
if  (k  <=  f[ 2 * c])
        
{
            c 
=   2   *  c;
            r 
=  mid;
        }

        
else
        
{
            k 
-=  f[ 2 * c];
            c 
=   2   *  c  +   1 ;
            l 
=  mid  +   1 ;
        }

    }

    
return  l;
}


int  main()
{
    
int  i, j;

    scanf(
" %d%d " & n,  & m);

    
for  (i = 1 ; i <= n; i ++ )
    
{
        scanf(
" %d " & a[i].data);
        a[i].index 
=  i;
    }

    sort(a
+ 1 , a + n + 1 , inorder2);
    
// 离散化
     for  (i = 1 ; i <= n; i ++ )
    
{
        a[i].incode 
=  i;
        aa[i] 
=  a[i].data;
    }

    sort(a
+ 1 , a + n + 1 , inorder3);

    
for  (i = 0 ; i < m; i ++ )
    
{
        scanf(
" %d%d%d " & d[i].b,  & d[i].e,  & d[i].k);
        d[i].index 
=  i;
    }

    sort(d, d
+ m, inorder);

    
for  (j = d[ 0 ].b; j <= d[ 0 ].e; j ++ )
        insertTree(a[j].incode);
    
out [d[ 0 ].index]  =  aa[searchTree(d[ 0 ].k)];

    
for  (i = 1 ; i < m; i ++ )
    
{
        
if  (d[i - 1 ].e  <  d[i].b)
        
{
            
for  (j = d[i - 1 ].b; j <= d[i - 1 ].e; j ++ )
                delTree(a[j].incode);
            
for  (j = d[i].b; j <= d[i].e; j ++ )
                insertTree(a[j].incode);
        }

        
else
        
{
            
for  (j = d[i - 1 ].b; j < d[i].b; j ++ )
            
{
                delTree(a[j].incode);
            }

            
for  (j = d[i - 1 ].e + 1 ; j <= d[i].e; j ++ )
            
{
                insertTree(a[j].incode);
            }

        }

        
        
out [d[i].index]  =  aa[searchTree(d[i].k)];
    }


    
for  (i = 0 ; i < m; i ++ )
        printf(
" %d\n " out [i]);
    

    
return   0 ;
}
posted on 2006-09-13 23:40 阅读(1910) 评论(6)  编辑 收藏 引用 所属分类: ACM题目

FeedBack:
# re: pku(2761)离散化+线段树 2007-03-28 18:58 过客
怎么注释这么少!!!!  回复  更多评论
  
# re: pku(2761)离散化+线段树 2007-08-15 19:06 lilu03555
最近在学线段树,今天用递归写了一个线段树,结果是错的,实在是台不爽了。。
看了你写的线段树,真的是他开眼界。。
佩服佩服。。  回复  更多评论
  
# re: pku(2761)离散化+线段树 2007-08-18 11:42 wcn(wengjiaxiang86@163.com)
pku2104差不多的一道题,
对k进行变换,
d[i].k=d[i].e-d[i]-b+2-d[i].k;
变成求第k大的元素.你的程序似乎连sample都不对呀?
请教一下....  回复  更多评论
  
# re: pku(2761)离散化+线段树 2007-08-18 21:07 wcn(wengjiaxiang86@163.com)
呃,是我看错了,不需要变换...
不过在判区间的时候得稍微改动一下,
不过超时了。  回复  更多评论
  
# re: pku(2761)离散化+线段树 2007-08-18 21:09 wcn(wengjiaxiang86@163.com)
呃,是我看错了,
不需要做变换。只是程序在判区间重叠的时候,得再加一个包含.
不过这样2104还是过不去,超时了,继续请教?  回复  更多评论
  
# re: pku(2761)离散化+线段树 2008-03-19 20:36 l-y-p
强大,向牛人学习了……  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理