正文

线段树的应用PKU27612008-08-22 16:41:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lqf110/37787.html

分享到:

题目链接:
http://acm.pku.cn/JudgeOnline/problem?id=2761

Feed the dogs



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
//PKU2761
//本题采用从小到大的顺序
// 区间长度为输入的cat个数
//结点权值为pretty值
#include <iostream>
#include<algorithm>
#include<cstdio>

using namespace std;

typedef struct
{
    int lbound, rbound;
    int left, right;
    int ct;
}TreeNode;

int ary[100010];
int bry[100010];

class SegmentTree
{
    public:
        int nodenum;
        TreeNode* tree;
    public:
        SegmentTree ( int );
        void build ( int, int );
        void insert ( int );
        int search ( int );
        void del ( int );
        virtual ~SegmentTree() {delete[]tree;}
};

SegmentTree::SegmentTree ( int a )
{
    tree = new TreeNode[2*a+10];
    nodenum = 0;
    build ( 1, a );
}

void SegmentTree::build ( int a, int b )
{
    int f, mid;
    nodenum++;
    f = nodenum;
    tree[f].lbound = a;
    tree[f].rbound = b;
    tree[f].ct  = 0;
    if ( ( b-a ) >= 1 )
    {
        mid = ( a+b ) >>1;
        tree[f].left = nodenum+1;
        build ( a, mid );
        tree[f].right = nodenum+1;
        build ( mid+1, b );
    }
}

//典型的二分插入,按从小到大的排列
void SegmentTree::insert ( int a )
{
    int mid,f;
    f = 1;
    while ( 1 )
    {
        tree[f].ct++;
        if ( tree[f].rbound == tree[f].lbound ) break;
        mid = ( tree[f].lbound+tree[f].rbound ) >>1;
        if ( ary[a] > bry[ mid] ) f = tree[f].right;
        else f = tree[f].left;
    }
}
//删除
void SegmentTree::del ( int a )
{
    int f, mid;
    f = 1;
    while ( 1 )
    {
        tree[f].ct--;
        if ( tree[f].rbound == tree[f].lbound ) break;
        mid = ( tree[f].lbound+tree[f].rbound ) >>1;
        if ( ary[a] > bry[mid] ) f = tree[f].right;
        else f = tree[f].left;
    }
}
//查询
int SegmentTree::search ( int  a )
{
    int f;
    f = 1;
    while ( 1 )
    {
        if ( tree[f].rbound == tree[f].lbound ) break;
        if ( a >  tree[tree[f].left].ct )
        {
            a = a-tree[tree[f].left].ct;
            f  =  tree[f].right;
        }
        else f = tree[f].left;
    }
    return tree[f].rbound;
}

//查询区间结点
typedef struct Node
{
    int s;            //区间始点
    int e;            //区间终点
    int kth;            //第k个
    int index;        //此区间在所有区间的下标
    int res;            //查询后的结果
}Require;
Require req[100010];
//排序用的比较函数
bool cmps ( const Require& first, const Require& second )
{
    return first.s < second.s;
}

bool cmpi ( const Require& first, const Require& second )
{
    return first.index < second.index;
}

int main ( int argv, char* argc[] )
{
    int vex, opt, i, j, num, f, s;
    cin>>vex>>opt;
    SegmentTree seg ( vex );

    for ( i = 1; i <= vex; ++i )
    {
        scanf ( "%d", &ary[i] );
        bry[i] = ary[i];
    }
    for ( i = 1; i <= opt; ++i )
    {
        scanf ( "%d%d%d", &req[i].s, &req[i].e, &req[i].kth );
        req[i].index = i;
    }
    //将数据按大小排序
    //即所谓的离散化
    sort ( bry, bry+vex );
    //按始区间始点排序
    sort ( req, req+opt+1 , cmps );

    //设前驱区间为A[a, b],当前查询区间为B[c, d]
    //删除前驱区间与查询区间的差集,即A-B
    //插入查询区间与前驱区间的差集,即B-A
    //A与B就两种情况
    //b < c
    //b >= c
    //将第一个区间插入
    for ( i = req[1].s; i <= req[1].e; ++i )
        seg.insert ( i );
    req[1].res = seg.search ( req[1].kth );
    //处理后续区间
    for ( i = 2; i <= opt; ++i )
    {
        for ( j = req[i-1].s; ( j < req[i].s ) && ( j <= req[i-1].e ); ++j )
            seg.del ( j );
        for ( j = req[i].e; j >= req[i].s && j > req[i-1].e; --j )
            seg.insert ( j );
        req[i].res = seg.search ( req[i].kth );
    }
    //将所得结果排序后输出
    sort ( req, req+opt+1, cmpi );
    for ( i = 1; i <= opt; ++i )
        cout<<bry[req[i].res]<<endl;
    return 0;
}

阅读(3392) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册