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 ...
随机推荐
- Typescript的优势
TypeScript是一种由微软开发的自由开源的编程语言,他是JavaScript的一个超集,扩展了JavaScript的语法. 优势: 一.Angular2框架的开发语言 Angular2是一款开源 ...
- Python中Numpy ndarray的使用
本文主讲Python中Numpy数组的类型.全0全1数组的生成.随机数组.数组操作.矩阵的简单运算.矩阵的数学运算. 尽管可以用python中list嵌套来模拟矩阵,但使用Numpy库更方便. 定义数 ...
- WPF知识点全攻略11- 命令(Command)
先看一下命令的简单使用: <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Cut ...
- VA助手添加扩展文件后缀名
Allow C/C++ files with a non-standard extension Follow these steps to make Visual Assist consider as ...
- EWS code return Error : Request failed. The remote server returned an error: (403) Forbidden OR (401) Unauthorized
Following is my code. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1 ...
- cocos2dx 加密spine文件遇到的问题(暂时没有解决方法)
今天我研究了一下加密spine动画的加密的方法,图片肯定要加密的,所以我只选择加密图片,另外的一个altas文件和json文件就不做加密打算. 我的思路是通过TexturePacker打包成加密的文件 ...
- cocos2d-x中解决暂停并保存画面和开始的功能
1.调用所有对象的pauseSchedulerAndActions().太麻烦,不太现实,而且有很多对象不易获取. 2.CCDirector::sharedirector()->pause(). ...
- ubuntu下RedisDesktopManager的安装,redis可视化工具
官方网站:https://redisdesktop.com/download 一句命令行解决: sudo snap install redis-desktop-manager 或者直接通过软件管理中心 ...
- Eclipse使用Mybatis-Generator插件
Mybatis-Generator插件极大地方便了我们的开发效率,不用每张表每个字段人工去敲,所以本文介绍使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件 版权 ...
- springboot maven 多环境配置
1.使用Intellij IDEA创建Spring Boot和Maven项目 2.Spring Boot项目下application.yaml(yaml支持中文)或者application.prope ...