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. popupwindow 与 输入法

    有时候popupwindow会被输入法覆盖, 有时候popupwindow会被输入法给顶上去. 而且这个问题还跟theme的windowFullscreen属性相关. 不过这些可以都不用管, 根据项目 ...

  2. 旧文—冬日感怀

    冬日感怀   下雪了,虽不大,可那雪花落在手上,融在心里,便泛起淡淡的温柔,因为这是冬日的馈赠啊,是上苍的礼物,是往昔记忆的灰烬化作天地间的精灵装点纷繁琐碎的世界.于是,透过汽车上雾气氤氲的玻璃窗,人 ...

  3. Castle.ActiveRecord (V3.0.0.130)

    为项目添加 Castle.ActiveRecord 的引用: 安装成功后,查看项目的引用如图: 配置文件 App.Config (MySQL) <?xml version="1.0&q ...

  4. dbvis MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT' at line 1

    转自:http://www.cnblogs.com/_popc/p/4053593.html 今天使用数据库查询工具DBvis链接mysql数据库时, 发现执行如何sql语句, 都报如下错误: 后来想 ...

  5. Android最佳实践之UI篇

    http://sr1.me/way-to-explore/2015/03/25/best-practice-for-android-ui.html

  6. itextpdf JAVA 输出PDF文档

    使用JAVA生成PDF的时候,还是有些注意事项需要处理的. 第一.中文问题,默认的itext是不支持中文的,想要支持,需要做些处理. 1.直接引用操作系统的中文字体库支持,由于此方案限制性强,又绑定了 ...

  7. 250W电源带i7+GTX1080?

    电源的科学: Q1:电源的额定功率是什么?峰值功率又是什么?A1:电源的额定功率就是电源正常工作时的功率,它的值为用电器的额定电压乘以额定电流.而峰值功率指的是电源短时间内能达到的最大功率, 一般情况 ...

  8. Log4net介绍

    一.Log4net介绍 log4net是一个功能著名的开源日志记录组件.利用log4net可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括MS Server,Access, ...

  9. 【转载】linux tail命令的使用方法详解

    本文介绍Linux下tail命令的使用方法.linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新 ...

  10. Android ActionBar Home按钮返回事件处理的两种方式

    今早无聊查看了一下android官方文档,最近对ActionBar很感兴趣,它确实对我们的日常开发起到了很便捷的作用. 对于通过点击ActionBar的Home按钮返回,以前我只知道有一种方式:也就是 ...