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

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

  • 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. inner join 和 left join 的区别

    1.left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表 ...

  2. 扫雷游戏 NOIP(入门)

    题目描述: 扫雷游戏是一款十分经典的单机小游戏.它的精髓在于,通过已翻开格子所提示的周围格地雷数,来判断未翻开格子里是否是地雷. 现在给出n行m列的雷区中的地雷分布,要求计算出每个非地雷格的周围格地雷 ...

  3. 点击tr实现选择checkbox功能,点击checkobx的时候阻止冒泡事件, jquery给checkbox添加checked属性或去掉checked属性不能使checkobx改变状态

    给tr添加点击事件,使用find方法查找tr下的所有层级的元素,children只查找下一层级的元素,所以使用find.find的返回值为jquery对象,在这个项目中不知道为什么使用jquery给c ...

  4. PHP做ERP, CRM, CMS系统需要注意哪些地方

    php作为二次开发弱类型语言, 可读性, 可视度都是比较高的. 在很多人眼里, 也许php只能做一些web应用开发, 比如某个公司的网站, 某个公司的网站后台, 其实,我可以告诉大家, php不比任何 ...

  5. loc与iloc函数的使用

    Pandas中loc和iloc函数用法详解(源码+实例)  https://blog.csdn.net/w_weiying/article/details/81411257 Pandas中loc,il ...

  6. list_add_tail()双向链表实现分析

    struct list_head { struct list_head *next, *prev; }; list_add_tail(&buf->vb.queue, &vid-& ...

  7. 查看 EGLIBC 版本

    $ ldd --versionldd (Debian EGLIBC 2.13-38+deb7u1) 2.13

  8. pidgin中使用QQ

    安装     实现的功能:   可以发送静态表情 可以发送动态表情 可以发图片? 可以添加好友 可以添加群?  

  9. selenium2等待元素加载

    1.硬性等待 Thread.sleep(8000); 所谓的硬性等待就是,执行完相应操作就等待我设置的8s.无论网速快与慢,网速快的话,也许5s就打开网页了,可是程序必须接着等待剩下的3秒. 网速慢的 ...

  10. Python虚拟机函数机制之无参调用(一)

    PyFunctionObject对象 在Python中,任何一个东西都是对象,函数也不例外.函数这种抽象机制,是通过一个Python对象——PyFunctionObject来实现的 typedef s ...