线段树模板题来源: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)基础模板的更多相关文章

  1. hdu1698(线段树区间替换模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意: 第一行输入 t 表 t 组测试数据, 对于每组测试数据, 第一行输入一个 n , 表示 ...

  2. 模板 - 数据结构 - 线段树/SegmentTree

    区间求加法和: 单点修改的,普通线段树. struct SegmentTree { #define ls (o<<1) #define rs (o<<1|1) static c ...

  3. 【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)

    第一部分---线段树:https://leetcode.com/tag/segment-tree/ [218]The Skyline Problem [307]Range Sum Query - Mu ...

  4. 【BZOJ 3196】二逼平衡树 线段树套splay 模板题

    我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...

  5. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 141093 ...

  6. 【luogu P3372 线段树1】 模板

    线段树的模板题 题目链接:https://www.luogu.org/problemnew/show/P3372 update区间修改,query区间求和 #include <iostream& ...

  7. java——线段树 SegmentTree

    应用: 区间染色 区间查询 线段树不是完全二叉树,线段树是平衡二叉树 使用数组来实现线段树:存储空间为4n 以下是使用数组实现的静态线段树: public class SegmentTree<E ...

  8. 启发式合并 splay合并 线段树合并基础

    Gold is everywhen! - somebody 启发式合并 将小的集合一个个插入到大的集合. 每次新集合大小至少比小集合大一倍,因此每个元素最多合并\(\log n\)次,总复杂度为\(n ...

  9. A Simple Problem with Integers(线段树区间更新模板)

    最基本的线段树的区间更新及查询和 用tag(lazy)数组来“延缓”更新,查询或添加操作必须进行pushdown操作,即把tag从p传到lp和rp并清楚tag[p],既然得往lp和rp递归,那么就可以 ...

随机推荐

  1. SAS数据挖掘实战篇【一】

    SAS数据挖掘实战篇[一] 1数据挖掘简介 1.1数据挖掘的产生 需求是一切技术之母,管理和计算机技术的发展,促使数据挖掘技术的诞生.随着世界信息技术的迅猛发展,信息量也呈几何指数增长,如何从巨量.复 ...

  2. 正说PropertyValuesProvider的应用

    Github地址:https://github.com/andyslin/spring-ext 编译.运行环境:JDK 8 + Maven 3 + IDEA + Lombok spring-boot: ...

  3. 【Linux开发】linux设备驱动归纳总结(三):2.字符型设备的操作open、close、read、write

    linux设备驱动归纳总结(三):2.字符型设备的操作open.close.read.write 一.文件操作结构体file_operations 继续上次没讲完的问题,文件操作结构体到底是什么东西, ...

  4. Eclipse注释

    首先,Window------>preferences------>java------>Code Style------>Code Templates------>Co ...

  5. apache 设置允许跨域

    1.首先找到 httpd.conf 文件 找到   #LoadModule headers_module modules/mod_headers.so 把#注释符去掉(也有可能已经默认去掉了) 目的是 ...

  6. java中利用hanlp比较两个文本相似度的步骤

    使用 HanLP - 汉语言处理包 来处理,他能处理很多事情,如分词.调用分词器.命名实体识别.人名识别.地名识别.词性识别.篇章理解.关键词提取.简繁拼音转换.拼音转换.根据输入智能推荐.自定义分词 ...

  7. SpringBoot自动化配置之四:@Conditional注解详解

    前言 之前在分析spring boot 源码时导出可见@ConditionalOnBean 之类的注解,那么它到底是如何使用的以及其工作流程如何,我们这里就围绕以下几点来分析: @Conditiona ...

  8. Type类的使用

    Type类的使用(类反射)通过类获得Type: Type t = typeof(Person)通过实例对象获得类的Type: Type t = p.GetType()获取Type的方法:MethodI ...

  9. const和static const的区别(未整理)

    对于C/C++语言来讲,const就是只读的意思,只在声明中使用;static一般有2个作用,规定作用域和存储方式.对于局部变量,static规定其为静态存储方式,每次调用的初始值为上一次调用的值,调 ...

  10. JavaScript刷新事件

    1, Location reload() 方法 2,