线段树(SegmentTree)基础模板
线段树模板题来源:https://www.lintcode.com/problem/segment-tree-build/description
201. 线段树的构造
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end) {
* this->start = start, this->end = end;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param start: start value.
* @param end: end value.
* @return: The root of Segment Tree.
*/
SegmentTreeNode * build(int start, int end) {
// write your code here
if(start > end) return nullptr;
auto root = new SegmentTreeNode(start, end);
if(start < end){
auto mid = (start + end) / 2;
root->left = build(start, mid);
root->right = build(mid+1, end);
}
return root;
}
};
202. 线段树的查询
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, max;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int max) {
* this->start = start;
* this->end = end;
* this->max = max;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of segment tree.
* @param start: start value.
* @param end: end value.
* @return: The maximum number in the interval [start, end]
*/
int query(SegmentTreeNode * root, int start, int end) {
// write your code here
auto mid = root->start + (root->end - root->start) / 2;
if(start <= root->start && root->end <= end) return root->max;
else if(start > mid) return query(root->right, start, end);
else if(end <= mid) return query(root->left, start, end);
else return max(query(root->left, start, mid), query(root->right, mid+1, end));
}
};
203. 线段树的修改
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, max;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int max) {
* this->start = start;
* this->end = end;
* this->max = max;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of segment tree.
* @param index: index.
* @param value: value
* @return: nothing
*/
void modify(SegmentTreeNode * root, int index, int value) {
// write your code here
if(root == nullptr || index > root->end || index < root->start) return;
if(root->start == root->end) {
root->max = value;
return ;
}
auto mid = root->start + (root->end - root->start) / 2;
if(index > mid){
modify(root->right, index, value);
} else {
modify(root->left, index, value);
}
root->max = max(root->right->max, root->left->max);
}
};
247. 线段树查询 II
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, count;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int count) {
* this->start = start;
* this->end = end;
* this->count = count;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: The root of segment tree.
* @param start: start value.
* @param end: end value.
* @return: The count number in the interval [start, end]
*/
int query(SegmentTreeNode * root, int start, int end) {
// write your code here
if(root == NULL) return 0;
if(start <= root->start && root->end <= end) return root->count;
auto mid = root->start + (root->end - root->start) / 2;
if(end <= mid) return query(root->left, start, end);
else if(start > mid) return query(root->right, start, end);
else return query(root->left, start, mid) + query(root->right, mid + 1, end);
}
};
248. 统计比给定整数小的数的个数
class Solution {
public:
/**
* @param A: An integer array
* @param queries: The query list
* @return: The number of element in the array that are smaller that the given integer
*/
struct SegmentTreeNode {
int start, end, count;
SegmentTreeNode* left, *right;
SegmentTreeNode(int start_, int end_) :
start(start_), end(end_), count(0), left(nullptr), right(nullptr){}
};
SegmentTreeNode* build(int start, int end){
if(start > end) return nullptr;
auto root = new SegmentTreeNode(start, end);
if(start != end){
auto mid = (start + end) / 2;
root->left = build(start, mid);
root->right = build(mid+1, end);
}
return root;
}
void add(SegmentTreeNode* root, int index, int val){
auto mid = (root->start + root->end) / 2;
if(root->start == root->end) {
root->count += val;
return;
}
if(index > mid){
add(root->right, index, val);
} else {
add(root->left, index, val);
}
root->count += val;
}
int query(SegmentTreeNode* root, int start, int end){
if(start <= root->start && root->end <= end) return root->count;
auto mid = (root->start + root->end) / 2;
if(start > mid) return query(root->right, start, end);
else if(end <= mid) return query(root->left, start, end);
else return query(root->right, mid+1, end) + query(root->left, start, mid);
}
vector<int> countOfSmallerNumber(vector<int> &A, vector<int> &queries) {
// write your code here
auto root = build(0, 10000);
for(int i = 0; i < A.size(); i++){
add(root, A[i], 1);
}
vector<int> ret;
for(int i = 0; i < queries.size(); i++){
ret.push_back(query(root, 0, queries[i]-1));
}
return ret;
}
};
249. 统计前面比自己小的数的个数
class Solution {
public:
/**
* @param A: an integer array
* @return: A list of integers includes the index of the first number and the index of the last number
*/
struct SegmentTreeNode {
int start, end, count;
SegmentTreeNode* left, *right;
SegmentTreeNode(int start_, int end_) :
start(start_), end(end_), count(0), left(nullptr), right(nullptr){}
};
SegmentTreeNode* build(int start, int end){
if(start > end) return nullptr;
auto root = new SegmentTreeNode(start, end);
if(start != end){
auto mid = (start + end) / 2;
root->left = build(start, mid);
root->right = build(mid+1, end);
}
return root;
}
void add(SegmentTreeNode* root, int index, int val){
auto mid = (root->start + root->end) / 2;
if(root->start == root->end) {
root->count += val;
return;
}
if(index > mid){
add(root->right, index, val);
} else {
add(root->left, index, val);
}
root->count += val;
}
int query(SegmentTreeNode* root, int start, int end){
if(start > end) return 0;
if(start <= root->start && root->end <= end) return root->count;
auto mid = (root->start + root->end) / 2;
if(start > mid) return query(root->right, start, end);
else if(end <= mid) return query(root->left, start, end);
else return query(root->right, mid+1, end) + query(root->left, start, mid);
}
vector<int> countOfSmallerNumberII(vector<int> &A) {
// write your code here
auto root = build(0, 10000);
vector<int> ret;
for(int i = 0; i < A.size(); i++){
auto c = query(root, 0, A[i]-1);
ret.push_back(c);
add(root, A[i], 1);
}
return ret;
}
};
线段树(SegmentTree)基础模板的更多相关文章
- hdu1698(线段树区间替换模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意: 第一行输入 t 表 t 组测试数据, 对于每组测试数据, 第一行输入一个 n , 表示 ...
- 模板 - 数据结构 - 线段树/SegmentTree
区间求加法和: 单点修改的,普通线段树. struct SegmentTree { #define ls (o<<1) #define rs (o<<1|1) static c ...
- 【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)
第一部分---线段树:https://leetcode.com/tag/segment-tree/ [218]The Skyline Problem [307]Range Sum Query - Mu ...
- 【BZOJ 3196】二逼平衡树 线段树套splay 模板题
我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...
- POJ 3468:A Simple Problem with Integers(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
- 【luogu P3372 线段树1】 模板
线段树的模板题 题目链接:https://www.luogu.org/problemnew/show/P3372 update区间修改,query区间求和 #include <iostream& ...
- java——线段树 SegmentTree
应用: 区间染色 区间查询 线段树不是完全二叉树,线段树是平衡二叉树 使用数组来实现线段树:存储空间为4n 以下是使用数组实现的静态线段树: public class SegmentTree<E ...
- 启发式合并 splay合并 线段树合并基础
Gold is everywhen! - somebody 启发式合并 将小的集合一个个插入到大的集合. 每次新集合大小至少比小集合大一倍,因此每个元素最多合并\(\log n\)次,总复杂度为\(n ...
- A Simple Problem with Integers(线段树区间更新模板)
最基本的线段树的区间更新及查询和 用tag(lazy)数组来“延缓”更新,查询或添加操作必须进行pushdown操作,即把tag从p传到lp和rp并清楚tag[p],既然得往lp和rp递归,那么就可以 ...
随机推荐
- Python web 项目的依赖管理工具
Poetry可以帮助你声明.管理和安装Python项目的依赖项,确保你可以在任何地方都拥有正确的堆栈. Poetry支持Python 2.7 和Python 3以上 安装 Poetry提供了一个自定义 ...
- Git(5):其他用法
分支操作 (1) 删除远程分支 $git remote add origin ssh://git@xxx.git ##如果未连接远程分支要先连接 $git push origin :<remot ...
- 【.NET】Browser Link: Failed to deserialize JSON in Browser Link call
问题 VS2013中调试程序发现,在浏览器控制台输出如下截图代码:
- 关于js中断ajax请求
停止javascript的ajax请求,一种是设置超时时间让ajax自动断开,另一种为手动去停止ajax请求,其核心是调用XMLHttpRequest对象上的abort方法,这里,我们以jquery举 ...
- 直方图匹配原理与python、matlab实现
直方图匹配本质上是让两幅图像的累积直方图尽量相似,累积直方图相似了,直方图也就相似了. 把原图像img的直方图匹配到参考图像ref的直方图,包括以下几个步骤: 1. 求出原图像img的累积直方图img ...
- U盘安装Ubuntu Server CD-ROM挂载失败
U盘安装 Ubuntu Server 发生Failed to copy file from CD-ROM问题 使用UltraISO制作Ubuntu Server安装盘,在安装过程中出现[!!] Loa ...
- TensorFlow自编码器(AutoEncoder)之MNIST实践
自编码器可以用于降维,添加噪音学习也可以获得去噪的效果. 以下使用单隐层训练mnist数据集,并且共享了对称的权重参数. 模型本身不难,调试的过程中有几个需要注意的地方: 模型对权重参数初始值敏感,所 ...
- [转帖]linux中systemctl详细理解及常用命令
linux中systemctl详细理解及常用命令 2019年06月28日 16:16:52 思维的深度 阅读数 30 https://blog.csdn.net/skh2015java/article ...
- <<大道至简>>伪代码
import.java.大道至简.* public class YuGong { public static void main(String [] args) throws IOException ...
- ABC136E Max GCD
Thinking about different ways of thinking. --- LzyRapx 题目 思路比较容易想到. Observations: 每次操作过后和不变. 枚举和的因子 ...