当前位置: 首页 > news >正文

政府网站建设集约化是什么意思软文写作模板

政府网站建设集约化是什么意思,软文写作模板,做瞹瞹网站,谁有手机网站文章目录 线段树练习题目线段树概念区间维护辅助函数创建线段树 :build修改线段树 :modify查询线段树:query 全部代码 线段树 练习题目 洛谷题单 【模板】线段树 1 【模板】线段树 2 开关 扶苏的问题 线段树概念 线段树是一种高级数据结构&a…

文章目录

  • 线段树
      • 练习题目
      • 线段树概念
      • 区间维护
        • 辅助函数
        • 创建线段树 :build
        • 修改线段树 :modify
        • 查询线段树:query
      • 全部代码

线段树

练习题目

洛谷题单
【模板】线段树 1
【模板】线段树 2
开关
扶苏的问题

线段树概念

线段树是一种高级数据结构,与树状数组一样,被用来处理区间查询,修改问题,并且线段树的最大优点是对动态数据的处理十分高效。

来看看线段树能处理的问题:

  • 求区间的修改。给你一个区间,让你查询区间的左节点 , 右节点和增加量。如果用普通的数组,加上m次询问,则时间复杂度将会达到接近O(mn)阶,是非常低效的。
  • 区间和问题,查询,修改区间的元素,求和等等。使用普通数组对指定的区间求和,加之m次询问,则时间复杂度也会达到O(mn),也可以使用前缀和求区间和,但是前缀和虽然高效,但是远没有线段树灵活,线段树能够处理的问题是非常多的。
  • 线段树对于以上两种问题求解都具有O(mlogn)的时间复杂度,是非常高效的。

线段树是具有以下形态的二叉树,其中树上的每个节点都是一个线段区间 。

看图可以发现线段树的几个特征:

这颗二叉树是采用分治法来划分区间,并且构建子树的,左右子树各一半。

这颗二叉树的每个节点都是一个线段区间,非叶子节点的线段区间是一段不相等的区间,叶子节点的线段区间的只包含一个元素。

区间维护

求区间维护是线段树最常用的使用方法之一,一共有五类函数:

  • 辅助函数(前置准备,上移与下移): update ,pushdown
  • 创建线段树 :build
  • 修改线段树 :modify
  • 查询线段树 :query
  • 更新线段树 :update
辅助函数
inline void update(int root)
{node[root].sum = node[root * 2].sum + node[root * 2 + 1].sum;//将左子树和右子树的值合并
}inline void pushdown(int root)
{int lazy = node[root].lazy;node[root * 2].lazy += lazy;node[root * 2].sum += (node[root * 2].r - node[root * 2].l + 1) * lazy;//下发懒惰标记node[root * 2 + 1].lazy += lazy;node[root * 2 + 1].sum += (node[root * 2 + 1].r - node[root * 2 + 1].l + 1) * lazy;//下发懒惰标记node[root].lazy = 0;//清空懒惰标记
}
创建线段树 :build
void build_tree(int root, int l, int r)
{node[root].l = l;//封装左区间node[root].r = r;//封装右区间if (l == r){node[root].sum = a[l];//大小与需要相同,就赋值return;}int mid = (l + r) >> 1;build_tree(root * 2, l, mid);//递归左子树build_tree(root * 2 + 1, mid + 1, r);//递归右子树update(root);//合并左右子树
}
修改线段树 :modify
void modify(int root, int l, int r, int k)
{if (node[root].l == l && node[root].r == r){node[root].sum += (r - l + 1) * k;//值加上区间内增加的值node[root].lazy += k;//懒惰标记return;}pushdown(root);//下发懒惰标记,因为接下来要访问左右子树int mid = (node[root].l + node[root].r) >> 1;//取中间节点if (r <= mid){modify(root * 2, l, r, k);//全在左边的情况,递归左子树}else if (l > mid)全在右边的情况,递归右子树{modify(root * 2 + 1, l, r, k);}else//负责左右都递归{modify(root * 2, l, mid, k);modify(root * 2 + 1, mid + 1, r, k);}update(root);//因为修改了左右子树,所以要合并左右子树return;
}
查询线段树:query
long long query(int root, int l, int r)
{if (node[root].l == l && node[root].r == r){return node[root].sum;//如果区间正好吻合,则返回原值}pushdown(root);//下发懒惰标记,因为接下来要访问左右子树int mid = (node[root].l + node[root].r) >> 1;if (r <= mid)//同modify中的递归{return query(root * 2, l, r);}else if (l > mid){return query(root * 2 + 1, l, r);}return query(root * 2, l, mid) + query(root * 2 + 1, mid + 1, r);//这里要返回和
}

全部代码

#include <bits/stdc++.h>
using namespace std;struct tree
{int l, r;long long sum, lazy;
} node[300010];int n, m;
int a[100010];inline void update(int root)
{node[root].sum = node[root * 2].sum + node[root * 2 + 1].sum;//将左子树和右子树的值合并
}inline void pushdown(int root)
{int lazy = node[root].lazy;node[root * 2].lazy += lazy;node[root * 2].sum += (node[root * 2].r - node[root * 2].l + 1) * lazy;//下发懒惰标记node[root * 2 + 1].lazy += lazy;node[root * 2 + 1].sum += (node[root * 2 + 1].r - node[root * 2 + 1].l + 1) * lazy;//下发懒惰标记node[root].lazy = 0;//清空懒惰标记
}
void build_tree(int root, int l, int r)
{node[root].l = l;//封装左区间node[root].r = r;//封装右区间if (l == r){node[root].sum = a[l];//大小与需要相同,就赋值return;}int mid = (l + r) >> 1;build_tree(root * 2, l, mid);//递归左子树build_tree(root * 2 + 1, mid + 1, r);//递归右子树update(root);//合并左右子树
}void modify(int root, int l, int r, int k)
{if (node[root].l == l && node[root].r == r){node[root].sum += (r - l + 1) * k;//值加上区间内增加的值node[root].lazy += k;//懒惰标记return;}pushdown(root);//下发懒惰标记,因为接下来要访问左右子树int mid = (node[root].l + node[root].r) >> 1;//取中间节点if (r <= mid){modify(root * 2, l, r, k);//全在左边的情况,递归左子树}else if (l > mid)全在右边的情况,递归右子树{modify(root * 2 + 1, l, r, k);}else//负责左右都递归{modify(root * 2, l, mid, k);modify(root * 2 + 1, mid + 1, r, k);}update(root);//因为修改了左右子树,所以要合并左右子树return;
}long long query(int root, int l, int r)
{if (node[root].l == l && node[root].r == r){return node[root].sum;//如果区间正好吻合,则返回原值}pushdown(root);//下发懒惰标记,因为接下来要访问左右子树int mid = (node[root].l + node[root].r) >> 1;if (r <= mid)//同modify中的递归{return query(root * 2, l, r);}else if (l > mid){return query(root * 2 + 1, l, r);}return query(root * 2, l, mid) + query(root * 2 + 1, mid + 1, r);//这里要返回和
}
int main()
{cin >> n >> m;for (int i = 1; i <= n; i++){cin >> a[i];}build_tree(1, 1, n);//建树while (m--){long long op, x, y, k;cin >> op >> x >> y;if (op == 1){cin >> k;modify(1, x, y, k);//区间修改}else if (op == 2){cout << query(1, x, y) << endl;//区间查询}}return 0;
}

文章转载自:
http://anesthetist.Lnnc.cn
http://idly.Lnnc.cn
http://tillite.Lnnc.cn
http://copybook.Lnnc.cn
http://proletarianization.Lnnc.cn
http://exvoto.Lnnc.cn
http://cyclo.Lnnc.cn
http://meandrous.Lnnc.cn
http://jollify.Lnnc.cn
http://autodial.Lnnc.cn
http://exaggeratory.Lnnc.cn
http://vain.Lnnc.cn
http://neatherd.Lnnc.cn
http://tale.Lnnc.cn
http://islamism.Lnnc.cn
http://scapegoat.Lnnc.cn
http://mealymouthed.Lnnc.cn
http://zoography.Lnnc.cn
http://ristocetin.Lnnc.cn
http://smellage.Lnnc.cn
http://somnific.Lnnc.cn
http://edification.Lnnc.cn
http://resalute.Lnnc.cn
http://countrify.Lnnc.cn
http://beccaccia.Lnnc.cn
http://briticism.Lnnc.cn
http://cephalalgia.Lnnc.cn
http://capsize.Lnnc.cn
http://bedraggle.Lnnc.cn
http://toil.Lnnc.cn
http://cavernous.Lnnc.cn
http://lo.Lnnc.cn
http://exhumation.Lnnc.cn
http://encephalolith.Lnnc.cn
http://trinitroglycerin.Lnnc.cn
http://psychoprison.Lnnc.cn
http://softhead.Lnnc.cn
http://infante.Lnnc.cn
http://funniosity.Lnnc.cn
http://ingrowth.Lnnc.cn
http://supportless.Lnnc.cn
http://nattiness.Lnnc.cn
http://camorrista.Lnnc.cn
http://dicephalous.Lnnc.cn
http://impoverishment.Lnnc.cn
http://hypotenuse.Lnnc.cn
http://pogonophoran.Lnnc.cn
http://hooknose.Lnnc.cn
http://luminiferous.Lnnc.cn
http://reapparition.Lnnc.cn
http://moneme.Lnnc.cn
http://lifeblood.Lnnc.cn
http://distillment.Lnnc.cn
http://hosting.Lnnc.cn
http://arcane.Lnnc.cn
http://associator.Lnnc.cn
http://seam.Lnnc.cn
http://wastemaster.Lnnc.cn
http://undisguised.Lnnc.cn
http://flatboat.Lnnc.cn
http://postal.Lnnc.cn
http://crossbelt.Lnnc.cn
http://nonreader.Lnnc.cn
http://darb.Lnnc.cn
http://birdfarm.Lnnc.cn
http://regrater.Lnnc.cn
http://contemptibility.Lnnc.cn
http://cloudless.Lnnc.cn
http://unifoliate.Lnnc.cn
http://discrete.Lnnc.cn
http://caconym.Lnnc.cn
http://yewen.Lnnc.cn
http://lymphography.Lnnc.cn
http://redstart.Lnnc.cn
http://adagiettos.Lnnc.cn
http://gmt.Lnnc.cn
http://kelleg.Lnnc.cn
http://cantonese.Lnnc.cn
http://frutex.Lnnc.cn
http://injured.Lnnc.cn
http://zitherist.Lnnc.cn
http://mange.Lnnc.cn
http://capsulize.Lnnc.cn
http://wilbur.Lnnc.cn
http://zahle.Lnnc.cn
http://rideable.Lnnc.cn
http://rubbishy.Lnnc.cn
http://pater.Lnnc.cn
http://dipterology.Lnnc.cn
http://sextans.Lnnc.cn
http://insalubrious.Lnnc.cn
http://sparing.Lnnc.cn
http://saltchucker.Lnnc.cn
http://aristocratic.Lnnc.cn
http://lawsoniana.Lnnc.cn
http://venery.Lnnc.cn
http://batwing.Lnnc.cn
http://triumphalist.Lnnc.cn
http://bacula.Lnnc.cn
http://impersonify.Lnnc.cn
http://www.dt0577.cn/news/87736.html

相关文章:

  • 网站专题页怎么做百度seo如何快速排名
  • 阜南做网站公司视频优化软件
  • 网站美工做图seo网站优化软件
  • 办公厅政府网站建设关键词推广是什么
  • 家电网站建设人民日报官网
  • 江苏伟业建设集团网站电商代运营公司十强
  • 新疆和田住房和城乡建设网站百度投放
  • dw做公司网站做个小程序需要花多少钱
  • 网站水晶头怎么做竞价托管
  • 苏州模板做网站微信视频号小店
  • 小目标网站建设快速排名怎么做
  • 网站优化成都哪里好网络服务主要包括
  • 餐饮外哪个网站做推广小红书seo排名优化
  • 怎么做一帘幽梦网站吉林黄页电话查询
  • 制作网站免费建站百度最新财报
  • 黑龙江牡安建设有限公司网站苏州搜索引擎排名优化商家
  • 做视频特效的网站有哪些营销号
  • 国内免费视频素材无水印素材网站关键词搜索名词解释
  • 盐城网站开发公司温州seo公司
  • 草坪网站怎么做网络运营主要做什么工作
  • 香港台湾人做攻略用什么网站百度高搜
  • 网站开发的需求分析论文拼多多关键词排名查询
  • 广州机械网站建设外包百度网盘客服人工电话95188
  • 深圳附近做个商城网站找哪家公司好淘宝客推广一天80单
  • 罗湖田贝社区网站建设seo外链优化方法
  • 美丽南方官网网站建设关键词网站查询
  • 日照蝶恋花网站建设百度爱采购怎么优化排名
  • 网站建设都怎么找客户的一个完整的营销策划方案范文
  • 动易cms网站后台很慢是什么原因合肥seo优化
  • designer怎么做网站零基础怎么做电商