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 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...
随机推荐
- httpd功能配置之虚拟主机【转】
apache默认使用80端口提供服务,使用主服务器配置的话,一台物理机只能提供一个站点服务:可以使用虚拟主机方式提供不同的访问,以实现一台主机提供多站点服务. 虚拟主机的实现方式有三种:基于端口.基于 ...
- JavaScript新手学习笔记(一)
1.JavaScript 对大小写敏感. JavaScript 对大小写是敏感的. 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getElementById 与 getE ...
- Java集合之Collection与之子类回顾
Java学习这么久,打算这几天回顾下java的基本知识点,首先是集合. 一.常用集合类关系图 Collection |___List 有序,可重复 |___ArrayList 底层数据结构是数组,增 ...
- mysql -> 索引_07
索引与sql语句优化 压力测试对比
- java基础31 List集合下的Vector集合
单例集合体系: ---------| collection 单例集合的根接口--------------| List 如果实现了list接口的集合类,具备的特点:有序,可重复 注:集合 ...
- 洛谷P3119 草鉴定
这个题调了一天.. 传送门 读完题目之后我们不难想出这个题是个tarjan缩点问题,因为尽量多的经过草场,所以一号点所在的强连通分量里左右的点都是不需要在进行走逆向边,所能到达的. 然后问题就落在怎么 ...
- Javascript之对象的创建
面向对象语言有一个非常显著的标志,那就是它们都有类的概念,通过类之间的继承就可以达到任意创建具有相同属性方法的对象.而在ECMAScript中并没有类的概念,它把对象定义为:无序属性的集合,其属性包含 ...
- Tetris:pygame实现
网上搜到一个Pygame写的俄罗斯方块(tetris),大部分看懂的前提下增加了注释,Fedora19下运行OK的 主程序: #coding:utf8 #! /usr/bin/env python # ...
- SSIS 学习之旅 序章 和 简介
SSIS 学习之旅目录: 第一章: SSIS 学习之旅 第一个SSIS 示例(一) 第二章: SSIS 学习之旅 第一个SSIS 示例(二) 第三章: SSIS 学习之旅 数据同步 第四章: SSIS ...
- C#和PHP 长整型时间互转
//2018/5/14 16:03:05转换:1526284985 public static double ConvertToDouble(DateTime date) { , , )); var ...