bzoj 维护序列seq(双标记线段树)
Seq 维护序列seq
Time Limit: 30 Sec Memory Limit: 64 MB
Submit: 4184 Solved: 1518
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7
Sample Output
35
8
HINT
【样例说明】
初始时数列为(1,2,3,4,5,6,7)。
经过第1次操作后,数列为(1,10,15,20,25,6,7)。
对第2次操作,和为10+15+20=45,模43的结果是2。
经过第3次操作后,数列为(1,10,24,29,34,15,16}
对第4次操作,和为1+10+24=35,模43的结果是35。
对第5次操作,和为29+34+15+16=94,模43的结果是8。
测试数据规模如下表所示
数据编号 1 2 3 4 5 6 7 8 9 10
N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
extern "C++"{
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cassert>
#include<cstdlib>
using namespace std;
typedef long long LL;
typedef unsigned u;
typedef unsigned long long ull;
#define mem(x,y) memset(x,y,sizeof(x))
void SI(double &x){scanf("%lf",&x);}
void SI(int &x){scanf("%d",&x);}
void SI(LL &x){scanf("%lld",&x);}
void SI(u &x){scanf("%u",&x);}
void SI(ull &x){scanf("%llu",&x);}
void SI(char *s){scanf("%s",s);} void PI(int x){printf("%d",x);}
void PI(double x){printf("%lf",x);}
void PI(LL x){printf("%lld",x);}
void PI(u x){printf("%u",x);}
void PI(ull x){printf("%llu",x);}
void PI(char *s){printf("%s",s);} #define NL puts("");
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
const int INF=0x3f3f3f3f;
const int MAXN=;
LL tree[MAXN << ];
LL lazy[MAXN << ];
LL lazy2[MAXN << ];
LL P;
}
void pushup(int root){
tree[root] = tree[ll] + tree[rr];
tree[root] %= P;
}
void pushdown(int root,int x){
if(lazy[root] != ){
lazy[ll] *= lazy[root];
lazy[rr] *= lazy[root]; lazy2[ll] *= lazy[root];
lazy2[rr] *= lazy[root];
// tree[ll]=lazy[root]*(mid-l+1);
// tree[rr]=lazy[root]*(r-mid);
tree[ll] *= lazy[root];
tree[rr] *= lazy[root]; lazy[ll] %= P;
lazy[rr] %= P;
tree[ll] %= P;
tree[rr] %= P;
lazy2[ll] %= P;
lazy2[rr] %= P; lazy[root] = ;
}
if(lazy2[root]){
lazy2[ll] += lazy2[root];
lazy2[rr] += lazy2[root]; lazy2[ll] %= P;
lazy2[rr] %= P; tree[ll] += lazy2[root] * (x - (x >> ) ) % P;
tree[rr] += lazy2[root] * (x >> ) % P; tree[ll] %= P;
tree[rr] %= P; lazy2[root] = ;
}
}
void build(int root,int l,int r){
int mid = (l + r) >> ;
lazy[root] = ;
lazy2[root] = ;
if(l == r){
SI(tree[root]);
return ;
}
build(lson);
build(rson);
pushup(root);
}
void update(int root,int l,int r,int L,int R,int C,int t){
if(l >= L && r <= R){
if(t == ){
lazy[root] *= C;
tree[root] *= C;
lazy2[root] *= C;
lazy2[root] %= P;
lazy[root] %= P;
tree[root] %= P;
}
else if(t == ){
lazy2[root] += C % P;
tree[root] += C * (r - l +) % P;
lazy2[root] %= P;
tree[root] %= P;
}
return ;
}
int mid = (l + r) >> ;
pushdown(root,r - l + );
if(mid >= L)
update(lson,L,R,C,t);
if(mid < R)
update(rson,L,R,C,t);
pushup(root);
}
LL query(int root,int l,int r,int L,int R){
int mid = (l + r) >> ;
if(l >= L && r <= R){
return tree[root];
}
pushdown(root,r - l + );
LL ans = ;
if(mid >= L)
ans += query(lson,L,R);
if(mid < R)
ans += query(rson,L,R);
return ans;
}
int main(){
// assert(true);
int N,M;
while(~scanf("%d%lld",&N,&P)){
build(,,N);
SI(M);
int a,l,r,c;
while(M--){
SI(a);SI(l);SI(r);
if(a == ){
printf("%lld\n",query(,,N,l,r) % P);
}
else{
SI(c);
update(,,N,l,r,c,a);
}
}
}
return ;
}
bzoj 维护序列seq(双标记线段树)的更多相关文章
- bzoj 1798 [Ahoi2009]Seq 维护序列seq(线段树+传标)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1798 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...
- 【BZOJ】1798: [Ahoi2009]Seq 维护序列seq(线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1798 之前写了个快速乘..........................20多s...... 还好 ...
- BZOJ 1798: [Ahoi2009]Seq 维护序列seq (线段树乘法加法的混合操作)
题目:点击打开链接 大意:一个数组.三个操作.第一种是区间[a,b]每一个数乘乘,另外一种是区间[a,b]每一个数加c,第三种是查询[a,b]区间的和并对p取摸. 两种操作就不能简单的仅仅往下传 ...
- _bzoj1798 [Ahoi2009]Seq 维护序列seq【线段树 lazy tag】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 注意,应保证当前节点维护的值是正确的,lazy tag只是一个下传标记,在下传时应即时 ...
- AHOI 2009 (BZOJ1798)维护序列 seq (线段树好题?)
我是不会说这个题很坑的.. 改了一晚上... // by SiriusRen #include <cstdio> #define N 150000 #define LSON l,mid,l ...
- P2023 [AHOI2009]维护序列 题解(线段树)
题目链接 P2023 [AHOI2009]维护序列 解题思路 线段树板子.不难,但是...有坑.坑有多深?一页\(WA\). 由于乘法可能乘\(k=0\),我这种做法可能会使结果产生负数.于是就有了这 ...
- BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )
线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...
- 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 老师交给小可可一个维护数列的任务,现在小可 ...
随机推荐
- UESTC_贪吃蛇 CDOJ 709
相信大家都玩过贪吃蛇游戏吧. 在n×m的迷宫中,有着一条长度不超过9的贪吃蛇,它已经将所有的食物吃光了,现在的目标是移动到出口. 它走的时候不能碰到自己的身体,也不能碰到墙壁.(如果贪吃蛇的长度> ...
- 【转】通过 ulimit 改善系统性能
概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一 ...
- [置顶] Android开发实战记录(三)---HelloWorld
1.新建Android项目,选择Android Project,然后Next 2.填写项目名称HelloWorld然后next,这里注意下,Java开发的命名规范 3.选择Android SDK版本, ...
- 算法设计手冊(第2版)读书笔记, Springer - The Algorithm Design Manual, 2ed Steven S.Skiena 2008
The Algorithm Design Manual, 2ed 跳转至: 导航. 搜索 Springer - The Algorithm Design Manual, 2ed Steven S.Sk ...
- hdu1753()模拟大型实景数字相加
标题信息: 手动求大的实数在一起, pid=1753">http://acm.hdu.edu.cn/showproblem.php?pid=1753 AC代码: /** *大实数相加 ...
- oc内存管理总结(一)
**内存管理 问题 1.什么是ios内存管理? 就是在对象不再被使用的时候,把它即时的从内存中清除掉 2.为什么要使用内存管理? 1.严格的内存管理,能够是我们的应用程在性能上有很大的提高 2.如果忽 ...
- C#操作AD的例子
一下连接中包含了使用c#对AD操作的各种列子 http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-D ...
- Spring Annotation注解进行aop的学习
使用Maven管理项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- CodeForces 222D - Olympiad
第一行给出两个个数字k和n,第二三行分别有k个数字,求将第二.三行之间的数字相互组合,求最多有多少个组合的和不小于n 纯粹暴力 #include <iostream> #include & ...
- 利用html5中的localStorage获取网页被访问的次数
利用html5中的localStorage获取网页被访问的次数 <!DOCTYPE html> <html> <head> <meta charset=&qu ...