线段树模板题来源: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. C基础知识(9):输入输出、文件读写

    输入输出 (1) getchar() & putchar() 函数: 读写字符. (2) scanf() 和 printf() 函数:根据提供的format来转换输入为其他数据类型,并根据提供 ...

  2. gin框架教程:代码系列demo地址

    gin框架教程代码地址: https://github.com/jiujuan/gin-tutorial demo目录: 01quickstart 02parameter 03route 04midd ...

  3. 解决vmware fusion + centos 7安装vmtools时提示The path "" is not a valid path to the xxx kernel headers.

    近日使用VMware fushion 8 + centos 7.0时,无法使用共享功能,所以必须安装vmtools.但是安装过程中有2个错误需要解决. 1.gcc错误 Searching for GC ...

  4. 抓包分析IP如何设置详细步骤

    首先,要知道的是,我们直接改以太网(校园网)的IP地址是不行的,校园网识别不了 如下图: 我们必须通过让电脑连接个人热点才能完成IP修改. 第一步,连接上热点后打开电脑的cmd命令程序,在命令窗口中输 ...

  5. 【神经网络与深度学习】Google Protocol Buffer介绍

    简介 什么是 Google Protocol Buffer? 假如您在网上搜索,应该会得到类似这样的文字介绍: Google Protocol Buffer( 简称 Protobuf) 是 Googl ...

  6. NoSQL--couchdb

    Couchdb CouchDB是Apache组织发布的一款开源的.面向文档类型的NoSQL数据库.由Erlang编写,使用json格式保存数据.CouchDB以RESTful的格式提供服务可以很方便的 ...

  7. 使用PowerShell 自动安装VC++补丁

    执行环境:Windows Server 2012 R2 VC++下载链接 这里有个问题,虽说可以静默安装,但是未对当前系统检测是否已安装vc++补丁,望大佬指点 # author:lttr <w ...

  8. Macaca环境搭建(一)----windows系统macaca安装

    一.安装JDK, 1.官方网站:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  9. PostgreSQL查看等待锁的SQL和进程

    查看等待锁的查询和进程: The following query may be helpful to see what processes are blocking SQL statements (t ...

  10. python 学习笔记三 (函数)

    1.把函数视为对象 def factorial(n): '''return n!''' return 1 if n < 2 else n*factorial(n-1) print(factori ...