程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> K

K

編輯:關於C++

 

Language: K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 40048   Accepted: 13081 Case Time Limit: 2000MS

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

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

Sample Output

5
6
3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion
題意:給定一個數組a1,a2,a3.....an和m個三元組表示的查詢。對於每個查詢(i,j,k),輸出ai,ai+1,.....,aj的升序排列中第k個數。

 

思路:和以前做的線段樹有點不同,這次每個點保存了一個數組。建立線段樹的過程和歸並排序類似,而每個節點的數列就是其兩個兒子節點的數列合並後的結果。

代碼:

 

#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Tree
{
    int l,r;
    vectord;
}tree[maxn<<2];

int n,m;
int num[maxn];

void build(int rt,int l,int r)
{
    tree[rt].l=l;
    tree[rt].r=r;
    if (tree[rt].l==tree[rt].r)
    {
        tree[rt].d.push_back(num[tree[rt].l]);
        return ;
    }
    int mid=(tree[rt].l+tree[rt].r)>>1;
    build(lson);
    build(rson);
    tree[rt].d.resize(tree[rt].r-tree[rt].l+1);
    //利用STL的merge函數吧兩個兒子的數列合並
    merge(tree[rt<<1].d.begin(),tree[rt<<1].d.end(),tree[rt<<1|1].d.begin(),tree[rt<<1|1].d.end(),tree[rt].d.begin());
}


//計算[i,j]中不超過x的個數,k是節點編號
int query(int rt,int l,int r,int x)
{
    if (tree[rt].rr) //完全不相交
        return 0;
    else if (l<=tree[rt].l&&tree[rt].r<=r) //完全包含在裡面
        return upper_bound(tree[rt].d.begin(),tree[rt].d.end(),x)-tree[rt].d.begin();
    else
    {
        int lc=query(rt<<1,l,r,x);
        int rc=query(rt<<1|1,l,r,x);
        return lc+rc;
    }
}

int solve(int l,int r,int k)        //二分
{
    int L=1,R=n,ans=1;
    while (L<=R)
    {
        int mid=(L+R)>>1;
        int c=query(1,l,r,num[mid]);
        if (c>=k)
        {
            ans=mid;
            R=mid-1;
        }
        else
            L=mid+1;
    }
    return ans;
}

int main()
{
    int i,j;
    while (~sff(n,m))
    {
        int l,r,k;
        FRE(i,1,n)
            sf(num[i]);
        build(1,1,n);
        sort(num+1,num+n+1);
        FRL(i,0,m)
        {
            //查找[l,r]中的第k個數
            sfff(l,r,k);
            pf("%d\n",num[solve(l,r,k)]);
        }
    }
    return 0;
}


 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved