HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁
思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的。
具体可以看q学姐的B站视频
代码:
#include<cmath> #include<set> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include <iostream> #include<algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 1e5 + 10; const int M = maxn * 30; const ull seed = 131; const int INF = 0x3f3f3f3f; const int MOD = 1000000007; int n, q, tot; int a[maxn], root[maxn]; vector<int> st; int getId(int x){return lower_bound(st.begin(), st.end(),x) - st.begin() + 1; } struct node{int lson, rson;int sum; }T[maxn * 40]; void update(int l, int r, int &now, int pre, int v, int pos){T[++tot] = T[pre], T[tot].sum += v, now = tot;if(l == r) return;int m = (l + r) >> 1;if(m >= pos)update(l, m, T[now].lson, T[pre].lson, v, pos);elseupdate(m + 1, r, T[now].rson, T[pre].rson, v, pos); } int query(int l, int r, int pre, int now, int k){if(l == r) return l;int m = (l + r) >> 1;int sum = T[T[now].lson].sum - T[T[pre].lson].sum;if(sum >= k)return query(l, m, T[pre].lson, T[now].lson, k);elsereturn query(m + 1, r, T[pre].rson, T[now].rson, k - sum); } int main(){int t;scanf("%d", &t);while(t--){tot = 0;scanf("%d%d", &n, &q);st.clear();for(int i = 1; i <= n; i++)scanf("%d", &a[i]), st.push_back(a[i]);sort(st.begin(), st.end());st.erase(unique(st.begin(), st.end()), st.end());for(int i = 1; i <= n; i++)update(1, n, root[i], root[i - 1], 1, getId(a[i]));while(q--){int l, r, k;scanf("%d%d%d", &l, &r, &k);printf("%d\n", st[query(1, n, root[l - 1], root[r], k) - 1]);}}return 0; }
转载于:https://www.cnblogs.com/KirinSB/p/10745320.html
HDU 2665 Kth number(主席树静态区间第K大)题解相关推荐
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- hdu 2665 Kth number 划分树
求区间第k大元素的值, 看代码的注释. #include<cstring> #include<cstdio> #include<iostream> #include ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- Greedy Sequence(2019南京icpc网络预选赛)主席树求区间小于k的最大值
题意:给出n个整数,构造s1,s2,s3-sn s1,s2,s3-sns1,s2,s3-sn,si sisi满足五个条件 1.s1[i]=i s1[i]=is1[i]=i 2.对于1<j< ...
- 【代码源 Div1 - 108】#464. 数数(主席树,区间比k小的数的个数)HDU4417
problem solution 主席树查询区间比k小的数的个数 建树之后直接在目标区间的主席树内将 H 作为挡板递归计数. #include<bits/stdc++.h> using n ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- 【划分树】 POJ 2104 HDU 2665 K-th Number 裸题
了解了.... #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ma ...
- hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)
题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...
- Just $h$-index HDU - 6278(主席树找区间大于等于k的个数)
The hh-index of an author is the largest hh where he has at least hh papers with citations not less ...
- 划分树基础 —— HDU 2665 Kth number
对应 HDU 题目 :点击打开链接 Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K ...
最新文章
- 直播|俄勒冈州立大学李伏欣:从热点图到对深度网络的结构化解释
- nginx 配置的server_name参数(转)
- 【翻译】理念:无冲突的扩展本地DOM原型
- C语言fstat函数获取文件的大小
- 摘: cmd环境 使用一点知识
- 好枪手是靠子弹喂出来的,好分析师是靠大量项目沉淀出来的
- 高并发秒杀——SpringBoot集成redis
- java中的文件处理io_Java的IO前奏曲:文件操作类之___File
- Spring详解(四)------注解配置IOC、DI
- VS2012错误之 warning LNK4075: 忽略“/EDITANDCONTINUE”(由于“/SAFESEH”规范)
- Linux上PyCharm报错及解决:ImportError: No module named 'sklearn'
- Obj-C的hello,world 1
- sonar扫描前端代码,解决CSS files were not analyzed Node.js version could not be detected using command:node-v
- AUTOMATE THE BORING STUFF WITH PYTHON读书笔记 - 第4章:LISTS
- ROS入门(十)——两只小乌龟(乌龟跟随C++实现)
- C#面向对象编程:构造函数
- 交换机putty怎么调试_【技术】如何登陆交换机进行命令配置?常用的交换机组网模拟器有哪些?...
- 【Android-Kotlin】匿名内部类与Lambda 表达式(附RecycleView监听)
- IOS项目新手引导页图片适配方案
- 系统部署服务器位置,室内定位系统在实际项目中的部署