题意 就是求区间第k大,区间 不互相包含。

尝试用treap解决一下 第k大的问题。

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e5+;
struct Node
{
Node *ch[];
int key, siz, r;
Node (int val)
{
ch[] = ch[] = NULL;
siz = ;
key = val;
r = rand();
}
bool operator < (const Node &rhs)const
{
return r < rhs.r;
}
int cmp(int x)const
{
if (key == x)
return -;
return key > x ? : ;
}
void maintain()
{
siz = ;
if (ch[] != NULL)
siz += ch[] -> siz;
if (ch[] != NULL)
siz += ch[] -> siz;
}
};
void rotate(Node* &root, int d)
{
Node *tmp = root -> ch[d^];
root -> ch[d^] = tmp -> ch[d];
tmp -> ch[d] = root;
root -> maintain();
tmp -> maintain();
root = tmp;
}
void insert (Node* &root, int x)
{
if (root == NULL)
root = new Node (x);
else
{
int d = x < root -> key ? : ;
insert(root -> ch[d], x);
if (root -> ch[d] -> r > root -> r)
rotate(root, d^);
}
root -> maintain();
}
void dele(Node* &root, int x)
{
int d = root -> cmp(x);
if (d == -)
{
Node* tmp = root;
if (root -> ch[] != NULL && root -> ch[] != NULL)
{
int d1 = (root -> ch[] -> r > root -> ch[] -> r ? : );
rotate(root, d1);
dele(root -> ch[d1], x);
}
else
{
if (root -> ch[] == NULL)
root = root -> ch[];
else
root = root -> ch[];
delete tmp;
}
}
else
dele(root -> ch[d], x);
if (root != NULL)
root -> maintain();
}
int Get_kth(Node* root,int k)
{
if (root == NULL || k <= || k > root -> siz)
return ;
int s = (root -> ch[] == NULL ? : root -> ch[] ->siz);
if (s + == k)
return root -> key;
else if (s >= k)
return Get_kth(root -> ch[], k);
else
return Get_kth(root -> ch[], k - s -);
}
struct Query
{
int l, r, k,idx;
bool operator < (const Query &rhs)const
{
return r < rhs.r;
//return l < rhs.l || (l == rhs.l&&r < rhs.r);
}
}Q[];
int a[], ans[];
Node* treap;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n, m;
while (~scanf ("%d%d",&n, &m))
{
for (int i = ; i < n; i++)
scanf ("%d", a+i+);
for (int i = ; i < m; i++)
{
scanf ("%d%d%d",&Q[i].l, &Q[i].r, &Q[i].k);
Q[i].idx = i;
if (Q[i].l > Q[i].r)
swap(Q[i].l,Q[i].r);
}
sort (Q, Q+m);
int index = Q[].l, lidx = ;
for (int i = ; i < m; i++)
{
for (int j = lidx; j && j < min(Q[i].l, Q[i-].r+); j++)
dele(treap, a[j]);
lidx = Q[i].l;
for (int j = max(Q[i].l,index); j <= Q[i].r; j++)
{
insert(treap, a[j]);
}
index = Q[i].r+;
ans[Q[i].idx] = Get_kth(treap, Q[i].k); }
for (int i = ; i < m; i++)
{
printf("%d\n",ans[i]);
}
}
return ;
}

POJ2761---Feed the dogs (Treap求区间第k大)的更多相关文章

  1. POJ 1442 Black Box treap求区间第k大

    题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...

  2. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  3. HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)

    题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...

  4. G - KiKi's K-Number(树状数组求区间第k大)

    For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. No ...

  5. [POJ2761] Feed the dogs (Treap)

    题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...

  6. [poj2104]kth-number(归并树求区间第k大)

    复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...

  7. HDU2665 求区间第K大 主席树

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...

  8. hdu2665可持久化线段树,求区间第K大

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

随机推荐

  1. Trie和Ternary Search Tree介绍

    Trie树 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie树与二叉搜索树不同,键不是直接保存在节 ...

  2. [Redux] Avoiding Object Mutations with Object.assign() and ...spread

    Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects. ...

  3. linux 启动network后报错:device eth0 does not seem to be present, delaying initialization

    问题背景: 在vsphere client中部署ovf模板后启动linux 的network后提示:device eth0 does not seem to be present, delaying ...

  4. [转] The Single Biggest Obstacle to Trading Success

    Why do some people succeed spectacularly in the market while others fail? The market is the same for ...

  5. JS日期格式化函数性能优化篇

    最近开发的软件中需要用到日志功能,其中有一个重要功能是显示日期和时间.于是网上搜了一把,搜到大量的日期格式化函数,不过比较了下,感觉代码都不够优雅,而且性能都不给力.对线上一些代码进行了评测,以下是一 ...

  6. Java基础知识强化69:基本类型包装类之Character案例(统计字符串中大写小写以及数字的次数)

    我们直接看案例如下: package cn.itcast_03; import java.util.Scanner; /* * 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数.(不考虑 ...

  7. -bash: ulimit: open files: cannot modify limit: Operation not permitted

    普通用户登录系统报错,提示: -bash: ulimit: open files: cannot modify limit: Operation not permitted. 处理方法: #vi /e ...

  8. NYOJ737

    题意:给n堆石子,按照顺序排列,只能相邻两堆石子合并,求最后合并为一堆时所花费的最小代价,石子合并代价为两堆石子之和. 输入: n(石子堆数) Xi(每堆石子个数) 输出: T(最小代价) 思路:经典 ...

  9. SQL从入门到基础 - 01 数据库开发及ADO.Net

    一.数据库概述 1. 用自定义文件格式保存数据的劣势:并发性差,查找数据的速度差. 2. DBMS(DataBase Management System数据库管理系统)和数据库.平时谈到“数据库”的含 ...

  10. 搭建maven项目简介

    http://jingyan.baidu.com/album/9f7e7ec0b714ae6f29155465.html?picindex=1 Maven学习 (一) 搭建Maven环境 http:/ ...