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

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

  • 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. Problem T: 结构体--学生信息排序

    Problem T: 结构体--学生信息排序 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2219  Solved: 1305[Submit][Sta ...

  2. 重新postgresql出现错误:Problem running post-install step. Installation may not complete correctly. The database cluster initialisation failed.

    以前正常使用的postgresql,今天出现问题:报*.dll错误.百度了一下,只能重新安装 . 在重新安装过程中报:Problem running post-install step. Instal ...

  3. opensue "Have a lot of fun..."的出处

    每次登陆opensuse都会出现“Have a lot of fun...”,觉得奇怪. 通过搜索发现在这是/etc/motd文件中配置的. MOTD(5)                       ...

  4. NFS缓存IO机制

    NFS的缓存IO机制<一> async 参数模式下分析 NFS 默认的mount参数为async,async 参数表示内核不会透传程序的IO请求给sever,对于写IO会延迟执行,积累一定 ...

  5. C++链表简单的应用

    学生管理系统,输入学生的姓名和学号,然后再输出: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include <stdlib ...

  6. ovx openVirtex的阅读文档

    由于flowvisor只有4个版本, 最新更新都是2013年的, 跟底层ovs版本不跟进, 最近斯坦福post一个 ovx, 猜测是flowvisor的加强版, 所以看一下文档说明 文档详见http: ...

  7. C# 使用Epplus导出Excel [5]:样式

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  8. 【贪心 计数】bzoj2006: [NOI2010]超级钢琴

    这么经典的贪心我怎么现在才做啊…… Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐. 这架超级钢琴可以弹奏出n个 ...

  9. destoon 给超级管理员系统权限(管理员管理,日志管理等)

    destoon 后台某些系统权限除了网站创始人之外其他超管事没有权限的,现需要给其他超级管理员添加普通管理员的权限. 1.首先  admin/global.func.php  admin_check函 ...

  10. python-time模块--pickle模块

    目录 time 模块 为什么要有time模块,time模块有什么用? time模块的三种格式 时间戳(timestamp) 格式化时间(需要自己定义格式) 结构化时间(struct-time) 结构化 ...