1798: [Ahoi2009]Seq 维护序列seq

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=1798

Description

老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。

Input

第一行两个整数N和P(1≤P≤1000000000)。第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N)。第三行有一个整数M,表示操作总数。从第四行开始每行描述一个操作,输入的操作有以下三种形式: 操作1:“1 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai×c (1≤t≤g≤N,0≤c≤1000000000)。 操作2:“2 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai+c (1≤t≤g≤N,0≤c≤1000000000)。 操作3:“3 t g”(不含双引号)。询问所有满足t≤i≤g的ai的和模P的值 (1≤t≤g≤N)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。

Output

对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。

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

2

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 线段树 区间乘法区间加法 区间求和的更多相关文章

  1. 【BZOJ1798】【AHOI2009】维护序列(线段树)

    题目链接 题解 这不就是luogu的线段树2的板子吗.... 没有任何的区别... 上代码吧... #include<iostream> #include<cstdio> #i ...

  2. BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )

    线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...

  3. Bzoj 1798: [Ahoi2009]Seq 维护序列seq(线段树区间操作)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小可 ...

  4. bzoj 1798: [Ahoi2009]Seq 维护序列seq (线段树 ,多重标记下放)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 7773  Solved: 2792[Submit ...

  5. 1798: [Ahoi2009]Seq 维护序列seq

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 2930  Solved: 1087[Submit ...

  6. BZOJ1798: [Ahoi2009]Seq 维护序列seq[线段树]

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 5504  Solved: 1937[Submit ...

  7. BZOJ_1798_[AHOI2009]维护序列_线段树

    BZOJ_1798_[AHOI2009]维护序列_线段树 题意:老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: ( ...

  8. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  9. bzoj 1798 [Ahoi2009]Seq 维护序列seq(线段树+传标)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1798 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...

随机推荐

  1. LinuxUSB驱动程序调试--009:编写应用程序---验证协议【转】

    转自:http://biancheng.dnbcw.info/linux/257411.html [1] 如何编译X86下的 uBuntu APP---非常简单:            gcc -o ...

  2. python并发爬虫利器tomorrow(一)

    tomorrow是我最近在用的一个爬虫利器,该模块属于第三方的一个模块,使用起来非常的方便,只需要用其中的threads方法作为装饰器去修饰一个普通的函数,既可以达到并发的效果,本篇将用实例来展示to ...

  3. 练习题 --- 写出5种css定位语法

    写出至少5种css语法(每种语法不一样)

  4. ROSCon 2017通知 Announcing ROSCon 2017: September 21st and 22nd in Vancouver

    ROSCon 2017通知:9月21日和22日在温哥华 我们很高兴地宣布,2017年ROSCon将在举行9月21-22日,2017年温哥华会议中心在加拿大温哥华.2017年IROS将在同一地点9月24 ...

  5. LeetCode664. Strange Printer

    There is a strange printer with the following two special requirements: The printer can only print a ...

  6. Ubuntu CEPH快速安装

    一.CEPH简介 不管你是想为云平台提供Ceph 对象存储和/或 Ceph 块设备,还是想部署一个 Ceph 文件系统或者把 Ceph 作为他用,所有 Ceph 存储集群的部署都始于部署一个个 Cep ...

  7. Linux命令之cp命令

    cp命令:用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标 ...

  8. PHP性能调优---php-fpm - 启动参数及重要配置详解

    约定几个目录/usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.ini 一,php-fpm ...

  9. more命令 less命令

    more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作.more名单中内置了若干快捷键,常用的有H(获得帮助信息),Enter(向下翻滚一行), ...

  10. MFC+WinPcap编写一个嗅探器之三(WinPcap)

    介绍程序模块前,这一节再复习一下WinPcap WinPcap开发一个嗅探器的主要步骤如下: (1)获取嗅探设备 WinPcap提供了pcap_findalldevs_ex() 函数来实现这个功能: ...