BZOJ-1798 维护序列
线段树。支持区间加、区间乘、区间查询和。
标记下移还有取模要注意。
var
n,p,q,i,s,t:longint;
a:int64;
num,n1,n2,n3:array[0..500000] of int64;
procedure build(o,l,r:longint);
var m,i:longint;
begin
m:=(l+r) div 2;
if l=r then
begin
n1[o]:=num[l];
n2[o]:=num[l];
end
else
begin
build(o*2,l,m);
build(o*2+1,m+1,r);
end;
if o>1 then n1[o div 2]:=(n1[o div 2]+n1[o]) mod p;
end;
procedure add(o,l,r:longint);
var m,i:longint;
begin
m:=(l+r) div 2;
if l<>r then
begin
n1[o*2]:=(n1[o*2]*n3[o]+n2[o]*((m-l+1) mod p)) mod p;
n2[o*2]:=(n2[o*2]*n3[o]+n2[o]) mod p;
n3[o*2]:=(n3[o*2]*n3[o]) mod p;
n1[o*2+1]:=(n1[o*2+1]*n3[o]+n2[o]*((r-m) mod p)) mod p;
n2[o*2+1]:=(n2[o*2+1]*n3[o]+n2[o]) mod p;
n3[o*2+1]:=(n3[o*2+1]*n3[o]) mod p;
n2[o]:=0;n3[o]:=1;
end;
if (s<=l)and(r<=t) then
begin
n1[o]:=(n1[o]+a*((r-l+1) mod p)) mod p;
n2[o]:=(n2[o]+a) mod p;
end
else
begin
if s<m+1 then add(o*2,l,m);
if m<t then add(o*2+1,m+1,r);
n1[o]:=(n1[o*2]+n1[o*2+1]) mod p;
end;
end;
procedure che(o,l,r:longint);
var m,i:longint;
begin
m:=(l+r) div 2;
if l<>r then
begin
n1[o*2]:=(n1[o*2]*n3[o]+n2[o]*((m-l+1) mod p)) mod p;
n2[o*2]:=(n2[o*2]*n3[o]+n2[o]) mod p;
n3[o*2]:=(n3[o*2]*n3[o]) mod p;
n1[o*2+1]:=(n1[o*2+1]*n3[o]+n2[o]*((r-m) mod p)) mod p;
n2[o*2+1]:=(n2[o*2+1]*n3[o]+n2[o]) mod p;
n3[o*2+1]:=(n3[o*2+1]*n3[o]) mod p;
n2[o]:=0;n3[o]:=1;
end;
if (s<=l)and(r<=t) then
begin
n1[o]:=(n1[o]*a) mod p;
n2[o]:=(n2[o]*a) mod p;
n3[o]:=(n3[o]*a) mod p;
end
else
begin
if s<m+1 then che(o*2,l,m);
if m<t then che(o*2+1,m+1,r);
n1[o]:=(n1[o*2]+n1[o*2+1]) mod p;
end;
end;
function que(o,l,r:longint):int64;
var m,i:longint;
begin
m:=(l+r) div 2;
que:=0;
if l<>r then
begin
n1[o*2]:=(n1[o*2]*n3[o]+n2[o]*((m-l+1) mod p)) mod p;
n2[o*2]:=(n2[o*2]*n3[o]+n2[o]) mod p;
n3[o*2]:=(n3[o*2]*n3[o]) mod p;
n1[o*2+1]:=(n1[o*2+1]*n3[o]+n2[o]*((r-m) mod p)) mod p;
n2[o*2+1]:=(n2[o*2+1]*n3[o]+n2[o]) mod p;
n3[o*2+1]:=(n3[o*2+1]*n3[o]) mod p;
n2[o]:=0;n3[o]:=1;
end;
if (s<=l)and(r<=t) then que:=n1[o]
else
begin
if s<m+1 then que:=(que+que(o*2,l,m)) mod p;
if m<t then que:=(que+que(o*2+1,m+1,r)) mod p;
end;
end;
begin
read(n,p);
for i:=1 to n do read(num[i]);
for i:=1 to n do num[i]:=num[i] mod p;
for i:=1 to 3*n do n3[i]:=1;
build(1,1,n);
read(q);
for i:=1 to q do
begin
read(a);
case a of
1:begin
read(s,t,a);
a:=a mod p;
che(1,1,n);
end;
2:begin
read(s,t,a);
a:=a mod p;
add(1,1,n);
end;
3:begin
read(s,t);
writeln(que(1,1,n));
end;
end;
end;
end.
我写的有些麻烦。。。
BZOJ-1798 维护序列的更多相关文章
- bzoj 1798 维护序列seq
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 题解: 高级一点的线段树,加上了区间乘法运算,则需要增加一个数组mulv记录乘的因数 ...
- bzoj 1798 维护序列seq 线段树
裸的线段树,注意标签下放就行了 多么痛的领悟,一定要开int64 /************************************************************** Pro ...
- [BZOJ 1500] 维护序列
Link: BZOJ 1500 传送门 Solution: 可能平衡树维护序列的所有操作都在这了吧…… 对序列的维护$fhq treap$和$Splay$都能做 有几个注意点: 1.维护序列时始终记得 ...
- BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )
线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...
- bzoj 1798: [Ahoi2009]Seq 维护序列seq (线段树 ,多重标记下放)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 7773 Solved: 2792[Submit ...
- bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeO ...
- Bzoj 1798: [Ahoi2009]Seq 维护序列seq(线段树区间操作)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小可 ...
- 【BZOJ】【1798】【AHOI2009】Seq维护序列
线段树 属于线段树中级应用吧…… 要打两种标记:乘法和加法标记.一开始我想着可以像只有加法标记那样,永不下传,查询的时候依次累加就好了.后来发现不会写……只好每次update的时候……遇到标记!下传! ...
- 1798: [Ahoi2009]Seq 维护序列seq
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 2930 Solved: 1087[Submit ...
- bzoj 维护序列seq(双标记线段树)
Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 4184 Solved: 1518[Submit][Status][Discus ...
随机推荐
- ansys-表格
转自http://blog.sina.com.cn/s/blog_833dee820102xwb3.html ANSYS中表格数组的定义及使用举例 ANSYS中会有许多的参数数据,这些参数的形成后要放 ...
- Ajax获取服务器响应头部信息
$.ajax({ type: 'HEAD', // 获取头信息,type=HEAD即可 url : window.location.href, complete: function( xhr,data ...
- win10中打开SQL Server 2008 的SQL Server配置管理器方法
win10找不到SQL Server配置管理器 搜索 SQLServerManager10.msc,或者运行文件:“C:\Windows\SysWOW64\SQLServerManager10.msc ...
- Framework的简单创建
如果你想将你开发的控件与别人分享,一种方法是直接提供源代码文件.然而,这种方法并不是很优雅.它会暴露所有的实现细节,而这些实现你可能并不想开源出来.此外,开发者也可能并不想看到你的所有代码,因为他们可 ...
- 基于matlab的蓝色车牌定位与识别---定位
接着昨天的工作继续.定位的过程有些是基于车牌的颜色进行定位的,自己则根据数字图像一些形态学的方法进行定位的. 合着代码进行相关讲解. 1.相对彩色图像进行灰度化,然后对图像进行开运算.再用小波变换获取 ...
- 【启发式拆分】bzoj5200: [NWERC2017]Factor-Free Tree
和bzoj4059: [Cerc2012]Non-boring sequences非常相似 Description 一棵Factor-Free Tree是指一棵有根二叉树,每个点包含一个正整数权值,且 ...
- python中文件操作的基本方法
在python中对一个文件进行操作,分为三大步:打开,操作,关闭 首先创建一个文件hello,里面内容为hello world 一.打开一个文件 1.#open(‘文件名或文件路径’,‘操作模式’,文 ...
- 【原创】关于高版本poi autoSizeColumn方法异常的情况
之前使用的3.9版本,autoSizeColumn方法一切正常,现在切换到了3.15版本这个方法就出先了问题,问题如下,无法自动追踪所有的列. Exception in thread "ma ...
- 【转】git bash here 右键菜单失效后的修复方法
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\git_shell] @="Git Ba&sh Here" [ ...
- 光学字符识别OCR-8 综合评估
数据验证 尽管在测试环境下模型工作良好,但是实践是检验真理的唯一标准.在本节中,我们通过自己的模型,与京东的测试数据进行比较验证. 衡量OCR系统的好坏有两部分内容:(1)是否成功地圈 ...