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 ...
随机推荐
- Unity runtime性能分析器
一. Profiler: 1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体 ...
- MySQL8 Authentication plugin 'caching_sha2_password' cannot be loaded
这是因为mysql8 和以前密码的验证方式不同,可以先从命令行进入 MySQL -uroot -p 然后输入 ALTER USER 'root'@'localhost' IDEN ...
- PWD简介与妙用(一个免费、随时可用的Docker实验室)
转载自 https://baiyue.one/archives/472.html 本文介绍下 PWD 的历史,并依据本站最近学习心得,经过多次尝试,终于打通了 Docker 与常规宝塔面板搭建,因此, ...
- 【转】瓜娃(guava)的API快速熟悉使用
http://www.cnblogs.com/snidget/archive/2013/02/05/2893344.html 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: I ...
- WINDOWS-API:关于线程 GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId
{返回当前线程的虚拟句柄} GetCurrentThread: THandle; {返回当前线程 ID} GetCurrentThreadId: DWORD; {返回当前进程的虚拟句柄} GetCur ...
- Word2vec资料
Word2vec 很好的资料 Word2Vec-知其然知其所以然 https://www.zybuluo.com/Dounm/note/591752 Word2Vec数学原理讲解 http://w ...
- c#List结合IEqualityComparer求交集
List元素类: public class MultiPointSearchingRet { public int ID { get; set; } public string PlateNumber ...
- 使用HTML5语义标签时要注意的问题
header,nav,section,article,aside,figue,figcaption,footer以上这些标签(除figcaption标签外)都是块级标签,为了让这些标签及元素在所有的浏 ...
- stataic 变量
static 是静态变量的的类型说明符 静态变量属于静态存储方式,(外部变量也是静态存储方式) 静态的局部变量 静态局部变量属于静态存储方式,它具有以下特点: (1)静态局部变量在函数内定义 它的生存 ...
- OI杂记
从今天开始记录一下为数不多天的OI历程 8.25 上 今天举行了难得的五校联考,模拟noip,题目的解压密码竟然是$aKnoIp2o18$,对你没有看错!!! 7:50老师?啊啊啊啊,收不到题目啊,还 ...