当前位置:网站首页>BZOJ 1798 维护序列 (多校连萌,对线段树进行加乘混合操作)
BZOJ 1798 维护序列 (多校连萌,对线段树进行加乘混合操作)
2022-08-04 13:15:00 【51CTO】
题目地址:http://www.lydsy.com/JudgeOnline/problem.php?id=1798
思路:对一段区间的数进行加乘混合操作,思路还是挺巧的,举个例子
sum[rt]为一段区间的和,对区间+x再乘以p(sum[rt]+x)*p=sum[rt]*p+x*p可以写成对这个区间乘以p,每次乘的时候对a[rt]乘p,这个思路很巧
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const
int
inf
=
0x3f3f3f3f;
//1061109567
typedef
long
long
LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using
namespace
std;
const
int
maxn
=
100010;
LL
add[
maxn
<<
2],
mul[
maxn
<<
2],
sum[
maxn
<<
2];
LL
p;
void
pushup(
int
rt)
{
sum[
rt]
= (
sum[
rt
<<
1]
+
sum[
rt
<<
1
|
1])
%
p;
}
void
pushdown(
int
rt,
int
m)
{
if(
add[
rt]
==
0
&&
mul[
rt]
==
1)
return;
add[
rt
<<
1]
= (
add[
rt
<<
1]
*
mul[
rt]
+
add[
rt])
%
p;
//前面包括的是乘,后面包括的是加和先加后乘的
//假设sum[rt]为一段和,(sum[rt]+x)*p,最后的结果只是这一段的和乘以p加上x*p
add[
rt
<<
1
|
1]
= (
add[
rt
<<
1
|
1]
*
mul[
rt]
+
add[
rt])
%
p;
mul[
rt
<<
1]
= (
mul[
rt
<<
1]
*
mul[
rt])
%
p;
mul[
rt
<<
1
|
1]
= (
mul[
rt
<<
1
|
1]
*
mul[
rt])
%
p;
sum[
rt
<<
1]
= (
sum[
rt
<<
1]
*
mul[
rt]
+ (
m
- (
m
>>
1))
*
add[
rt])
%
p;
//注意里面的参数
sum[
rt
<<
1
|
1]
= (
sum[
rt
<<
1
|
1]
*
mul[
rt]
+ (
m
>>
1)
*
add[
rt])
%
p;
add[
rt]
=
0,
mul[
rt]
=
1;
}
void
updata(
int
L,
int
R,
int
value,
int
l,
int
r,
int
rt,
int
op)
{
if(
l
>=
L
&&
r
<=
R)
{
if(
op
==
1)
{
add[
rt]
=
add[
rt]
*
value
%
p;
mul[
rt]
=
mul[
rt]
*
value
%
p;
sum[
rt]
=
sum[
rt]
*
value
%
p;
}
else
{
add[
rt]
= (
add[
rt]
+
value)
%
p;
sum[
rt]
= (
sum[
rt]
+ (
r
-
l
+
1)
* (
LL)
value)
%
p;
}
return;
}
pushdown(
rt,
r
-
l
+
1);
int
m
= (
l
+
r)
>>
1;
if(
L
<=
m)
updata(
L,
R,
value,
lson,
op);
if(
R
>
m)
updata(
L,
R,
value,
rson,
op);
pushup(
rt);
}
void
build(
int
l,
int
r,
int
rt)
{
add[
rt]
=
0,
mul[
rt]
=
1;
if(
l
==
r)
{
scanf(
"%lld",
&
sum[
rt]);
return;
}
int
m
= (
l
+
r)
>>
1;
build(
lson);
build(
rson);
pushup(
rt);
}
LL
query(
int
L,
int
R,
int
l,
int
r,
int
rt)
{
if(
l
>=
L
&&
r
<=
R)
return
sum[
rt];
pushdown(
rt,
r
-
l
+
1);
int
m
= (
l
+
r)
>>
1;
LL
ans
=
0;
if(
L
<=
m)
ans
+=
query(
L,
R,
lson);
if(
R
>
m)
ans
+=
query(
L,
R,
rson);
return
ans
%
p;
}
int
main()
{
int
n;
while(
scanf(
"%d%lld",
&
n,
&
p)
!=
EOF)
{
build(
1,
n,
1);
int
m;
scanf(
"%d",
&
m);
for(
int
i
=
0;
i
<
m;
i
++)
{
int
x,
left,
right,
value;
scanf(
"%d",
&
x);
if(
x
<
3)
{
scanf(
"%d%d%d",
&
left,
&
right,
&
value);
updata(
left,
right,
value,
1,
n,
1,
x);
}
else
{
scanf(
"%d%d",
&
left,
&
right);
printf(
"%lld\n",
query(
left,
right,
1,
n,
1));
}
}
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
边栏推荐
猜你喜欢

RobotFramework二次开发(一)

到底什么是真正的HTAP?

MySQL-数据类型

项目里的各种配置,你都了解吗?

Is the code more messy?That's because you don't use Chain of Responsibility!

持续交付(四)Jenkins多线程任务执行

为什么密码云服务平台是云时代的必然之选?

未来已来,只是尚未流行
![LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road](/img/fd/c827608b96f678a67c7e920c51d8c5.png)
LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road

【VSCode】一文详解vscode下安装vim后无法使用Ctrl+CV复制粘贴 使用Vim插件的配置记录
随机推荐
odoo13 note point
ROS设置plugin插件
LeetCode_643_子数组的最大平均数Ⅰ
Ultra-QuickSort
“蔚来杯“2022牛客暑期多校训练营4 N
Cockpit human-computer interaction "undercurrent", voice "down", multi-modal "up"
HDU1580 输出先手能取的方案数
Two years of independent development experience Programmers tell us the experience of making money (listen to the masters who really make money)
面试官:说一下NIO和BIO的区别
未来已来,只是尚未流行
绩效考核带给员工的不能只是压力
基于双层共识控制的直流微电网优化调度(Matlab代码实现)
博尔赫斯-诗中的经典语段
用过Apifox这个API接口工具后,确实感觉postman有点鸡肋......
yum 查看已经安装过的包并卸载
Billboard
Diffusion Models:生成扩散模型
【VSCode】一文详解vscode下安装vim后无法使用Ctrl+CV复制粘贴 使用Vim插件的配置记录
云原生Devops 的实现方法
到底什么是真正的HTAP?