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
 
Solution:区间第K大,主席树模板
 #include <iostream>
#include <cstdio>
#include <algorithm>
#define N 100000
using namespace std;
int sum[],ls[],rs[],root[N+],hash[N+],a[N+],num[N+];
int n,m,cnt,size;
int findpos(int x)
{
int l=,r=cnt,mid;
while (l<=r)
{
mid=(l+r)>>;
if (hash[mid]<x) l=mid+;
else r=mid-;
}
return l;
} void updata(int l,int r,int x,int &y,int v)
{
y=++size; sum[y]=sum[x]+;
if (l==r) return;
ls[y]=ls[x]; rs[y]=rs[x];
int mid=(l+r)>>;
if (v<=mid) updata(l,mid,ls[x],ls[y],v);
else updata(mid+,r,rs[x],rs[y],v);
} int query(int l,int r,int x,int y,int k)
{
if (l==r) return l;
int mid=(l+r)>>;
if (sum[ls[y]]-sum[ls[x]]>=k) return query(l,mid,ls[x],ls[y],k);
else return query(mid+,r,rs[x],rs[y],k-(sum[ls[y]]-sum[ls[x]]));
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++)
{
scanf("%d",&a[i]);
num[i]=a[i];
}
sort(num+,num+n+);
hash[++cnt]=num[];
for (int i=;i<=n;i++)
if (num[i]!=num[i-])
hash[++cnt]=num[i];
for (int i=;i<=n;i++)
{
int t=findpos(a[i]);
updata(,cnt,root[i-],root[i],t);
}
for (int i=;i<=m;i++)
{
int a,b,x,re;
scanf("%d%d%d",&a,&b,&x);
re=query(,cnt,root[a-],root[b],x);
printf("%d\n",hash[re]);
}
return ;
}

【POJ2104/2761】K-th Number的更多相关文章

  1. 【codeforces 546D】Soldier and Number Game

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【11.61%】【codeforces 670F】Restore a Number

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 514A】Chewbaсca and Number

    [题目链接]:http://codeforces.com/contest/514/problem/A [题意] 允许你把每个数字翻转 ->x变成9-x 然后问你能够变成的最小的数字是什么; 不能 ...

  4. 【分类算法】K近邻(KNN) ——kd树(转载)

    K近邻(KNN)的核心算法是kd树,转载如下几个链接: [量化课堂]一只兔子帮你理解 kNN [量化课堂]kd 树算法之思路篇 [量化课堂]kd 树算法之详细篇

  5. 【学习笔记】K 短路问题详解

    \(k\) 短路问题简介 所谓"\(k\) 短路"问题,即给定一张 \(n\) 个点,\(m\) 条边的有向图,给定起点 \(s\) 和终点 \(t\),求出所有 \(s\to t ...

  6. 【POJ 3696】 The Luckiest number

    [题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...

  7. 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)

    Yet Another Number Sequence Description Everyone knows what the Fibonacci sequence is. This sequence ...

  8. 【洛谷 P1216】【IOI1994】【USACO1.5】数字三角形 Number Triangles

    (如此多的标签qaq) 数字三角形 Number Triangles[传送门] 本来打算当DP练的,没想到写着写着成递推了(汗) 好的没有时间了,我们附个ac代码(改天不写): #include< ...

  9. 【LeetCode 25】K 个一组翻转链表

    题目链接 [题解] 模拟就好. 就k个k个节点地翻转. 每个节点都把next域指向它前面那个节点 修改完之后把这个节点前面的那个节点的next域改成这一段的最后一个节点. 然后把这一段最左边的那个节点 ...

随机推荐

  1. Newtonsoft.Json(Json.Net)学习笔记(转)

    概述 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库,通过Nuget获取.(查看原文) 下面是Json序列化和反序列化的简单封装: /// <summary&g ...

  2. Sexagenary Cycle(天干地支法表示农历年份)

    Sexagenary Cycle Time Limit: 2 Seconds      Memory Limit: 65536 KB 题目链接:zoj 4669 The Chinese sexagen ...

  3. SQLAlchemy Core插入数据,有好几种方法呢

    看是一次插入一条还是多条, 看是数据表名是变量还是常量, 操作还是很灵活的, 主要看哪种顺手顺眼啦. #coding=utf-8 from datetime import datetime from ...

  4. T-SQL 常用语句

    1. 查看 Table 或者 Column 被那些object(存储过程.函数或View)调用. select a.* from sysobjects a, syscomments b where a ...

  5. Android ViewPager使用详解(转)

    这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那如 ...

  6. 使用Aspose.Cell.dll导出Excel总结

    这两天项目上用Aspose导出Excel来着.开始感觉挺简单的,但是实际操作起来还是挺复杂的,调试占的时间很长.主要是动态生成列.合并单元格.调样式占了很长时间,还是总结一下吧. 基础操作: //EX ...

  7. Java学习笔记(八)——封装

    一.封装 1.定义 将类的信息隐藏在类的内部,不允许外部程序直接进行访问,而是通过该类提供的方法来实现对隐藏信息的操作和方法. 2.优点 (1)只能通过规定的方法访问数据 (2)隐藏类的细节,方便修改 ...

  8. TextView展开和收回

    第一步:接口请求返回数据 第二步:使用handler和textview.getLineCount方法判断是否超过指定行数: community_desc_more.setVisibility(View ...

  9. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  10. Android利用Fiddler进行网络数据抓包

    最新最准确内容建议直接访问原文:Android利用Fiddler进行网络数据抓包 主要介绍Android及IPhone手机上如何进行网络数据抓包,比如我们想抓某个应用(微博.微信.墨迹天气)的网络通信 ...