题目链接: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)的更多相关文章

  1. poj 2761 Feed the dogs (treap树)

    /************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...

  2. [POJ2761]Feed the dogs

    Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...

  3. 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs

    先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...

  4. [Poj2761]Feed the dogs(主席树)

    Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...

  5. 【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 ...

  6. 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 ...

  7. 划分树---Feed the dogs

    POJ  2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...

  8. poj2761 feed the dog

    题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...

  9. POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                                Feed the dogs Time Limit: 6000MS   Memor ...

随机推荐

  1. linux 查找文件与进程常用命令

    Linux的五个查找命令 1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> ...

  2. Windows Server 2012 没有远程桌面授权服务器可以提供许可证,远程会话被中断

    今天在登录公司内部的服务器的时候,无法进行远程访问. 弹出错误信息:没有远程桌面授权服务器可以提供许可证,远程会话被中断 经过网上的寻找,原来是server 2012 远程登录只提供120天的使用期限 ...

  3. iOS RSA加密解密及签名验证

    1.首先要下载openssl,这个不用说,直接官网下载或者用brew install openssl下载 2.终端生成私钥密钥 2.1生成私钥 openssl genrsa - 2.2生成密钥 ope ...

  4. python正则表达式

    group 组是通过 "(" 和 ")" 元字符来标识的. "(" 和 ")" 有很多在数学表达式中相同的意思:它们一起 ...

  5. 用.NET从外部dwg文件导入块

    翻译并引自Kean's blog的两篇文章: http://through-the-interface.typepad.com/through_the_interface/2006/08/import ...

  6. Flex 关闭浏览器

    在Actionscript 2及以前,要打开任何网址,只需调用全局函数getURL()即可.在Actionscript 3中,已经取消了getURL()这个全局函数,取而代之的是flash.net包中 ...

  7. IOS懒加载

    1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...

  8. equals与==的区别

    equals与==的区别. 下面是equals的源码 public boolean equals(Object anObject) { if (this == anObject) { //如果两个对象 ...

  9. UVA 10815

    题意:输入文本,把不同的单词按照字典序排列. /* tolower 功 能: 把字符转换成小写字母,非字母字符不做出处理 头文件 目前在头文件iostream中也可以使用 isalpha 一种函数:判 ...

  10. EF6 连接Oracle 迁移数据错误解决方法

    环境:vs2015 + EF6 +ODP 数据库Oracle 11G add-migratioin 正常,但在update-database 时报如下错误: System.Runtime.Serial ...