bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和
1798: [Ahoi2009]Seq 维护序列seq
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=1798
Description
Input
Output
Sample Input
7 43
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
题意
题解:
啊,就是一个傻逼线段树,区间乘法+区间加法,都扔给一个updata就好,然后最后搞一搞
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=, k=; char c=getchar(); for(; c<''||c>''; c=getchar()) if(c=='-') k=-; for(; c>=''&&c<=''; c=getchar()) r=r*+c-''; return k*r; } const int N=;
#define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, mid, lc
#define rson mid+1, r, rc int n, MD; struct node {
int sum, add, mul;
void upd(int a, int m, int len) {
add=((ll)add*m+a)%MD;
mul=((ll)mul*m)%MD;
sum=((ll)sum*m+(ll)a*len)%MD;
}
}t[N<<];
void pushdown(int x, int len) {
if(t[x].add!= || t[x].mul!=)
t[lc].upd(t[x].add, t[x].mul, (len-(len>>))),
t[rc].upd(t[x].add, t[x].mul, len>>),
t[x].add=, t[x].mul=;
}
void pushup(int x) { t[x].sum=(t[lc].sum+t[rc].sum)%MD; }
void build(int l, int r, int x) {
t[x].add=;
t[x].mul=;
if(l==r) { t[x].sum=getint(); return; }
int mid=MID;
build(lson); build(rson);
pushup(x);
}
void update(int l, int r, int x, int L, int R, int add, int mul) {
if(L<=l && r<=R) { t[x].upd(add, mul, r-l+); return; }
pushdown(x, r-l+);
int mid=MID;
if(L<=mid) update(lson, L, R, add, mul);
if(mid<R) update(rson, L, R, add, mul);
pushup(x);
}
int query(int l, int r, int x, int L, int R) {
if(L<=l && r<=R) return t[x].sum;
pushdown(x, r-l+);
int mid=MID, ret=;
if(L<=mid) ret+=query(lson, L, R);
if(mid<R) ret+=query(rson, L, R);
ret%=MD;
return ret;
}
int main() {
read(n); read(MD);
build(, n, );
int m=getint();
while(m--) {
int c=getint();
if(c==) { int l=getint(), r=getint(), x=getint(); update(, n, , l, r, , x); }
else if(c==) { int l=getint(), r=getint(), x=getint(); update(, n, , l, r, x, ); }
else if(c==) { int l=getint(), r=getint(); printf("%d\n", query(, n, , l, r)); }
}
return ;
}
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=, k=; char c=getchar(); for(; c<''||c>''; c=getchar()) if(c=='-') k=-; for(; c>=''&&c<=''; c=getchar()) r=r*+c-''; return k*r; } const int N=;
#define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, mid, lc
#define rson mid+1, r, rc int n, MD; struct node {
int sum, add, mul;
void upd(int a, int m, int len) {
add=((ll)add*m+a)%MD;
mul=((ll)mul*m)%MD;
sum=((ll)sum*m+(ll)a*len)%MD;
}
}t[N<<];
void pushdown(int x, int len) {
if(t[x].add!= || t[x].mul!=)
t[lc].upd(t[x].add, t[x].mul, (len-(len>>))),
t[rc].upd(t[x].add, t[x].mul, len>>),
t[x].add=, t[x].mul=;
}
void pushup(int x) { t[x].sum=(t[lc].sum+t[rc].sum)%MD; }
void build(int l, int r, int x) {
t[x].add=;
t[x].mul=;
if(l==r) { t[x].sum=getint(); return; }
int mid=MID;
build(lson); build(rson);
pushup(x);
}
void update(int l, int r, int x, int L, int R, int add, int mul) {
if(L<=l && r<=R) { t[x].upd(add, mul, r-l+); return; }
pushdown(x, r-l+);
int mid=MID;
if(L<=mid) update(lson, L, R, add, mul);
if(mid<R) update(rson, L, R, add, mul);
pushup(x);
}
int query(int l, int r, int x, int L, int R) {
if(L<=l && r<=R) return t[x].sum;
pushdown(x, r-l+);
int mid=MID, ret=;
if(L<=mid) ret+=query(lson, L, R);
if(mid<R) ret+=query(rson, L, R);
ret%=MD;
return ret;
}
int main() {
read(n); read(MD);
build(, n, );
int m=getint();
while(m--) {
int c=getint();
if(c==) { int l=getint(), r=getint(), x=getint(); update(, n, , l, r, , x); }
else if(c==) { int l=getint(), r=getint(), x=getint(); update(, n, , l, r, x, ); }
else if(c==) { int l=getint(), r=getint(); printf("%d\n", query(, n, , l, r)); }
}
return ;
}
bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和的更多相关文章
- 【BZOJ1798】【AHOI2009】维护序列(线段树)
		
题目链接 题解 这不就是luogu的线段树2的板子吗.... 没有任何的区别... 上代码吧... #include<iostream> #include<cstdio> #i ...
 - BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )
		
线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...
 - Bzoj 1798: [Ahoi2009]Seq 维护序列seq(线段树区间操作)
		
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小可 ...
 - bzoj 1798: [Ahoi2009]Seq 维护序列seq  (线段树 ,多重标记下放)
		
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 7773 Solved: 2792[Submit ...
 - 1798: [Ahoi2009]Seq 维护序列seq
		
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 2930 Solved: 1087[Submit ...
 - BZOJ1798: [Ahoi2009]Seq 维护序列seq[线段树]
		
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 5504 Solved: 1937[Submit ...
 - BZOJ_1798_[AHOI2009]维护序列_线段树
		
BZOJ_1798_[AHOI2009]维护序列_线段树 题意:老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: ( ...
 - [AHOI 2009] 维护序列(线段树模板题)
		
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
 - bzoj 1798 [Ahoi2009]Seq 维护序列seq(线段树+传标)
		
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1798 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...
 
随机推荐
- C# 读取指定文件夹下所有文件
			
#region 读取文件 //返回指定目录中的文件的名称(绝对路径) string[] files = System.IO.Directory.GetFiles(@"D:\Test" ...
 - linux安装python3(已有python2.x情况下)
			
参考:https://www.cnblogs.com/Guido-admirers/p/6259410.html 1.官网下载python3 cd /home/download wget https: ...
 - 奇妙的CSS之伪类与伪元素
			
我们都知道,在CSS中有很多选择器,例如id(#), class(.),属性[attr],这些虽然可以满足一些需要,但有时候还力有未逮.伪类和伪元素就提供了一个有益的补充,可以使我们更高效编码.伪类和 ...
 - DEDECMS去除后门隐患和漏洞以及冗余代码的方法
			
链接:http://jingyan.baidu.com/article/4d58d541195bdb9dd4e9c029.html 工具/原料 织梦网站管理系统 sublime编辑器 方法/步骤 第一 ...
 - Centos之帮助命令
			
帮助命令man (manual) 比如我们可以看下man命令的解释 [root@localhost ~]# man man MAN(1) ...
 - Effective STL 学习笔记 Item 30: 保证目标区间足够大
			
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
 - Java关于数组操作函数
			
数组排序及元素查找 sort()方法对Java数组进行排序. binarySearch() 方法来查找数组中的元素,返回该元素所在的位置. import java.util.*; public cla ...
 - Struts DynaActionForm example
			
The Struts DynaActionForm class is an interesting feature to let you create a form bean dynamically ...
 - CSharp中的?.运算符
			
在编译chromiumFX工程时候,编译失败,无法正常工作.是运算符 (?.)的错误,经过查找,该运算符 参考NULL 条件运算符(C# 和 Visual Basic) 用于在执行成员访问 (?.) ...
 - 【LOJ】#2542. 「PKUWC2018」随机游走
			
题解 虽然我知道minmax容斥,但是--神仙能想到把这个dp转化成一个一次函数啊= = 我们相当于求给定的\(S\)集合里最后一个被访问到的点的时间,对于这样的max的问题,我们可以用容斥把它转化成 ...