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 ...
随机推荐
- [转]maven项目部署到tomcat
其实maven项目部署到tomcat的方式很多,我从一开始的打war包到tomcat/webapps目录,到使用tomcat-maven插件,到直接使用servers部署,一路来走过很多弯路. 下面就 ...
- 删除表中一个字段的SQL语句
1.删除没有默认值的列:alter table Test drop COLUMN BazaarType 2.删除有默认值的列:先删除约束(默认值)alter table Test DROP CONST ...
- 复杂软件的考虑点与UITableView
对象的要素.组织.整体情况: 对象的生成步骤.生成的时间节点. 考虑UITableview.
- [论文理解]SSD:Single Shot MultiBox Detector
SSD:Single Shot MultiBox Detector Intro SSD是一套one-stage算法实现目标检测的框架,速度很快,在当时速度超过了yolo,精度也可以达到two-stag ...
- Python中Numpy mat的使用
前面介绍过用dnarray来模拟,但mat更符合矩阵,这里的mat与Matlab中的很相似.(mat与matrix等同) 基本操作 >>> m= np.mat([1,2,3]) #创 ...
- 探讨 JS 的面向对象中继承的那些事
最近学了 JS 的面向对象,这篇文章主要是探讨 JS 的面向对象中继承的那些事. JS中继承的特点: 1.子类继承父类: 2.子类可以用父类的方法和属性 3.子类的改变可以不影响父类 下面用一个例子来 ...
- 转 Spring Security 简介
https://blog.csdn.net/xlecho/article/details/80026527 Spring Security 简介 2018年04月21日 09:53:02 阅读数:13 ...
- cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
参考 http://blog.csdn.net/mazicwong/article/details/54946952 1.到https://curl.haxx.se/ca/cacert.pem复制下 ...
- destoon 信息发布表单提交验证
sell 模块的form表单如下: <form method="post" id="dform" action="?" target= ...
- MySQL如何复制一个表
MySQL如何复制一个表 1 复制 employee 表 => employee2 () create table employee2 like employee () insert into ...