[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 ...
随机推荐
- SPI机制
Service Provider Interface 是java的服务提供的发现机制,很多框架中都有用到. 使用这个机制需要做以下几步: 1,在calsspath下见一个目录:META-INF\ser ...
- [读书笔记]java中的volatile关键词
以下内容大多来自周志明的<深入理解Java虚拟机>. 当一个变量被volatile修饰后,它将具备两种特性: 1. 保证此变量对所有线程的可见性,这里的“可见性”是指当一条线程修改了这个变 ...
- 【Cocos2d-x 3.x】 动作类Action源码分析
游戏设计中,动作是不可缺少的,Cocos2d-x中所有的动作都继承自Action类,而Action类继承自Ref和Clonable类,整个动作类继承体系如图: FiniteTimeAction是所有瞬 ...
- java程序员烂大街为何还不便宜?
最近跟一朋友聊天,他是做c#开发的.他答应了老板带领一帮java工程师开发网站.披星戴月终于搞定,现在已经盈利.但是他公司的那帮搞c#的同事不淡定了. 在招聘java程序员的时候2年有开15k的.5年 ...
- [转]linux,windows 可执行文件(ELF、PE)
ELF (Executable Linkable Format)UNIX类操作系统中普遍采用的目标文件格式 . 首先要知道它有什么作用:工具接口标准委员会TIS已经将ELF作为运行在Intel32位架 ...
- 帝国时代II 高清版 steam 4.4 字体替换 微软雅黑
其实默认的中文字体算是中规中矩吧,但是我并不喜欢 从昨天开始就想着换 于是我就开始搜索帝国时代2的游戏目录的资源,马上就锁定到了\Steam\steamapps\common\Age2HD\resou ...
- https/相对路径,绝对路径
1. htttps HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全 ...
- 前端学习 第七弹: Javascript实现图片的延迟加载
前端学习 第七弹: Javascript实现图片的延迟加载 为了实现图片进入视野范围才开始加载首先: <img src="" x-src="/acsascas ...
- hdu1087 dp
题意:给定一串数字,要求选取一个严格递增的子序列,使序列和最大. dp[i] 表示以 i 为结尾的子序列的最大和,dp[i] = max{dp[j]+a[i]}(j 从 0 到 i-1),dp[0]是 ...
- linux下安装安装pcre-8.32
linux下安装安装pcre-8.32 ./configure --prefix=/usr/local/pcre 出现以下错误 configure: error: You need a C++ com ...