划分树---Feed the dogs
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.
Input
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
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的更多相关文章
- 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 ...
- POJ 题目2761 Feed the dogs(主席树||划分树)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 16860 Accepted: 5273 De ...
- POJ 2761 Feed the dogs (主席树)(K-th 值)
Feed the dogs Time Limit: 6000MS Memor ...
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- [划分树] POJ 2104 K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51732 Accepted: 17722 Ca ...
- poj 2104 K-th Number (划分树入门 或者 主席树入门)
题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...
- hdu2665 && poj2104划分树
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 47066 Accepted: 15743 Ca ...
- poj 2104:K-th Number(划分树,经典题)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 35653 Accepted: 11382 Ca ...
- sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)
Boring Counting Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 In this problem you a ...
随机推荐
- 可编辑的DIV -编辑器
找了好多,没几个好用的,都或多或少有问题 目前这个最好用.. 不过有一个奇葩的问题,就是要放在"<a></a>"标签里面, js或者jQuery获取 $ ...
- 编写并发程序 Inversion
做完了 scala parallel 课程作业后,觉得 scala 写并发程序的便捷性是 java 永远都追不上的.scala 的Future 和 Promise,java 里 Future 和 Co ...
- 电商大促准备流程v2
1 概述 对于电商企业而言,每年都会有几次大的促销活动,像双十一.店庆等,对于第一次参加这个活动的新手,难免会有些没有头绪,因而将自己参加双十一.双十二活动中的过程心得进行下总结,一方面供以后工作中继 ...
- Linux下添加硬盘,分区,格式化详解
2005-10-17 在我们添加硬盘前,首先要了解linux系统下对硬盘和分区的命名方法. 在Linux下对IDE的设备是以hd命名的,第一个ide设备是hda,第二个是hdb.依此类推 我们一般主板 ...
- TeamViewer连接Windows8.1系统黑屏解决方案
TeamViewer用win7连接win8.1 都是64位系统,总是黑屏,可以看到鼠标也联动了,聊天传输文件都没有问题,反向用win8.1连接win7也没问题,而且TeamViewer更新到最新版本了 ...
- 【Android学习】XML文本的三种解析方式(通过搭建本地的Web项目提供XML文件)
XML为一种可扩展的标记语言,是一种简单的数据存储语言,使用一系列简单的标记来描述. 一.SAX解析 即Simple API for XML,以事件的形式通知程序,对Xml进行解析. 1.首先在Web ...
- JavaScript 32位整型无符号操作
在 JavaScript 中,所有整数字变量默认都是有符号整数,这意味着什么呢? 有符号整数使用 31 位表示整数的数值,用第 32 位表示整数的符号,0 表示正数,1 表示负数. 数值范围从 -2^ ...
- Android代码优化工具——Android lint
作为移动应用开发者,我们总希望发布的apk文件越小越好,不希望资源文件没有用到的图片资源也被打包进apk,不希望应用中使用了高于minSdk的api,也不希望AndroidManifest文件存在异常 ...
- MVVMlight框架应用:Data Binding、Command
常用Wpf开发中我们在ViewModel中实现INotifyPropertyChanged接口,通过触发PropertyChanged事件达到通知UI更改的目的:在MVVMLight框架里,这里我们定 ...
- Hadoop入门进阶课程8--Hive介绍和安装部署
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...