POJ 2761 Feed the dogs(平衡树or划分树or主席树)
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
题目大意:给n个数,m次询问区间[l, r]的第k小数。
思路:对询问排序,再离线处理每一个询问(加减点直至平衡树对应区间等于询问区间),不过这样做复杂度是没有保障的……因此差不多的题目如POJ2104妥妥地TLE了……
关于正解之划分树和主席树这里不写。
关于划分树和主席树:
POJ 2104 K-th Number(划分树)
POJ 2104 K-th Number(不带修改主席树——附讲解)
代码(3438MS)(treap树):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ; int key[MAXN], weight[MAXN], child[MAXN][], size[MAXN];
int stk[MAXN], top, cnt; inline void init() {
top = cnt = ;
} inline int new_node(int k) {
int x = (top ? stk[top--] : ++cnt);
key[x] = k;
size[x] = ;
weight[x] = rand();
child[x][] = child[x][] = ;
return x;
} inline void update(int &x) {
size[x] = size[child[x][]] + size[child[x][]] + ;
} void rotate(int &x, int t) {
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
update(x); update(y);
x = y;
} void insert(int &x, int k) {
if(x == ) x = new_node(k);
else {
int t = key[x] < k;
insert(child[x][t], k);
if(weight[child[x][t]] < weight[x]) rotate(x, t);
}
update(x);
} void remove(int &x, int k) {
if(key[x] == k) {
if(child[x][] && child[x][]) {
int t = weight[child[x][]] < weight[child[x][]];
rotate(x, t); remove(child[x][t ^ ], k);
}
else {
stk[++top] = x;
x = child[x][] + child[x][];
}
}
else remove(child[x][key[x] < k], k);
if(x > ) update(x);
} int kth(int &x, int k) {
if(x == || k <= || k > size[x]) return ;
int s = ;
if(child[x][]) s = size[child[x][]];
if(k == s + ) return key[x];
if(k <= s) return kth(child[x][], k);
return kth(child[x][], k - s - );
} struct Node {
int l, r, k, id;
void read(int i) {
id = i;
scanf("%d%d%d", &l, &r, &k);
}
bool operator < (const Node &rhs) const {
if(r != rhs.r) return r < rhs.r;
return l < rhs.l;
}
}; Node query[MAXN];
int a[MAXN], ans[MAXN]; int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i < m; ++i) query[i].read(i);
sort(query, query + m);
int left = , right = , root = ;
for(int i = ; i < m; ++i) {
while(right < query[i].r)
if(!root) root = new_node(a[++right]);
else insert(root, a[++right]);
while(left > query[i].l)
if(!root) root = new_node(a[--left]);
else insert(root, a[--left]);
while(left < query[i].l) remove(root, a[left++]);
ans[query[i].id] = kth(root, query[i].k);
}
for(int i = ; i < m; ++i) printf("%d\n", ans[i]);
}
POJ 2761 Feed the dogs(平衡树or划分树or主席树)的更多相关文章
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- POJ 2761 Feed the dogs (主席树)(K-th 值)
Feed the dogs Time Limit: 6000MS Memor ...
- POJ 2761 Feed the dogs
主席树,区间第$k$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- poj 2104 K-th Number 划分树,主席树讲解
K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...
- 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665
如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...
- POJ 题目2761 Feed the dogs(主席树||划分树)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 16860 Accepted: 5273 De ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- POJ 2104 K-th Number(分桶,线段树,主席树)
一道比较经典的数据结构题.可以用多种方式来做. 一,分桶法(平方分解). 根据数字x的大小和区间内不大于x的数字数量cnt的单调性,可知第k大数kth对应的cnt应该满足cnt≥k, 且kth是满足条 ...
随机推荐
- Openresty最佳案例 | 第1篇:Nginx介绍
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616591 本文出自方志朋的博客 Nginx 简介 Nginx是一个高性能的Web 服务 ...
- JavaScript 基础(一)
基本语法: 区分大小写: ECMAScript 中的一切(变量,函数名和操作符)都区分大小写. 标识符: 表示符就是指,变量,函数,属性名字,或者函数的参数. 1.第一个字符必须是一个字母,下划线(_ ...
- Web前端几种常见的实现水平垂直居中的方法
第一种: 父容器不设置宽度,用定位实现水平垂直居中. <!DOCTYPE html> <html lang="en"> <head> <m ...
- BZOJ1030: [JSOI2007]文本生成器(AC自动机)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5984 Solved: 2523[Submit][Status][Discuss] Descripti ...
- ABAP术语-BAPI Explorer
BAPI Explorer 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/24/1012110.html Tool for developi ...
- Windows登录密码明文获取器
软件原理:本软件根据开源工具mimikatz2.0 修改!软件能直接读取系统明文密码! 支持32位.64位系统 win xp/vista/7/8/8.1 本机win10专业版测试不能获取,虚拟机win ...
- php-7.2.3源代码和php-5.6.26源代码摘录,对比 “汇编php文件”和“执行opcode代码”
php-7.2.3 在“汇编php文件”和“执行opcode代码”上做了大量改变php-5.6.26 没见到支持抽象语法树的相关代码,php-7.2.3 见到支持抽象语法树的相关代码php-5.6.2 ...
- VSCode插件整理
VSCode插件整理 VSCode插件整理 官网地址 vscode常用配置(User Settings文件) 基本插件 前端插件 VUE部分 python MarkDown部分 连接Linux 本地与 ...
- Overview of the High Efficiency Video Coding (HEVC) Standard阅读笔记
1.INTRODUCTION High Efficiency Video Coding(HEVC) <-> H.265 MPEG-4 Advanced Video Coding(AVC) ...
- python2.7入门---file(文件)&OS 文件&目录方法
首先file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.f ...