[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 ...
随机推荐
- .NET涉及的一些名词
本文在最为概略的层次上对.NET涉及的一些名词进行解释, 包括: 通用语言基础架构(Common Language Infrastructure, CLI). 虚拟执行系统(Virtual Execu ...
- Ubuntu vi 常用命令集合
:w 保存文件但不退出vi:w file 将修改另外保存到file中,不退出vi:w! 强制保存,不推出vi:wq 保存文件并退出vi:wq! 强制保存文件,并退出viq: 不保存文件,退出vi:q! ...
- C# 之泛型详解
转自牛人博客:http://www.blogjava.net/Jack2007/archive/2008/05/05/198566.html 鄙人才疏学浅,经常遇到泛型,一知半解,用的时候也是模糊不清 ...
- HDU 3342 Legal or Not(判断是否存在环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...
- centos 6.8 安装 nginx-1.11.4
yum -y install gcc-c++ wget http://nginx.org/download/nginx-1.11.4.tar.gz wget https://www.openssl. ...
- .stop()
一. 在使用animate()的时候 前面需要加上.stop()来防止移进移出的山东问题. 二. 1.定义: stop() 方法为被选元素停止当前正在运行的动画. 2.语法: $(selec ...
- 数论 UVA 10791
这道题目是关于满足同意最小公倍数的所有数对中两数之和的最小值. 题目大意是给你一个数n,要求你求出在所有以n为最小公倍数的数对中两数之和的最小值. 方法:将n进行质因数分解,再将所有分解出的质因子加起 ...
- HTML5获取地理位置
包含了以下功能:(1)通过IP地址获取城市地址(并不完全准确,存在代理IP或IP中转时定位与实际位置不一致的情况)(2)通过移动端浏览器及GPS定位位置坐标(3)根据位置坐标转换百度地图坐标(4)根据 ...
- 【转载】javascript与C#的语法区别
由于博主不允许的情况下不允许转载,我在这里只放上链接 http://blog.csdn.net/ranlianjie/article/details/2484139
- canvas 画字
用canvas画字还是头一回,要想和UI设计的画的一模一样还是真有些苦难,不过现在实现的效果已经很像了. <!--通过字体文件引入字体--><style>@font-face ...