区间问题

news/2025/9/19 20:38:08/文章来源:https://www.cnblogs.com/JaJH/p/19101655

区间问题

ST表(静态区间查找)

​ ST表是利用倍增思想来缩短时间的,数组 f[i][j] 的含义,从第 i 个元素开始,向后连续 2^j 个元素组成的区间内的最值(最大值或最小值,需提前确定)。

\[f[i][j] = \max\left(f[i][j-1],\ f\left[i + 2^{j-1}\right][j-1]\right) \]

void solved()
{int n, t;cin >> n >> t;for (int i = 1; i <= n; i++)cin >> stmax[i][0];for (int j = 1; (1 << j) <= n; j++){for (int i = 1; i + (1 << j) - 1 <= n; i++){stmax[i][j] = max(stmax[i][j - 1], stmax[i + (1 << (j - 1))][j - 1]);}}while (t--){int l, r;cin >> l >> r;int s = log2(r - l + 1);cout << max(stmax[l][s], stmax[r + 1 - (1 << s)][s]) << endl;}
}

树状数组

单修区查

int lowbit(int x)
{return x & -x;
}int query(int x)
{int sum = 0;for(int i = x; i; i -=lowbit(i)){sum += c[i];}return sum;
}void update(int x, int v)
{for(int i = x; i <= n; i += lowbit(i)){c[i] += v;}
}
void solved()
{cin >> n >> t;for (int i = 1; i <= n; i++){cin >> arr[i];update(i, arr[i]);}while(t --){int op, x, y, k;cin >> op;if(op == 1){cin >> x >> k;update(x, k);}else {cin >> x >> y;cout << query(y) - query(x - 1) << endl;}}
}

区修单查(差分)

​ 把t[i]构建成差分数组,query(x)时求(1, x)前缀和就是点x的值

//构建差分数组
update(i, x - last);
//修改区间
update(x, k);
update(y + 1, -k);

区修区查

int lowbit(int x)
{return x & -x;
}void update(int x, LL c) 
{for (int i = x; i <= n; i += lowbit(i)){b1[i] += c, b2[i] += x * c;}
}LL query(int x)  
{LL res = 0;for (int i = x; i; i -= lowbit(i)){res += (x + 1) * b1[i] - b2[i];}return res;
}

题目链接二维模板题

二维树状数组模板(区修区查),公式推导过程太复杂,记下就好(其实就是不会)_ 根二维差分数组类似

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;const int N = 2050;int n, m ,q;LL a[N][N], b[N][N], c[N][N], d[N][N];int lowbit(int x){return x & -x;
}void add(int x, int y, LL w){for(int i = x; i <= n; i += lowbit(i)){for(int j = y; j <= m; j += lowbit(j)){a[i][j] += w;b[i][j] += (x - 1) * w;c[i][j] += (y - 1) * w;d[i][j] += (x - 1) * (y - 1) * w;}}
}LL query(int x, int y){LL res = 0;for(int i = x; i > 0; i -= lowbit(i)){for(int j = y; j > 0; j -= lowbit(j)){res += x * y * a[i][j] - y * b[i][j]- x * c[i][j]+ d[i][j];}}return res;
}int main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m >> q;while (q --) {int op, x1, y1, x2, y2;cin >> op >> x1 >> y1 >> x2 >> y2;if (op == 1) {LL v;cin >> v;add(x1, y1, v);add(x1, y2 + 1, -v);add(x2 + 1, y1, -v);add(x2 + 1, y2 + 1, v);} else {cout << query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1)<< '\n';}}
}

线段树

单修区查

struct node
{int l, r, sum;
} tree[4 * N];void build(int i, int l, int r)
{tree[i].l = l;tree[i].r = r;if (l == r){tree[i].sum = arr[l];return;}int mid = (l + r) >> 1;build(i * 2, l, mid);build(i * 2 + 1, mid + 1, r);tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum;
}void update(int i, int x, int k)
{if (tree[i].l == tree[i].r){tree[i].sum += k;return;}if (x <= tree[i * 2].r)update(i * 2, x, k);elseupdate(i * 2 + 1, x, k);tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum;
}int query(int i, int l, int r)
{if (tree[i].l >= l && tree[i].r <= r){return tree[i].sum;}if (tree[i].l > r || tree[i].r < l)return 0;int res = 0;if (tree[i * 2].r >= l)res += query(i * 2, l, r);if (tree[i * 2 + 1].l <= r)res += query(i * 2 + 1, l, r);return res;
}

区修区查(lazy 延迟更新)

​ 线段树的区间更新(如给 [L, R] 内所有元素加 delta),如果不使用懒标记,需要递归遍历所有与 [L, R] 重叠的叶子节点并逐个更新,在最坏情况下(如更新整个数组)时间复杂度为 O (n),效率极低。

​ 当更新的区间完全覆盖某个节点的区间时,不立即更新该节点的子节点,而是给该节点打上一个 “标记”,记录需要更新的内容。只有当后续操作(如查询、再次更新)需要访问该节点的子节点时,才将标记 “下推” 到子节点,确保子节点的状态正确。

struct node
{int l, r, sum, lazy;
} tree[4 * N];void build(int i, int l, int r)
{tree[i].l = l;tree[i].r = r;tree[i].lazy = 0;tree[i].sum = 0;if (l == r){tree[i].sum = arr[l];return;}int mid = (l + r) >> 1;build(i * 2, l, mid);build(i * 2 + 1, mid + 1, r);tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum;
}void pushdown(int i)
{if (tree[i].lazy){tree[i * 2].lazy += tree[i].lazy;tree[i * 2 + 1].lazy += tree[i].lazy;int mid = (tree[i].l + tree[i].r) >> 1;tree[i * 2].sum += tree[i].lazy * (mid - tree[i * 2].l + 1);tree[i * 2 + 1].sum += tree[i].lazy * (tree[i * 2 + 1].r - mid + 1);tree[i].lazy = 0;}
}
void update(int i, int l, int r, int k)
{if (tree[i].l >= l && tree[i].r <= r){tree[i].sum += k * (tree[i].r - tree[i].l + 1);tree[i].lazy += k;return;}pushdown(i);if (tree[i * 2].r >= l)update(i * 2, l, r, k);if (tree[i * 2 + 1].l <= r)update(i * 2 + 1, l, r, k);tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum;
}int query(int i, int l, int r)
{if (tree[i].l >= l && tree[i].r <= r){return tree[i].sum;}if (tree[i].l > r || tree[i].r < l)return 0;pushdown(i);int res = 0;if (tree[i * 2].r >= l)res += query(i * 2, l, r);if (tree[i * 2 + 1].l <= r)res += query(i * 2 + 1, l, r);return res;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/908071.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

操作系统,知识体系一共包含哪些部分? - 实践

操作系统,知识体系一共包含哪些部分? - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &q…

vscode 下载 VS Code Server 卡住(无需手动下载)

其实 vscode server 的默认下载逻辑是这样的(auto 模式): 优先在远程直接下载,如果远程服务器连接不到 https://update.code.visualstudio.com/ ,就会转为本地下载,然后 scp 复制到远程。 但容易出现一个网络波…

详细介绍:Git如何无痕上传当前项目最新状态从当前远程到另一个远程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

【qt】全局事件总线

#ifndef APPEVENT_HPP #define APPEVENT_HPP#include <QObject> #include <QMetaMethod> #include <QCoreApplication>class AppEvent : public QObject {Q_OBJECT public:static AppEvent &ins…

查询本地IPV6 地址

https://ipw.cn/ https://ipv6ready.me/index.html.zh_CN # 请勿用于商业用途,仅供个人测试学习之用,请遵守中国法律法规 # 查询本机外网IPv4地址 curl 4.ipw.cn# 查询本机外网IPv6地址 curl 6.ipw.cn# 测试网络是IP…

深入解析:React Device Detect 完全指南:构建响应式跨设备应用的最佳实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

ctfshow web89

<?php/* # -*- coding: utf-8 -*- # @Author: h1xa # @Date: 2020-09-16 11:25:09 # @Last Modified by: h1xa # @Last Modified time: 2020-09-18 15:38:51 # @email: h1xa@ctfer.com # @link: https://ctfer…

ctfshow web90

<?php/* # -*- coding: utf-8 -*- # @Author: h1xa # @Date: 2020-09-16 11:25:09 # @Last Modified by: h1xa # @Last Modified time: 2020-09-18 16:06:11 # @email: h1xa@ctfer.com # @link: https://ctfer…

解决 Ubuntu 25.04 下 make menuconfig 报 ncurses 错误的问题 - 指南

解决 Ubuntu 25.04 下 make menuconfig 报 ncurses 错误的问题 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: &q…

web359

题目提示 打无密码的mysql为什么是无密码呢? https://paper.seebug.org/510/ MySQL客户端连接并登录服务器时存在两种情况: 需要密码认证以及无需密码认证。当需要密码认证时使用挑战应答模式, 服务器先发送salt然后…

实用指南:Android中handler机制

实用指南:Android中handler机制2025-09-19 20:18 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !import…

web360

<?php error_reporting(0); highlight_file(__FILE__); $url=$_POST[url]; $ch=curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result=curl_exec($ch); …

缺失的第一个正数-leetcode

题目描述 给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。 请你实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案。 示例 1: 输入:nums = [1,2,0] 输出:3 解释:范围 [1,2] 中的…

hbase的安装应用

1.完成虚拟机上hbase的安装 2.完成idea与hbase的连接

如何在后端优雅地生成并传递动态错误提示?

在现代Web应用开发中,向前端返回清晰、准确且结构化的错误信息至关重要。这不仅能提升用户体验,还能简化前端应用的逻辑处理。然而,在复杂的业务场景下,如何优雅地处理那些需要动态生成的错误提示(例如,“密码错…

ctfshow web357

<?php error_reporting(0); highlight_file(__FILE__); $url=$_POST[url]; $x=parse_url($url); if($x[scheme]===http||$x[scheme]===https){ $ip = gethostbyname($x[host]); echo </br>.$ip.</br>;…

深入解析:Java全栈开发面试实录:从基础到微服务的实战解析

深入解析:Java全栈开发面试实录:从基础到微服务的实战解析pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Conso…

实用指南:设计模式:建造者模式

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

web358

<?php error_reporting(0); highlight_file(__FILE__); $url=$_POST[url]; $x=parse_url($url); if(preg_match(/^http:\/\/ctf\..*show$/i,$url)){echo file_get_contents($url); }检测url以 **<font style=&q…

谁会不爱低温静音 性能还更强的!酷睿Ultra 5 230F vs 锐龙5 9600X生产力、功耗、温度全方位对比

谁会不爱低温静音 性能还更强的!酷睿Ultra 5 230F vs 锐龙5 9600X生产力、功耗、温度全方位对比Posted on 2025-09-19 20:11 lzhdim 阅读(0) 评论(0) 收藏 举报一、前言:两款千元处理器的全方位对比 在酷睿Ultr…