裸的区间第k大问题,划分树搞起。

#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fillarray(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; #ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-12; /* -------------------------------------------------------------------------------- */ const int maxn = 1e5 + ; /** 过程:快排的过程,通过记录进入左子区间的个数的前缀和来解决区间第k大问题 **/
class PartitionTree {
int cnt[][maxn], val[][maxn], buf[maxn];
int n; void init(int a[], int n) {
this->n = n;
fillchar(cnt, );
fillchar(val, );
fillarray(val[], a);
fillarray(buf, a);
sort(buf, buf + n);
} void build(int l, int r, int dep) {
if (l == r) return ;
int m = (l + r) >> , c = , small = ;
for (int i = l; i <= r; i ++) small += val[dep][i] < buf[m];
for (int i = l; i <= r; i ++) {
if (c < m - l + ) {
if (val[dep][i] < buf[m] || val[dep][i] == buf[m] && small < m - l + ) {
cnt[dep][i] = ;
val[dep + ][l + c ++] = val[dep][i];
small += val[dep][i] == buf[m];
}
}
else break;
}
for (int i = l; i <= r; i ++) {
if (!cnt[dep][i]) val[dep + ][l + c ++] = val[dep][i];
}
build(l, m, dep + );
build(m + , r, dep + );
}
/** 第k小 */
int querykth(int L, int R, int k, int l, int r, int dep) {
if (k <= || k > R - L + ) return - ;
if (L == R) return val[dep][L];
int m = (l + r) >> , cl = cnt[dep][L - ] - cnt[dep][l - ], cr = cnt[dep][R] - cnt[dep][l - ];
if (cr - cl >= k) return querykth(l + cl, l + cr - , k, l, m, dep + );
return querykth(m + + L - l - cl, m + R - l + - cr, k - cr + cl, m + , r, dep + );
}
public:
void build(int a[], int n) {
init(a, n);
build(, n, );
for (int i = ; i < ; i ++) {
for (int j = ; j <= n; j ++) {
cnt[i][j] += cnt[i][j - ];
}
}
}
int querykth(int L, int R, int k) { return querykth(L, R, k, , n, ); }
};/** 下标从1开始 */ PartitionTree pt;
int a[maxn]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, m;
while (cin >> n >> m) {
for (int i = ; i <= n; i ++) {
scanf("%d", a + i);
}
pt.build(a, n);
int l, r, k;
for (int i = ; i < m; i ++) {
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", pt.querykth(l, r, k));
}
}
return ;
}

[NBUT 1458 Teemo]区间第k大问题,划分树的更多相关文章

  1. 整体二分初探 两类区间第K大问题 poj2104 & hdu5412

    看到好多讲解都把整体二分和$CDQ$分治放到一起讲 不过自己目前还没学会$CDQ$分治 就单独谈谈整体二分好了 先推荐一下$XHR$的 <浅谈数据结构题的几个非经典解法> 整体二分在当中有 ...

  2. 区间第k大问题 权值线段树 hdu 5249

    先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...

  3. poj2104 K大数 划分树

    题意:给定一个数列,求一个区间的第K大数 模板题, 其中的newl, newr 有点不明白. #include <iostream> #include <algorithm> ...

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

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

  5. POJ-2104-K-th Number(区间第K大+主席树模板题)

    Description You are working for Macrohard company in data structures department. After failing your ...

  6. 划分树---hdu4417---区间查找(不)大于h的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    ...

  7. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Ca ...

  8. 解决区间第K大的问题的各种方法

    例题:http://poj.org/problem?id=2104 最近可能是念念不忘,必有回响吧,总是看到区间第k大的问题,第一次看到是在知乎上有人面试被弄懵了后来又多次在比赛中看到.以前大概是知道 ...

  9. 线段树专题2-(加强版线段树-可持续化线段树)主席树 orz! ------用于解决区间第k大的问题----xdoj-1216

    poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

随机推荐

  1. Coin Change UVA

    Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to makech ...

  2. lua使用笔记1:Linux 中安装lua

    1.lua安装 1)http://www.lua.org/download.html为下载页面 linux中运行 wget http://www.lua.org/ftp/lua-5.2.3.tar.g ...

  3. 今天开始让我们一起来学JavaScript吧!(今天先扯先别的)

    1.为什么要学习JavaScript? 首先它是web开发人员必须学习的3门语言之一: ①HTML定义了网页的内容 ②CSS描述了网页的布局: ③JavaScript网页的行为 首先JavaScrip ...

  4. 前后端分离下用jwt做用户认证

    0 前后端分离下的用户信息认证 前端使用Vue+axios,后端使用SpringBoot+SpringSecurity. 为了解决http无状态的问题,我采用jwt(json web token)保存 ...

  5. python3爬虫 爬取动漫视频

    起因 因为本人家里有时候网速不行,所以看动漫的时候播放器总是一卡一卡的,看的太难受了.闲暇无聊又F12看看.但是动漫网站却无法打开控制台.这就勾起了我的兴趣.正好反正无事,去寻找下视频源. 但是这里事 ...

  6. redis:安装及基础知识(一)

    Redis官网:https://redis.io/ Redis中文网:http://www.redis.cn/ Redis 是一个开源的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. ...

  7. css的变量教程,更强大的css

    当微软宣布 Edge 浏览器将支持 CSS 变量.这个重要的 CSS 新功能,所有主要浏览器已经都支持了.本文全面介绍如何使用它,你会发现原生 CSS 从此变得异常强大. 一.变量的声明 声明变量的时 ...

  8. 前端基础进阶(七)-前端工程师最容易出错的问题-this关键字

    我们在学习JavaScript的时候,因为对一些概念不是很清楚,但是又会通过一些简洁的方式把它给记下来,那么这样自己记下来的概念和真正的概念产生了很强的偏差. 当然,还有一些以为这个是对的,还会把它发 ...

  9. 常用mysql 语句

    ALTER TABLE table_name AUTO_INCREMENT = 1;重置自增字段值从1开始 truncate table `table_name` 清空表,保留数据结构

  10. Zabbix备份数据文件

    mysql自带的工具mysqldump,当数据量大了之后进行全备所花的时间比较长,这样将会造成数据库的锁读.从而zabbix服务的监控告警不断,想着做下配置文件的备份.刚好有这么个脚本.满足了需求. ...