线段树。支持区间加、区间乘、区间查询和。

标记下移还有取模要注意。

  • 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 维护序列的更多相关文章

  1. bzoj 1798 维护序列seq

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 题解: 高级一点的线段树,加上了区间乘法运算,则需要增加一个数组mulv记录乘的因数 ...

  2. bzoj 1798 维护序列seq 线段树

    裸的线段树,注意标签下放就行了 多么痛的领悟,一定要开int64 /************************************************************** Pro ...

  3. [BZOJ 1500] 维护序列

    Link: BZOJ 1500 传送门 Solution: 可能平衡树维护序列的所有操作都在这了吧…… 对序列的维护$fhq treap$和$Splay$都能做 有几个注意点: 1.维护序列时始终记得 ...

  4. BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )

    线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...

  5. bzoj 1798: [Ahoi2009]Seq 维护序列seq (线段树 ,多重标记下放)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 7773  Solved: 2792[Submit ...

  6. bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeO ...

  7. Bzoj 1798: [Ahoi2009]Seq 维护序列seq(线段树区间操作)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小可 ...

  8. 【BZOJ】【1798】【AHOI2009】Seq维护序列

    线段树 属于线段树中级应用吧…… 要打两种标记:乘法和加法标记.一开始我想着可以像只有加法标记那样,永不下传,查询的时候依次累加就好了.后来发现不会写……只好每次update的时候……遇到标记!下传! ...

  9. 1798: [Ahoi2009]Seq 维护序列seq

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 2930  Solved: 1087[Submit ...

  10. bzoj 维护序列seq(双标记线段树)

    Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 4184  Solved: 1518[Submit][Status][Discus ...

随机推荐

  1. Unity四元素运用之风向标跟随箭头

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class WindVane ...

  2. code Gym 100500D T-shirts(暴力)

    因为只能买一次,暴力枚举一下买的衣服的大小. #include<cstdio> #include<map> #include<algorithm> using na ...

  3. wine卸载

    Wine手动卸载,出现殘留,导致安装其他软件也不成功. 错误如下: 正在读取软件包列表... 完成正在分析软件包的依赖关系树       正在读取状态信息... 完成       您也许需要运行“ap ...

  4. 修改Windows默认调试器

    程序崩溃时,系统会弹窗让你选择是否进行调试,可以设置系统默认调试器. 注册表位置: HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVe ...

  5. *运算和&运算

    /* &:取地址运算符 *:指针运算符(或称为间接运算符),取指针所指向的对象的内容 */ int a,b; int *pointer_1, *pointer_2; pointer_1 = & ...

  6. 学习笔记(二):使用 TensorFlow 的起始步骤(First Steps with TensorFlow)

    目录 1.工具包 TensorFlow 张量 (Tensor) 图 (graph) TensorBoard 2.tf.estimator API Estimator 预创建的 Estimator (p ...

  7. 【转】C++后台开发应该读的书

    转载自http://www.cnblogs.com/balloonwj/articles/9094905.html 作者 左雪菲 根据我的经验来谈一谈,先介绍一下我的情况,坐标上海,后台开发(也带团队 ...

  8. 【mac】【nginx】开机重启

    homebrew.mxcl.nginx.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  9. [译]The Python Tutorial#4. More Control Flow Tools

    [译]The Python Tutorial#More Control Flow Tools 除了刚才介绍的while语句之外,Python也从其他语言借鉴了其他流程控制语句,并做了相应改变. 4.1 ...

  10. 用python编写简易登录接口

    需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 用户3次认证失败后,退出程序,再次启动程序尝试登陆时,还是锁定状态 下面是我写的代码,如果有BUG或者不 ...