POJ  2761

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 题意:给了一个数列,求某个区间的第k大数。 思路:使用划分树算法,方便多次查询指定区间的第k大数。 代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=;
int sor[maxn]; struct node
{
int num[maxn];
int cnt[maxn];
}tree[]; void buildtree(int l, int r, int d)
{
if (l == r)
{
return;
}
int mid = (l+r)>>;
int oplift = l, opright = mid+; ///对左右子树的操作位置的初始化
int same_as_mid = ;
///用来计算在mid左边有多少个和sor[mid]相同的数(包括mid),这些都要放到左子树
for (int i = mid; i > ; i--)
{
if (sor[i] == sor[mid])
same_as_mid++;
else
break;
}
int cnt_lift = ;
for (int i = l; i <= r; i++)
{
if (tree[d].num[i] < sor[mid])
{
tree[d+].num[oplift++] = tree[d].num[i];
cnt_lift++;
tree[d].cnt[i] = cnt_lift;
}
else if(tree[d].num[i] == sor[mid] && same_as_mid)
{
tree[d+].num[oplift++] = tree[d].num[i];
cnt_lift++;
tree[d].cnt[i] = cnt_lift;
same_as_mid--;
}
else
{
tree[d].cnt[i] = cnt_lift;
tree[d+].num[opright++] = tree[d].num[i];
}
}
buildtree(l, mid, d+);
buildtree(mid+, r, d+);
} int query(int l, int r, int d, int ql, int qr, int k)
{
if (l == r)
return tree[d].num[l];
int mid = (l+r)>>;
int sum_in_lift, lift;
if (ql == l)
{
sum_in_lift = tree[d].cnt[qr];
lift = ;
}
else
{
sum_in_lift = tree[d].cnt[qr] - tree[d].cnt[ql-];
/// 区间进入左子树的总和
lift = tree[d].cnt[ql-];
}
if (sum_in_lift >= k)
///证明要找的点在左子树
{
int new_ql = l+lift;
int new_qr = l+lift+sum_in_lift-;
return query(l, mid, d+, new_ql, new_qr, k);
///这里有必要解释一下,我们要确定下一步询问的位置,如果在ql的左边有i个进入左子树,
///那么ql到qr中第一个进入左子树的必定在l+i的位置
}
else
{
int a = ql - l - lift;
int b = qr - ql + - sum_in_lift;
int new_ql = mid + a + ;
int new_qr = mid + a + b;
return query(mid+, r, d+, new_ql, new_qr, k - sum_in_lift);
}
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",&tree[].num[i]);
sor[i]=tree[].num[i];
}
sort(sor+,sor+n+);
buildtree(,n,);
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int ans=query(,n,,l,r,k);
printf("%d\n",ans);
}
}
}

划分树---Feed the dogs的更多相关文章

  1. POJ 2761 Feed the dogs(平衡树or划分树or主席树)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

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

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

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

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

  4. poj 2761 Feed the dogs (treap树)

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

  5. [划分树] POJ 2104 K-th Number

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

  6. poj 2104 K-th Number (划分树入门 或者 主席树入门)

    题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...

  7. hdu2665 && poj2104划分树

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

  8. poj 2104:K-th Number(划分树,经典题)

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

  9. sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

    Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you a ...

随机推荐

  1. DB2解除锁表

    背景 生产环境中,我几乎没有遇到过锁表.多是在开发过程中遇到的,比如团队开发中经常会遇到多个功能访问同一张表的情况.如果有开发人员在这张表加了排它锁,然后又忘记提交事务,那么其他开发人员就要一直等待了 ...

  2. java&postgresql时区总结

    介绍这篇文章之前,首先回答一个问题,以前都没有时区的概念,程序也写的好好的,为什么要计算时区哪?举个例子,比如有一个订单的时间是:2015-07-04 11:28:19,那么咋一看没什么问题,可是如果 ...

  3. Python--类-例子

    class Base: def __init__(self): self.data = [] def add(self, x): self.data.append(x) def addtwice(se ...

  4. 获得 LayoutInflater 实例的三种方式

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  5. Linq专题之var关键字

    在c#1.0,c#2.0中声明一个变量时,总是要指定变量的类型,如果不指定变量类型编译器就会报错,产生编译错误.在c#3.0中我们可以不指定变量的具体类型,而使用一个新的关键字"var&qu ...

  6. RESTful 良好的API设计风格

    1.使用名词而不是动词 Resource资源 GET读 POST创建 PUT修改 DELETE /cars 返回 cars集合 创建新的资源 批量更新cars 删除所有cars /cars/711 返 ...

  7. JS中的constructor与prototype

    http://www.cnblogs.com/qiantuwuliang/archive/2011/01/08/1930548.html 在学习JS的面向对象过程中,一直对constructor与pr ...

  8. LINQ to SQL语句非常详细(原文来自于网络)

    LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...

  9. 《Programming with Objective-C》第三章 Working with Objects

    Object和普通变量的区别 If you’re used to using terms like the stack and the heap, a local variable is alloca ...

  10. WebStorm 8 注册码

    UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA INauvJkeVJB ...