当前位置:网站首页>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.
边栏推荐
- 并发刺客(False Sharing)——并发程序的隐藏杀手
- Control CD-ROM with VbScript
- "Social Enterprises Conducting Civilian Personnel Training Specifications" group standard on the shelves of Xinhua Bookstore
- 【LeetCode】1403. 非递增顺序的最小子序列
- Diffusion Models:生成扩散模型
- LeetCode_424_替换后的最长重复字符
- Escape character is ‘^]’什么意思?怎么使用telnet
- rpm安装提示error: XXX: not an rpm package (or package manifest):
- MFC的相机双目标定界面设计
- 视觉SLAM十四讲学习笔记 第7讲 视觉里程计
猜你喜欢
随机推荐
03 多线程与高并发 - ReentrantLock 源码解析
LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
SCA兼容性分析工具(ORACLE/MySQL/DB2---&gt;MogDB/openGauss/PostgreSQL)
面试官:连 INSERT INTO SET 都不知道怎么用,你这3年都干些什么了?
并发刺客(False Sharing)——并发程序的隐藏杀手
“蔚来杯“2022牛客暑期多校训练营2 G、J、K
MFC的相机双目标定界面设计
PAT甲级:1038 Recover the Smallest Number
使用COLMAP初步三维重建
Valentine's Day Romantic 3D Photo Wall [with source code]
【解决方案 三十一】Navicat数据库结构同步
Ceres库运行,模板内报内存冲突问题。(已解决)
跨链桥已成行业最大安全隐患 为什么和怎么办
yum 查看已经安装过的包并卸载
Do you understand the various configurations in the project?
leetcode 48. Rotate Image 旋转图像(Medium)
oracle sql中根据条件判断是否插入数据
Ultra-QuickSort
d不要直接用转串
项目里的各种配置,你都了解吗?