题目链接: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. Atom 如何隐藏 .Ds_Store 文件

    明明在core里设置了忽略文件列表,但是在右侧的文件列表中还是看到了,相当烦人.然后我查了下资料,原来还需要核心插件TreeView再设置一次遵循全局忽略文件才生效. 版本:1.12.6  (其它版本 ...

  2. 【视频演示】FireBase链接已有应用&实时数据库的配置与使用

    全程无编辑,一镜到底,一帧未减,带上犯傻的时间全部不到31分钟:Firebase链接与基础功能使用如此之简单,跟我一起动手,从零开始链接Firebase吧~注意: 输入声源为笔记本自带麦克风,所以风扇 ...

  3. 系统弹性概念[TODO]

    系统弹性 Shopify构建分布式可扩展应用的最佳实践 [编者的话]在构建大型分布式系统应用时,如何降低不同部分之间的依赖,增强系统的弹性,电商解决方案提供商 Shopify 给出了解决方法. 弹性矩 ...

  4. iOS中的extern与static

    1.extern #import <Foundation/Foundation.h> extern NSString *DBDefaultName; @interface DataBase ...

  5. 各种模板(part 2)

    堆: using namespace dui //堆 { #include<queue> //需要的库 priority_queue < int > Q; //定义一个Q的大根 ...

  6. Linear Algebra lecture7 note

    Computing the nullspace (Ax=0) Pivot variables-free variables Special solutions: rref( A)=R   rank o ...

  7. BLOCK封装带菊花的网络请求

    #import <Foundation/Foundation.h> @class HttpRequestManager; typedef void(^httpRequestBlock) ( ...

  8. C# 导入Excel到DataSet中

    class Import { /// <summary> /// 导入Excel到DataSet中 /// </summary> /// <param name=&quo ...

  9. Spring源码学习之:FactoryBean的使用

    转载:http://book.51cto.com/art/201311/419081.htm ==========个人理解========================= FactoryBean和B ...

  10. 定义信息源的一些示例(xml文件)

    本文目录 1.订阅博客,简单一例 2.从网页获取信息,简单一例 3.充分使用callback回调代码 4.html_re中包含多个block 5.使用html_json这个worker,解析json数 ...