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

【线段树】P1253 扶苏的问题|普及+

本文涉及知识点

C++线段树

扶苏的问题

题目描述

给定一个长度为 n n n 的序列 a a a,要求支持如下三个操作:

  1. 给定区间 [ l , r ] [l, r] [l,r],将区间内每个数都修改为 x x x
  2. 给定区间 [ l , r ] [l, r] [l,r],将区间内每个数都加上 x x x
  3. 给定区间 [ l , r ] [l, r] [l,r],求区间内的最大值。

输入格式

第一行是两个整数,依次表示序列的长度 n n n 和操作的个数 q q q
第二行有 n n n 个整数,第 i i i 个整数表示序列中的第 i i i 个数 a i a_i ai
接下来 q q q 行,每行表示一个操作。每行首先有一个整数 o p op op,表示操作的类型。

  • o p = 1 op = 1 op=1,则接下来有三个整数 l , r , x l, r, x l,r,x,表示将区间 [ l , r ] [l, r] [l,r] 内的每个数都修改为 x x x
  • o p = 2 op = 2 op=2,则接下来有三个整数 l , r , x l, r, x l,r,x,表示将区间 [ l , r ] [l, r] [l,r] 内的每个数都加上 x x x
  • o p = 3 op = 3 op=3,则接下来有两个整数 l , r l, r l,r,表示查询区间 [ l , r ] [l, r] [l,r] 内的最大值。

输出格式

对于每个 o p = 3 op = 3 op=3 的操作,输出一行一个整数表示答案。

样例 #1

样例输入 #1

6 6
1 1 4 5 1 4
1 1 2 6
2 3 4 2
3 1 4
3 2 3
1 1 6 -1
3 1 6

样例输出 #1

7
6
-1

样例 #2

样例输入 #2

4 4
10 4 -3 -7
1 1 3 0
2 3 4 -4
1 2 4 -9
3 1 4

样例输出 #2

0

提示

数据规模与约定

  • 对于 10 % 10\% 10% 的数据, n = q = 1 n = q = 1 n=q=1
  • 对于 40 % 40\% 40% 的数据, n , q ≤ 1 0 3 n, q \leq 10^3 n,q103
  • 对于 50 % 50\% 50% 的数据, 0 ≤ a i , x ≤ 1 0 4 0 \leq a_i, x \leq 10^4 0ai,x104
  • 对于 60 % 60\% 60% 的数据, o p ≠ 1 op \neq 1 op=1
  • 对于 90 % 90\% 90% 的数据, n , q ≤ 1 0 5 n, q \leq 10^5 n,q105
  • 对于 100 % 100\% 100% 的数据, 1 ≤ n , q ≤ 1 0 6 1 \leq n, q \leq 10^6 1n,q106 1 ≤ l , r ≤ n 1 \leq l, r \leq n 1l,rn o p ∈ { 1 , 2 , 3 } op \in \{1, 2, 3\} op{1,2,3} ∣ a i ∣ , ∣ x ∣ ≤ 1 0 9 |a_i|, |x| \leq 10^9 ai,x109

提示

请注意大量数据读入对程序效率造成的影响。

线段树

最大值线段树的改进。
Save类型:long long ,某个数+1e9 1e5次超过int范围。
Record类型:int op,long long x。
如果op是1,缓存值变成(1,x)。
如果op1是2,缓存的类型是2, old.x += iNew.x。
如果缓存的类型是1,old.x += iNew.x。

代码

核心代码

#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>#include <bitset>
using namespace std;template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {in >> pr.first >> pr.second;return in;
}template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) ;return in;
}template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);return in;
}template<class T = int>
vector<T> Read() {int n;scanf("%d", &n);vector<T> ret(n);for(int i=0;i < n ;i++) {cin >> ret[i];}return ret;
}template<class T = int>
vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}template<int N = 12 * 1'000'000>
class COutBuff
{
public:COutBuff() {m_p = puffer;}template<class T>void write(T x) {int num[28], sp = 0;if (x < 0)*m_p++ = '-', x = -x;if (!x)*m_p++ = 48;while (x)num[++sp] = x % 10, x /= 10;while (sp)*m_p++ = num[sp--] + 48;}inline void write(char ch){*m_p++ = ch;}inline void ToFile() {fwrite(puffer, 1, m_p - puffer, stdout);}
private:char  puffer[N], * m_p;
};template<int N = 12 * 1'000'000>
class CInBuff
{
public:inline CInBuff() {fread(buffer, 1, N, stdin);}inline int Read() {int x(0), f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);return f ? -x : x;}
private:char buffer[N], * S = buffer;
};template<class TSave, class TRecord >
class CRangUpdateLineTree
{
protected:virtual void OnQuery(const TSave& save, const int& iSaveLeft, const int& iSaveRight) = 0;virtual void OnUpdate(TSave& save, const int& iSaveLeft, const int& iSaveRight, const TRecord& update) = 0;virtual void OnUpdateParent(TSave& par, const TSave& left, const TSave& r, const int& iSaveLeft, const int& iSaveRight) = 0;virtual void OnUpdateRecord(TRecord& old, const TRecord& newRecord) = 0;
};template<class TSave, class TRecord >
class CVectorRangeUpdateLineTree : public CRangUpdateLineTree<TSave, TRecord>
{
public:CVectorRangeUpdateLineTree(int iEleSize, TSave tDefault, TRecord tRecordNull) :m_iEleSize(iEleSize), m_save(iEleSize * 4, tDefault), m_record(iEleSize * 4, tRecordNull) {m_recordNull = tRecordNull;}void Update(int iLeftIndex, int iRightIndex, TRecord value){Update(1, 0, m_iEleSize - 1, iLeftIndex, iRightIndex, value);}void Query(int leftIndex, int rightIndex) {Query(1, 0, m_iEleSize - 1, leftIndex, rightIndex);}//void Init() {//	Init(1, 0, m_iEleSize - 1);//}TSave QueryAll() {return m_save[1];}void swap(CVectorRangeUpdateLineTree<TSave, TRecord>& other) {m_save.swap(other.m_save);m_record.swap(other.m_record);std::swap(m_recordNull, other.m_recordNull);assert(m_iEleSize == other.m_iEleSize);}
protected://void Init(int iNodeNO, int iSaveLeft, int iSaveRight)//{//	if (iSaveLeft == iSaveRight) {//		this->OnInit(m_save[iNodeNO], iSaveLeft);//		return;//	}//	const int mid = iSaveLeft + (iSaveRight - iSaveLeft) / 2;//	Init(iNodeNO * 2, iSaveLeft, mid);//	Init(iNodeNO * 2 + 1, mid + 1, iSaveRight);//	this->OnUpdateParent(m_save[iNodeNO], m_save[iNodeNO * 2], m_save[iNodeNO * 2 + 1], iSaveLeft, iSaveRight);//}void Query(int iNodeNO, int iSaveLeft, int iSaveRight, int iQueryLeft, int iQueryRight) {if ((iSaveLeft >= iQueryLeft) && (iSaveRight <= iQueryRight)) {this->OnQuery(m_save[iNodeNO], iSaveLeft, iSaveRight);return;}if (iSaveLeft == iSaveRight) {//没有子节点return;}Fresh(iNodeNO, iSaveLeft, iSaveRight);const int mid = iSaveLeft + (iSaveRight - iSaveLeft) / 2;if (mid >= iQueryLeft) {Query(iNodeNO * 2, iSaveLeft, mid, iQueryLeft, iQueryRight);}if (mid + 1 <= iQueryRight) {Query(iNodeNO * 2 + 1, mid + 1, iSaveRight, iQueryLeft, iQueryRight);}}void Update(int iNode, int iSaveLeft, int iSaveRight, int iOpeLeft, int iOpeRight, TRecord value){if ((iOpeLeft <= iSaveLeft) && (iOpeRight >= iSaveRight)){this->OnUpdate(m_save[iNode], iSaveLeft, iSaveRight, value);this->OnUpdateRecord(m_record[iNode], value);return;}if (iSaveLeft == iSaveRight) {return;//没有子节点}Fresh(iNode, iSaveLeft, iSaveRight);const int iMid = iSaveLeft + (iSaveRight - iSaveLeft) / 2;if (iMid >= iOpeLeft){Update(iNode * 2, iSaveLeft, iMid, iOpeLeft, iOpeRight, value);}if (iMid + 1 <= iOpeRight){Update(iNode * 2 + 1, iMid + 1, iSaveRight, iOpeLeft, iOpeRight, value);}// 如果有后代,至少两个后代this->OnUpdateParent(m_save[iNode], m_save[iNode * 2], m_save[iNode * 2 + 1], iSaveLeft, iSaveRight);}void Fresh(int iNode, int iDataLeft, int iDataRight){if (m_recordNull == m_record[iNode]){return;}const int iMid = iDataLeft + (iDataRight - iDataLeft) / 2;Update(iNode * 2, iDataLeft, iMid, iDataLeft, iMid, m_record[iNode]);Update(iNode * 2 + 1, iMid + 1, iDataRight, iMid + 1, iDataRight, m_record[iNode]);m_record[iNode] = m_recordNull;}vector<TSave> m_save;vector<TRecord> m_record;TRecord m_recordNull;const int m_iEleSize;
};typedef long long TSave;
typedef pair<int, long long>  TRecord;
class  CMyLineTree : public  CVectorRangeUpdateLineTree<TSave, TRecord>
{
public:long long m_ans = LLONG_MIN;using CVectorRangeUpdateLineTree::CVectorRangeUpdateLineTree;
protected:virtual void OnQuery(const TSave& save, const int& iSaveLeft, const int& iSaveRight) {m_ans = max(m_ans, save);}virtual void OnUpdate(TSave& save, const int& iSaveLeft, const int& iSaveRight, const TRecord& update) override{if (1 == update.first) {save = update.second;}else {save += update.second;}}virtual void OnUpdateParent(TSave& par, const TSave& left, const TSave& r, const int& iSaveLeft, const int& iSaveRight)  override{par = max(left, r);}virtual void OnUpdateRecord(TRecord& old, const TRecord& newRecord) override{if (1 == newRecord.first) {old = newRecord;}else {old.second += newRecord.second;}}
};//P1253 扶苏的问题
class Solution {
public:vector<long long> Ans(vector<int>& a, vector<tuple<int, int, int, int>>& que) {const int N = a.size();CMyLineTree tree(N + 1, LLONG_MIN / 2, { 2,0 });for (int i = 1; i <= N; i++) {tree.Update(i, i, { 1,a[i - 1] });}vector<long long> ans;for (const auto& [op, L, R, x] : que) {if (3 == op) {tree.m_ans = LLONG_MIN;tree.Query(L, R);ans.emplace_back(tree.m_ans);}else {tree.Update(L, R, { op,x });}}return ans;}
};int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUG	int n, q;
CInBuff<15*5*1000'000> ib;n = ib.Read();q = ib.Read();vector<int> a(n);for (int i = 0; i < n; i++) {a[i] = ib.Read();}vector<tuple<int, int, int, int>> que(q);for (int i = 0; i < q; i++) {get<0>(que[i]) = ib.Read();get<1>(que[i]) = ib.Read();get<2>(que[i]) = ib.Read();if (3 != get<0>(que[i])) { get<3>(que[i]) = ib.Read(); }}auto res = Solution().Ans(a,que);COutBuff<24*1000'000> ob;for (const auto& ll : res) {ob.write(ll);ob.write('\n');}ob.ToFile();
#ifdef _DEBUG	/*Out(a, "a=");Out(que, ",que="); */
#endif // DEBUG	return 0;
}

单元测试

	vector<int> a;vector<tuple<int, int, int, int>> que;TEST_METHOD(TestMethod11){		a = { 1,1,4,5,1,4 }, que = { {1,1,2,6},{2,3,4,2},{3,1,4,0},{3,2,3,0},{1,1,6,-1},{3,1,6,0} };auto res = Solution().Ans(a,que);AssertEx({ 7,6,-1 }, res);}TEST_METHOD(TestMethod12){a = { 10,4,-3,-7 }, que = { {1,1,3,0},{2,3,4,-4},{1,2,4,-9},{3,1,4,0} };auto res = Solution().Ans(a, que);AssertEx({ 0 }, res);}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

相关文章:

  • 操作系统期中复习
  • 初识Redis · C++客户端list和hash
  • 第七届传智杯全国IT技能大赛程序设计赛道 国赛(总决赛)—— (B组)题解
  • 【PyQt5】@QtCore.pyqtSlot()的作用
  • oracle不同数据库版本的自增序列
  • element-ui中的上传组件el-upload非自动上传监听不到success
  • go for 闭环问题【踩坑记录】
  • DeepseekV3MLP 模块
  • 快充协议芯片XSP04D支持使用一个Type-C与电脑传输数据和快充取电功能
  • 腾讯一面-软件开发实习-PC客户端开发方向
  • LX4-数据手册相关
  • CentOS 7进入救援模式——VirtualBox虚拟机
  • 23. git reset
  • unity TEngine学习4
  • 【Andorid备案获取keystore里面的公钥和SHA-1码等等】
  • 怎么发布、更新Python第三方库?以potx-cloud为例
  • PHP日志会对服务器产生哪些影响?
  • 基于DeepSeek/AI的资产测绘与威胁图谱构建
  • 华为VRP系统知识总结及案例试题
  • 【Python核心库实战指南】从数据处理到Web开发
  • GDP增长4.1%,一季度广东经济数据出炉
  • 牛市早报|现货黄金价格站上3400美元,上交所召开私募机构座谈会
  • 泽连斯基:乌英法美将在伦敦讨论停火事宜
  • 广东省东莞市委原书记、市人大常委会原主任徐建华被开除党籍
  • 北京媒体锐评男子地铁辱骂他人:北京地铁永远欢迎沾着泥巴的普通劳动者
  • ETF市场规模首破4万亿,月内ETF基金净流入超3000亿