当前位置:网站首页>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.
边栏推荐
猜你喜欢
荧光磷脂PEG衍生物之一磷脂-聚乙二醇-荧光素,Fluorescein-PEG-DSPE
小程序对接企业微信客服
双目立体视觉笔记(二)
"Lonely Walking on the Moon" is a powerful medicine, it can't cure the internal friction of happy twist
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
5 cloud security management strategies enterprises should implement
持续交付(二)PipeLine基本使用
CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔,可用于改善循环时间
nVisual二次开发——第二章 nVisual API操作指南Swagger使用
[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)
随机推荐
一文梳理NLP主要模型发展脉络
Motion Rule (16)-Union Check Basic Questions-Relations
MFC的相机双目标定界面设计
密码设置有关方法:不能相同字母,不能为连续字符
ROS设置plugin插件
MySQL性能指标TPS\QPS\IOPS如何压测?
k8s上安装mysql
项目里的各种配置,你都了解吗?
Various problems with npm install
redis未授权访问漏洞【vulhub靶场】复现
RK1126编译gdb 板子上gdb调试程序
Geoffrey Hinton:深度学习的下一个大事件
c#之winform(软件开发)
面试官:说一下NIO和BIO的区别
Motion Rule (16)-Union Check Basic Questions-Grid Game
"Social Enterprises Conducting Civilian Personnel Training Specifications" group standard on the shelves of Xinhua Bookstore
yum 查看已经安装过的包并卸载
SSRF-服务器端请求伪造-相关知识
接到“网站动态换主题”的需求,我是如何踩坑的
Unity 3D模型展示框架篇之资源打包、加载、热更(Addressable Asset System | 简称AA)