首页 > 其他分享 >[线段树 多tag]D-数据结构

[线段树 多tag]D-数据结构

时间:2022-11-25 19:40:07浏览次数:43  
标签:lazy ch int 线段 tag 数据结构 root sum op


[线段树 多tag]D-数据结构

题目

[线段树 多tag]D-数据结构_线段树


[线段树 多tag]D-数据结构_c++_02

思路

多tag的线段树有时序性问题,因此不能直接把标记累计。
例如:对于同样两个标记+和*,ax+b和(x+b)*a是不一样的,因此我们需要设计tag合并的规则。

对于一个标记,他可以表示成ax+b的形式,因此我按照下图的方式来推导合并后的lazy

同时推导区间所需要的区间和,区间平方和信息

[线段树 多tag]D-数据结构_c++_03

设计好lazy相关函数就完事了

void init_lazy(int root)
{
t[root].lazy[0] = 1;//乘法把1作为初始值
t[root].lazy[1] = 0;//加法把0作为初始值
}
void union_lazy(int fa, int ch)
{
long long temp[2];
temp[0] = t[fa].lazy[0] * t[ch].lazy[0];//a=a1*a2
temp[1] = t[fa].lazy[0] * t[ch].lazy[1] + t[fa].lazy[1];b//b=a1*b2+b1
t[ch].lazy[0] = temp[0];
t[ch].lazy[1] = temp[1];
}
void cal_lazy(int root)
{
//因为平方和用到了非平方和的项,所以要先更新平方项
t[root].sum[1] = t[root].lazy[0] * t[root].lazy[0] * t[root].sum[1] +
t[root].lazy[1] * t[root].lazy[1] * (t[root].r - t[root].l + 1) +
t[root].lazy[0] * t[root].lazy[1] * 2 * t[root].sum[0];

t[root].sum[0] = t[root].lazy[0] * t[root].sum[0] +
t[root].lazy[1] * (t[root].r - t[root].l + 1);
return;
}

代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
long long A[MAXN];
struct tnode
{
long long sum[2], lazy[2];
int l, r;
};
struct Segment_Tree
{
tnode t[4 * MAXN];
void init_lazy(int root)
{
t[root].lazy[0] = 1;//乘法把1作为初始值
t[root].lazy[1] = 0;//加法把0作为初始值
}
void union_lazy(int fa, int ch)
{
long long temp[2];
temp[0] = t[fa].lazy[0] * t[ch].lazy[0];//a=a1*a2
temp[1] = t[fa].lazy[0] * t[ch].lazy[1] + t[fa].lazy[1];b//b=a1*b2+b1
t[ch].lazy[0] = temp[0];
t[ch].lazy[1] = temp[1];
}
void cal_lazy(int root)
{
//因为平方和用到了非平方和的项,所以要先更新平方项
t[root].sum[1] = t[root].lazy[0] * t[root].lazy[0] * t[root].sum[1] +
t[root].lazy[1] * t[root].lazy[1] * (t[root].r - t[root].l + 1) +
t[root].lazy[0] * t[root].lazy[1] * 2 * t[root].sum[0];

t[root].sum[0] = t[root].lazy[0] * t[root].sum[0] +
t[root].lazy[1] * (t[root].r - t[root].l + 1);
return;
}
void push_down(int root)
{
if (t[root].lazy[0] != 1 || t[root].lazy[1] != 0)
{
cal_lazy(root);
if (t[root].l != t[root].r)
{
int ch = root << 1;
union_lazy(root, ch);
union_lazy(root, ch + 1);
}
init_lazy(root);
}
}
void update (int root)
{
int ch = root << 1;
push_down(ch);
push_down(ch + 1);
t[root].sum[0] = t[ch].sum[0] + t[ch + 1].sum[0];
t[root].sum[1] = t[ch].sum[1] + t[ch + 1].sum[1];
}
void build(int root, int l, int r)
{
t[root].l = l;
t[root].r = r;
init_lazy(root);
if (l != r)
{
int mid = (l + r) >> 1;
int ch = root << 1;
build(ch, l, mid);
build(ch + 1, mid + 1, r);
update(root);
}
else
{
t[root].sum[0] = A[l];
t[root].sum[1] = A[l] * A[l];
}
}
void change(int root, int l, int r, long long delta, int op)
{
push_down(root);
if (l == t[root].l && r == t[root].r)
{
t[root].lazy[op] = delta;
return;
}
int mid = (t[root].l + t[root].r) >> 1;
int ch = root << 1;
if (r <= mid)change(ch, l, r, delta,op);
else if (l > mid)change(ch + 1, l, r, delta,op);
else {change(ch, l, mid, delta,op); change(ch + 1, mid + 1, r, delta,op);}
update(root);
}
long long sum(int root, int l, int r, int op)
{
push_down(root);
if (t[root].l == l && t[root].r == r)
{
return t[root].sum[op];
}
int mid = (t[root].l + t[root].r) >> 1;
int ch = root << 1;
if (r <= mid)return sum(ch, l, r, op);
else if (l > mid)return sum(ch + 1, l, r, op);
else return sum(ch, l, mid, op) + sum(ch + 1, mid + 1, r, op);
}
};
Segment_Tree ST;
int n, m, op, l, r;
long long x;
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
{
scanf("%lld", &A[i]);
}
ST.build(1, 1, n);
for (int i = 1; i <= m; ++i)
{
scanf("%d %d %d", &op, &l, &r);
if (op >= 3)
{
scanf("%lld", &x);
ST.change(1, l, r, x, op - 3);
}
else
{
printf("%lld\n", ST.sum(1, l, r, op-1));
}
}
return 0;
}


标签:lazy,ch,int,线段,tag,数据结构,root,sum,op
From: https://blog.51cto.com/u_15891800/5887538

相关文章

  • [NEFU 数据结构] 第 2 章 线性表 知识点整理
    [NEFU数据结构]第2章线性表知识点整理阅读须知需求指向:此博客用于应付NEFU数据结构考试,基于题目进行整理,不适合想深入学习数据结构与算法艺术的同学。前置知识:C语言......
  • [NEFU]数据结构 知识点整理和代码实现
    [NEFU]数据结构知识点整理和代码实现Author:2020-计6-zslID:Fishingrod阅读须知需求指向:此博客用于应付NEFU数据结构考试,基于题目进行整理,不适合想深入学习数据结构与算法......
  • [NEFU 数据结构] 第 1 章 绪论 知识点整理
    [NEFU数据结构]第1章绪论知识点整理阅读须知需求指向:此博客用于应付NEFU数据结构考试,基于题目进行整理,不适合想深入学习数据结构与算法艺术的同学。前置知识:C语言参......
  • 数据结构初阶--单链表(讲解+类模板实现)
    单链表概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。值得注意的是:1.链表的在逻辑是连续的,物理上不一......
  • ESP32-S3 复用 JTAG 引脚 为普通引脚之后就识别不到USB口
    目录前言过程RTC时钟配置小结前言把MTCK(GPIO39),接到外部设备之后,在代码里面初始化之后,就不能识别到USB口了(USBJTAG/serialdebugunit)。只要不初始化这个引脚,就能......
  • TemplateSyntaxError: 'staticfiles' is not a registered tag library. Must be one
    django.template.exceptions.TemplateSyntaxError:'staticfiles'isnotaregisteredtaglibrary.Mustbeoneof:在settings.py中添加:TEMPLATES=[{......
  • 数据结构——学习经验
    数据结构各位读者朋友,我是你们的好朋友IT黑铁,最近巩固一下数据结构,大部分适合我当前阶段的知识都已做了简介,而其他只列出了名字的有的是省略点到即可,有些高深的暂未研究。......
  • IDEA编辑器下Vue项目中Element标签出现标黄(Unknown html tag el-form)问题解决方案!
      第一步:检查配置中的依赖项是否勾选,如未勾选则勾上  第二步:检查配置中的Excludes项,如果有被排除的项目则删除  第三步:执行npminstall后,在node_modul......
  • Java常用数据结构
    1、数组数组(Array) 是一种很常见的数据结构。它由相同类型的元素(element)组成,并且是使用一块连续的内存来存储。我们直接可以利用元素的索引(index)可以计算出该元......
  • 数据结构与算法java实现
    什么是数组?(1)数组是计算机中最基本的数据结构之一,我们会用一些名为索引的数字来标识每项数据在数组中的位置。(2)大多数编程语言中索引是从0开始的。(3)数组在内存中是存在......