POJ 题目2761 Feed the dogs(主席树||划分树)
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 16860 | Accepted: 5273 |
Description
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 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
Sample Input
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1
Sample Output
3
2
Source
POJ Monthly--2006.02.26,zgl & twb
求区间第k大值
ac代码
主席树版本号
Problem: 2761 User: kxh1995
Memory: 24508K Time: 2813MS
Language: C++ Result: Accepted
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[100010],t[100010];
int T[100010*30],lson[100010*30],rson[100010*30],c[100010*30];
int n,m,cnt,tot;
void init_hash()
{
int i;
for(i=1;i<=n;i++)
t[i]=a[i];
sort(t+1,t+1+n);
cnt=unique(t+1,t+1+n)-t-1;
}
int build(int l,int r)
{
int root=tot++;
c[root]=0;
if(l!=r)
{
int mid=(l+r)>>1;
lson[root]=build(l,mid);
rson[root]=build(mid+1,r);
}
return root;
}
int hash(int x)
{
return lower_bound(t+1,t+1+cnt,x)-t;
}
int update(int root,int pos,int val)
{
int newroot=tot++;
int temp=newroot;
c[newroot]=c[root]+val;
int l=1,r=cnt;
while(l<r)
{
int mid=(l+r)>>1;
if(pos<=mid)
{
lson[newroot]=tot++;
rson[newroot]=rson[root];
newroot=lson[newroot];
root=lson[root];
r=mid;
}
else
{
rson[newroot]=tot++;
lson[newroot]=lson[root];
newroot=rson[newroot];
root=rson[root];
l=mid+1;
}
c[newroot]=c[root]+val;
}
return temp;
}
int query(int l_root,int r_root,int k)
{
int l=1,r=cnt;
while(l<r)
{
int mid=(l+r)>>1;
if(c[lson[l_root]]-c[lson[r_root]]>=k)
{
r=mid;
l_root=lson[l_root];
r_root=lson[r_root];
}
else
{
l=mid+1;
k-=c[lson[l_root]]-c[lson[r_root]];
l_root=rson[l_root];
r_root=rson[r_root];
}
}
return l;
}
int main()
{
//int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
tot=0;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
init_hash();
T[n+1]=build(1,cnt);
for(i=n;i>0;i--)
{
int pos=hash(a[i]);
T[i]=update(T[i+1],pos,1);
}
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",t[query(T[l],T[r+1],k)]);
}
}
}
划分树版本号
Problem: 2761 User: kxh1995
Memory: 18988K Time: 2000MS
Language: C++ Result: Accepted
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int tree[30][100100],sorted[100010],toleft[30][100010];
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
void build(int l,int r,int dep)
{
if(l==r)
{
return;
}
int mid=(l+r)>>1;
int same=mid-l+1;
int i;
for(i=l;i<=r;i++)
if(tree[dep][i]<sorted[mid])
same--;
int lpos=l;
int rpos=mid+1;
for(i=l;i<=r;i++)
{
if(tree[dep][i]<sorted[mid])
{
tree[dep+1][lpos++]=tree[dep][i];
}
else
if(tree[dep][i]==sorted[mid]&&same>0)
{
tree[dep+1][lpos++]=tree[dep][i];
same--;
}
else
tree[dep+1][rpos++]=tree[dep][i];
toleft[dep][i]=toleft[dep][l-1]+lpos-l;
}
build(l,mid,dep+1);
build(mid+1,r,dep+1);
}
int query(int L,int R,int l,int r,int dep,int k)
{
if(l==r)
{
return tree[dep][l];
}
int mid=(L+R)>>1;
int cnt=toleft[dep][r]-toleft[dep][l-1];
if(cnt>=k)
{
int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
int newr=newl+cnt-1;
return query(L,mid,newl,newr,dep+1,k);
}
else
{
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+1,R,newl,newr,dep+1,k-cnt);
}
}
int main()
{
int n,m;
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(tree,0,sizeof(tree));
for(i=1;i<=n;i++)
{
scanf("%d",&tree[0][i]);
sorted[i]=tree[0][i];
}
qsort(sorted+1,n,sizeof(sorted[1]),cmp);
build(1,n,0);
while(m--)
{
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
int ans=query(1,n,a,b,0,k);
printf("%d\n",ans);
}
}
}
POJ 题目2761 Feed the dogs(主席树||划分树)的更多相关文章
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665
如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...
- 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 (主席树)(K-th 值)
Feed the dogs Time Limit: 6000MS Memor ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- hdu3473 线段树 划分树
//Accepted 28904 KB 781 ms //划分树 //所求x即为l,r区间排序后的中位数t //然后求出小于t的数的和sum1,这个可以用划分树做 //求出整个区间的和sum,可以用O ...
- poj2104 线段树 划分树
学习:http://www.cnblogs.com/pony1993/archive/2012/07/17/2594544.html 划分树的build: 划分树是分层构建的,在构建的t层时,我们可以 ...
- POJ 2761 Feed the dogs
主席树,区间第$k$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- 划分树---Feed the dogs
POJ 2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...
随机推荐
- Verification Mind Games---how to think like a verifier像验证工程师一样思考
1. 有效的验证需要验证工程师使用不同于设计者的思维方式思考问题.具体来说,验证更加关心在严格遵循协议的基础上发现设计里面的bug,搜索corner cases,对设计的不一致要保持零容忍的态度. m ...
- 如何利用CSS中的ime-mode用来控制页面上文本框中的全角/半角输入
css 之 ime-mode语法:ime-mode : auto | active | inactive | disabled取值:auto : 默认值.不影响ime的状态.与不指定 ime-mode ...
- 用 webpack 实现持久化缓存
什么是持久化缓存? 原文链接https://sebastianblade.com/using-webpack-to-achieve-long-term-cache/ 缓存(cache)一直是前端性能优 ...
- CAD把当前图上数据保存为一个二进流对象(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::WriteBinStreamEx 把当前图上数据保存为一个二进流对象,详细说明如下: 参数 说明 LPCTSTR pszPasswor ...
- dom4j使用方法详解
本文先做知识点的简单介绍,最后附完整案例. 一.解析XML文件 public class Foo { //url为XML文档地址 //自己封装了一个工具类 返回解析完成的document public ...
- 【转载】appium自动化环境搭建
1.java开发环境JDK 2.android SDK(platform/platform tools/tools/build tools) 3.python下载安装(pip) 4.appium下载安 ...
- 07Html、CSS
07Html.CSS-2018/07/17 1.HTML是用来描述网页的一种标记语言,是一套标记标签.HTML用使用标记标签来描述网页.超文本 标记语言. 2.格式 <html> < ...
- python爬虫28 | 你爬下的数据不分析一波可就亏了啊,使用python进行数据可视化
通过这段时间 小帅b教你从抓包开始 到数据爬取 到数据解析 再到数据存储 相信你已经能抓取大部分你想爬取的网站数据了 恭喜恭喜 但是 数据抓取下来 要好好分析一波 最好的方式就是把数据进行可视化 这样 ...
- Python面向对象之面向对象基本概念
面向过程和面向对象概念 过程和函数:过程类似于函数,只能执行,但是没有返回结果:函数不仅能执行,还能返回结果. 面向过程和面向对象 基本概念 面向过程-怎么做 把完成某一个需求的所有步骤从头到尾逐步实 ...
- 05 Python运算符
Python运算符: 此图来源于菜鸟教程,更详细参考 http://www.runoob.com/python3/python3-basic-operators.html 说明: 同一优先级的通常从左 ...