POJ 2761 Feed the dogs (主席树)(K-th 值)
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 20634 | Accepted: 6494 |
Description
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
【分析】这题跟POJ2104很像,所以可以用划分树来做。这里只是为了练习主席树就用了主席树的板子,不过主席树还不是很懂。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e5+;
const int M=N*N+;
struct seg {
int lson,rson;
int cnt;
};
seg T[N*];
int root[N],tot;
vector<int>pos;
int arr[N];
int last_pos[N]; void init() {
pos.clear();
met(root,);met(last_pos,);
tot=;
T[].cnt=T[].lson=T[].rson=;
}
void update(int &cur,int ori,int l,int r,int pos,int flag) {
cur=++tot;
T[cur]=T[ori];
T[cur].cnt+=flag;
if(l==r)
return ;
int mid=(l+r)/;
if(pos<=mid)
update(T[cur].lson,T[ori].lson,l,mid,pos,flag);
else
update(T[cur].rson,T[ori].rson,mid+,r,pos,flag);
}
int query(int S,int E,int l,int r,int k) {
if(l==r)return l;
int mid=(l+r)/;
int sum=T[T[E].lson].cnt-T[T[S].lson].cnt;
if(sum>=k)return query(T[S].lson,T[E].lson,l,mid,k);
else query(T[S].rson,T[E].rson,mid+,r,k-sum);
}
int main(void) {
int n,m,i,l,r,k;
scanf("%d%d",&n,&m);
init();
for (i=; i<=n; ++i) {
scanf("%d",&arr[i]);
pos.push_back(arr[i]);
}
sort(pos.begin(),pos.end());
pos.erase(unique(pos.begin(),pos.end()),pos.end());
int temp_rt=;
for (i=; i<=n; ++i) {
arr[i]=lower_bound(pos.begin(),pos.end(),arr[i])-pos.begin()+;
update(root[i],root[i-],,n,arr[i],);
}
for (i=; i<m; ++i) {
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",pos[query(root[l-],root[r],,n,k)-]);
} return ;
}
POJ 2761 Feed the dogs (主席树)(K-th 值)的更多相关文章
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- 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$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- poj 2104 K-th Number(主席树,详细有用)
poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...
- 主席树 【权值线段树】 && 例题K-th Number POJ - 2104
一.主席树与权值线段树区别 主席树是由许多权值线段树构成,单独的权值线段树只能解决寻找整个区间第k大/小值问题(什么叫整个区间,比如你对区间[1,8]建立一颗对应权值线段树,那么你不能询问区间[2,5 ...
- POJ 题目2761 Feed the dogs(主席树||划分树)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 16860 Accepted: 5273 De ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- POJ 2104 K-th Number(主席树模板题)
http://poj.org/problem?id=2104 题意:求区间$[l,r]$的第k小. 思路:主席树不好理解啊,简单叙述一下吧. 主席树就是由多棵线段树组成的,对于数组$a[1,2...n ...
- POJ 2104 K-th Number(主席树——附讲解)
Description You are working for Macrohard company in data structures department. After failing your ...
随机推荐
- BigDecimal简单说
1) 浮点数的舍弃规则: 假设小数点后保留两位 RoundingMode.CEILING:向正无穷大的方向舍入: 1.245 → 1.25 -1.245 → -1.24 RoundingMode ...
- Java基础-6流程控制
一).选择控制: 选择控制分为两种:if...else...和switch 单分支结构:这是最简单的一种选择结构,它只是简单的判断某个条件是否成立,如果成立就执行一段代码,语句形式为: if(条件表达 ...
- [转] mysql分区性能初探
本文转自:http://www.cnblogs.com/acpp/archive/2010/08/09/1795464.html 一, 分区概念 分区允许根据指定的规则,跨文件系统分配单个 ...
- 软工实践Alpha冲刺(6/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ...
- iOS 实现设备应用之间的相互切换
今天突发奇想,想做个应用之间能够切换的demo. 前提条件是,你得知道对应应用程序的URL Scheme.URL Scheme不一定和bundle id相同,但是,一定要是一致的. [[UIAppli ...
- Comparable和Comparator的学习笔记
目录 Comparable和Comparator的实现 Comparable接口 Comparator接口 总结 参考自 今天在项目开发中,遇到要对List中的对象按照对象某一属性进行排序的问题,我发 ...
- 【JAVA进阶】——myEclipse连接mysql启动数据库服务
背景: DRP项目要求使用Oracle数据库,但目前由于种种原因,暂时还装不了Oracle.但也不能闲着啊,就拿mysql来试试.安装完mysql以后,使用myEclipse连接数据库,就一直报错,报 ...
- springmvc和struts2的区别(转)
1.客户端浏览器发出HTTP请求. 2.根据web.xml配置,该请求被FilterDispatcher接收. 3.根据struts.xml配置,找到需要调用的Action类和方法, 并通过I ...
- hdu 1195 Open the Lock (BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 《c程序设计语言》读书笔记-5.9-指针转换天数和日期
#include "stdio.h" #include "stdlib.h" #include "string.h" static char ...