博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和
阅读量:6943 次
发布时间:2019-06-27

本文共 4263 字,大约阅读时间需要 14 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
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=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=100005;#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<<2];void pushdown(int x, int len) { if(t[x].add!=0 || t[x].mul!=1) t[lc].upd(t[x].add, t[x].mul, (len-(len>>1))), t[rc].upd(t[x].add, t[x].mul, len>>1), t[x].add=0, t[x].mul=1;}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=0; t[x].mul=1; 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+1); return; } pushdown(x, r-l+1); int mid=MID; if(L<=mid) update(lson, L, R, add, mul); if(mid

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
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=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=100005;#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<<2];void pushdown(int x, int len) { if(t[x].add!=0 || t[x].mul!=1) t[lc].upd(t[x].add, t[x].mul, (len-(len>>1))), t[rc].upd(t[x].add, t[x].mul, len>>1), t[x].add=0, t[x].mul=1;}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=0; t[x].mul=1; 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+1); return; } pushdown(x, r-l+1); int mid=MID; if(L<=mid) update(lson, L, R, add, mul); if(mid

 

转载地址:http://evanl.baihongyu.com/

你可能感兴趣的文章
Nginx Java 日志切割脚本
查看>>
浅谈代码审计入门实战:某博客系统最新版审计之旅
查看>>
nyoj 119士兵杀敌(三)(线段树区间最值查询,RMQ算法)
查看>>
truncate/drop表非常慢,怎么办?用硬链接,极速体验
查看>>
spring boot测试
查看>>
Timer使用
查看>>
H5+混合移动app应用开发——坑我太甚
查看>>
nc/netcat命令
查看>>
web3.js编译Solidity,发布,调用全部流程(手把手教程)
查看>>
Java国际化号码验证方法,国内手机号正则表达式
查看>>
HDU 1158 Employment Planning
查看>>
表格内嵌编辑控件
查看>>
DNS解析原理和流程
查看>>
Windows程序设计_15_求书
查看>>
Firefox 6 正式发布
查看>>
ESET Smart Security – 免费90天(sv)
查看>>
微软职位内部推荐-Senior SDE
查看>>
js Object.prototype.toString.call()
查看>>
android:padding和android:margin的区别[转]
查看>>
开放源码的对象关系映射工具ORM.NET 快档开发入门 Quick Start
查看>>