[POJ2761] Feed the dogs (Treap)
题目链接:http://poj.org/problem?id=2761
题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个。这m个区间不会互相包含。
Treap,自己学着写了个板子,留贴备用。
离线操作,将区间移动之,删除旧的添加新的。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <cmath>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstdlib>
#include <functional>
#include <queue>
#include <stack>
#include <list>
#include <ctime>
using namespace std; const int inf = ~0U >> ; typedef long long LL; struct TreapNode {
int key, val, siz;
TreapNode* ch[];
TreapNode() :val(), siz(){ key = rand() - ; ch[] = ch[] = NULL; }
void rz(){
siz = ;
if (ch[]) siz += ch[]->siz;
if (ch[]) siz += ch[]->siz;
}
~TreapNode(){
if (ch[]) delete ch[];
if (ch[]) delete ch[];
}
}; void rot(TreapNode*& rt, bool d){
TreapNode* c = rt->ch[d];
rt->ch[d] = c->ch[!d];
c->ch[!d] = rt;
rt->rz(); c->rz();
rt = c;
} void Insert(TreapNode*& rt, int val) {
if (!rt){ rt = new TreapNode(); rt->val = val; return; }
//if (val == rt->val) return;
bool d = val > rt->val;
Insert(rt->ch[d], val);
if (rt->ch[d]->key < rt->key) rot(rt, d);
else rt->rz();
} void Delete(TreapNode*& rt, int val) {
if (rt == NULL) return;
if (rt->val == val) {
if (rt->ch[] == NULL && rt->ch[] == NULL) {
delete rt;
rt = NULL;
return;
}
else if (rt->ch[] == NULL) {
rot(rt, );
Delete(rt->ch[], val);
}
else if (rt->ch[] == NULL) {
rot(rt, );
Delete(rt->ch[], val);
}
else {
bool d = rt->ch[]->key > rt->ch[]->key;
rot(rt, d);
Delete(rt->ch[!d], val);
}
}
else {
bool d = val > rt->val;
Delete(rt->ch[d], val);
}
rt->rz();
} int Select(TreapNode* rt, int k) {
if (!rt) return -inf;
if (rt->siz < k) return -inf;
int r = ;
if (!rt->ch[]) r = ;
else r = rt->ch[]->siz;
if (r == k - ) return rt->val;
if (k - < r) return Select(rt->ch[], k);
return Select(rt->ch[], k - r - );
} int n, m;
int a[], ans[];
//int ptr = 0; struct bwl{
int l, r, k, idx;
bool operator<(const bwl& b) const{
return l < b.l;
}
}; bwl ee[]; int main() {
srand(time());
while (scanf("%d%d", &n, &m) != EOF) {
TreapNode *root = NULL;
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = ; i < m; i++) {
scanf("%d%d%d", &ee[i].l, &ee[i].r, &ee[i].k);
ee[i].idx = i;
}
sort(ee, ee + m);
int pre = ,last = ;
for (int i = ; i < m; i++) {
if (i == ) {
pre = last = ee[i].l;
}
while (pre <= last && pre < ee[i].l) {
Delete(root, a[pre-]);
pre++;
}
while (last <= n && last <= ee[i].r) {
Insert(root, a[last-]);
last++;
}
ans[ee[i].idx] = Select(root, ee[i].k);
}
for (int i = ; i < m; i++) {
printf("%d\n", ans[i]);
}
delete root;
}
return ;
} /*
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1 9 3
1 3 10 2 4 7 7 8 2
1 3 3
5 9 2
3 8 4
*/
[POJ2761] Feed the dogs (Treap)的更多相关文章
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- [POJ2761]Feed the dogs
Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...
- 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs
先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...
- [Poj2761]Feed the dogs(主席树)
Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...
- 【POJ2761】【区间第k大】Feed the dogs(吐槽)
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(平衡树or划分树or主席树)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
- 划分树---Feed the dogs
POJ 2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...
- poj2761 feed the dog
题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...
- POJ 2761 Feed the dogs (主席树)(K-th 值)
Feed the dogs Time Limit: 6000MS Memor ...
随机推荐
- jenkins 中 Poll SCM 和 Build periodically 的区别
Build periodically 定时触发构建任务,不管远程代码分支上的代码是否发生变化,都执行一次构建. 示例:H 2 * * * 每天两点定时执行构建. Poll SCM:定时感知代码分支是否 ...
- dede currentstyle属性完美解决方案
问题一.dede让channelartlist标签支持currentstyle属性 完美解决 打开include\taglib\channelartlist.lib.php找到$pv->Fiel ...
- 自适应布局,响应式布局以及rem,em区别
一.自适应和响应式 先说共同点: 两者都是因为越来越多的 移动设备( mobile, tablet device )加入到互联网中来而出现的为移动设备提供更好的体验的技术.用技术来使网页适应从小到大( ...
- SAX解析DOM4J的方法总结
<二 .利用SAX进行DOM解析DOM4J的基本练习> 一. 本次总结用到的xml文档 <?xml version="1.0" encoding=&q ...
- 《Android深度探索HAL与驱动开发》第一章阅读心得
首先了解到Android系统架构是由四层构成:其中第一层是Linux内核,他的作用是负责Linux的驱动程序以及内存.进程.电源等管理操作:第二层是C/C++代码库,也就是Linux下.so的文件:第 ...
- HTML DOM 节点
一切皆节点 在 HTML DOM (文档对象模型)中,节点主要包括(括号中用数字表示节点类型):元素(1).属性(2).文本(3,其中换行符也是一个文本节点).注释(8).文档(9). 其中重要的方法 ...
- Sqlserver 函数(例子)
--SQL SERVER 2008 函数大全/*author:TracyLeecsdncount:Travylee*/ /*一.字符串函数:1.ascii(字符串表达式) 返回字符串中最右侧字符的 ...
- 【OPENGL】第三篇 着色器基础(二)
在这一小节,主要学习GLSL的基本数据类型以及控制结构.GLSL具备了C++和Java的很多特性,我们会先了解所有着色阶段共有的特性,再了解各个着色器的专属特性. 1.着色器的基本结构 一个着色器程序 ...
- StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks 论文笔记
StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks 本文将利 ...
- Java命名规范基础
一.java命名规范 1.类和接口:由多个单词组成时,所有单词的首字母大写,如TestJava 2.变量名和方法(函数):由多个单词组成时,所有第一个单词的首字母小写,之后每一个单词的首字母大写,如: ...