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.
 

题目大意:给n个数,m次询问区间[l, r]的第k小数。

思路:对询问排序,再离线处理每一个询问(加减点直至平衡树对应区间等于询问区间),不过这样做复杂度是没有保障的……因此差不多的题目如POJ2104妥妥地TLE了……

关于正解之划分树和主席树这里不写。

关于划分树和主席树:

POJ 2104 K-th Number(划分树)

POJ 2104 K-th Number(不带修改主席树——附讲解)

代码(3438MS)(treap树):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ; int key[MAXN], weight[MAXN], child[MAXN][], size[MAXN];
int stk[MAXN], top, cnt; inline void init() {
top = cnt = ;
} inline int new_node(int k) {
int x = (top ? stk[top--] : ++cnt);
key[x] = k;
size[x] = ;
weight[x] = rand();
child[x][] = child[x][] = ;
return x;
} inline void update(int &x) {
size[x] = size[child[x][]] + size[child[x][]] + ;
} void rotate(int &x, int t) {
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
update(x); update(y);
x = y;
} void insert(int &x, int k) {
if(x == ) x = new_node(k);
else {
int t = key[x] < k;
insert(child[x][t], k);
if(weight[child[x][t]] < weight[x]) rotate(x, t);
}
update(x);
} void remove(int &x, int k) {
if(key[x] == k) {
if(child[x][] && child[x][]) {
int t = weight[child[x][]] < weight[child[x][]];
rotate(x, t); remove(child[x][t ^ ], k);
}
else {
stk[++top] = x;
x = child[x][] + child[x][];
}
}
else remove(child[x][key[x] < k], k);
if(x > ) update(x);
} int kth(int &x, int k) {
if(x == || k <= || k > size[x]) return ;
int s = ;
if(child[x][]) s = size[child[x][]];
if(k == s + ) return key[x];
if(k <= s) return kth(child[x][], k);
return kth(child[x][], k - s - );
} struct Node {
int l, r, k, id;
void read(int i) {
id = i;
scanf("%d%d%d", &l, &r, &k);
}
bool operator < (const Node &rhs) const {
if(r != rhs.r) return r < rhs.r;
return l < rhs.l;
}
}; Node query[MAXN];
int a[MAXN], ans[MAXN]; int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i < m; ++i) query[i].read(i);
sort(query, query + m);
int left = , right = , root = ;
for(int i = ; i < m; ++i) {
while(right < query[i].r)
if(!root) root = new_node(a[++right]);
else insert(root, a[++right]);
while(left > query[i].l)
if(!root) root = new_node(a[--left]);
else insert(root, a[--left]);
while(left < query[i].l) remove(root, a[left++]);
ans[query[i].id] = kth(root, query[i].k);
}
for(int i = ; i < m; ++i) printf("%d\n", ans[i]);
}

POJ 2761 Feed the dogs(平衡树or划分树or主席树)的更多相关文章

  1. poj 2761 Feed the dogs (treap树)

    /************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...

  2. POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                                Feed the dogs Time Limit: 6000MS   Memor ...

  3. POJ 2761 Feed the dogs

    主席树,区间第$k$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...

  4. poj 2104 K-th Number 划分树,主席树讲解

    K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...

  5. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  6. POJ 题目2761 Feed the dogs(主席树||划分树)

    Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16860   Accepted: 5273 De ...

  7. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Ca ...

  8. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  9. POJ 2104 K-th Number(分桶,线段树,主席树)

    一道比较经典的数据结构题.可以用多种方式来做. 一,分桶法(平方分解). 根据数字x的大小和区间内不大于x的数字数量cnt的单调性,可知第k大数kth对应的cnt应该满足cnt≥k, 且kth是满足条 ...

随机推荐

  1. SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-apidoc/ 本文出自方志朋的博客 首先声明下 ...

  2. Java Iterator ListIterator 理解

    一. Iterator 常用操作 next hasNext remove 先上源码:JDK8 简化版本,用于说明问题 private class Itr implements Iterator< ...

  3. hdu_2588_GCD

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  4. Lintcode算法

    题目: 给出一组非负整数,重新排列他们的顺序把他们组成一个最大的整数. 样例 给出 [1, 20, 23, 4, 8],返回组合最大的整数应为8423201. 思路:直接交换两个数,然后判断交换之后的 ...

  5. linux系统之-vi编辑器

    在linux系统使用中,掌握熟练的vi编辑器,可以提高linux工作效率.那么vi编辑器的使用方法有哪些呢? vi编辑器可在绝大部分linux发行版中使用. Vi编辑器的作用:创建或修改文件:维护li ...

  6. DevOps - 项目私库 - Nexus Repository

    相关链接 Sonatype官网:https://www.sonatype.com Products: Nexus Repository OSS2.x & 3.x Documentation:  ...

  7. Yaf学习(一)----Linux安装Yaf

    1.简介 Yaf,全称 Yet Another Framework,是一个高性能的PHP开发框架,采用PHP扩展实现(c语言).Blablablabla....... 2.环境 2.1 虚拟机 虚拟机 ...

  8. 以源码安装的lamp环境为依托,源码安装zabbix监控系统

    1.源码安装lamp环境 1)安装httpd, 以源码httpd-2.4.33为基础,解压后,执行./configure --prefix=/usr/local/ --sysconfdir=/etc/ ...

  9. UVA - 12230

    #include <bits/stdc++.h> using namespace std; int n; double d; double p,l,v,ret,sum; ; /* 村庄A, ...

  10. poj2230 欧拉回路

    http://poj.org/problem?id=2230 Description Bessie's been appointed the new watch-cow for the farm. E ...