【题目链接】

点击打开链接

【算法】

线段树

注意标记下传

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 500000 typedef long long LL; struct SegTreeNode {
LL l,r,sum,lazya,lazyb;
}tree[MAXN+]; LL i,N,P,Q,l,r,x,opt;
LL a[MAXN+]; template <typename T> void read(T &x) {
LL f=; char c = getchar(); x=;
for (; !isdigit(c); c = getchar()) { if (c=='-') f=-; }
for (; isdigit(c); c = getchar()) x=x*+c-'';
x*=f;
} inline void write(LL x) {
if (x < ) {
putchar('-');
x = -x;
}
if (x / > ) write(x/);
putchar(x%+'');
} inline void pushup(LL index) {
tree[index].sum = (tree[index*].sum + tree[index*+].sum) % P;
} inline void pushdown(LL index) {
tree[index*].sum = (tree[index*].sum * tree[index].lazya + tree[index].lazyb * (tree[index*].r - tree[index*].l + )) % P;
tree[index*+].sum = (tree[index*+].sum * tree[index].lazya + tree[index].lazyb * (tree[index*+].r - tree[index*+].l + )) % P;
tree[index*].lazya = (tree[index*].lazya * tree[index].lazya) % P;
tree[index*+].lazya = (tree[index*+].lazya * tree[index].lazya) % P;
tree[index*].lazyb = (tree[index*].lazyb * tree[index].lazya + tree[index].lazyb) % P;
tree[index*+].lazyb = (tree[index*+].lazyb * tree[index].lazya + tree[index].lazyb) % P;
tree[index].lazya = ; tree[index].lazyb = ;
} inline void build(LL index,LL l,LL r) {
LL mid;
tree[index].l = l;
tree[index].r = r;
tree[index].lazya = ;
if (l == r) tree[index].sum = a[l] % P;
else {
mid = (l + r) >> ;
build(index*,l,mid);
build(index*+,mid+,r);
pushup(index);
}
} inline void changea(int index,int l,int r,int x) {
LL mid;
pushdown(index);
if ((tree[index].l == l) && (tree[index].r == r)) {
tree[index].sum = tree[index].sum * x % P;
tree[index].lazya = (tree[index].lazya * x) % P;
tree[index].lazyb = (tree[index].lazyb * x) % P;
}else {
if (tree[index].l == tree[index].r) return;
mid = (tree[index].l + tree[index].r) >> ;
if (r <= mid) changea(index*,l,r,x);
else if (l >= mid + ) changea(index*+,l,r,x);
else {
changea(index*,l,mid,x);
changea(index*+,mid+,r,x);
}
pushup(index);
}
} inline void changeb(LL index,LL l,LL r,LL x) {
LL mid;
pushdown(index);
if ((tree[index].l == l) && (tree[index].r == r)) {
tree[index].sum = (tree[index].sum + x * (tree[index].r - tree[index].l + )) % P;
tree[index].lazyb = (tree[index].lazyb + x) % P;
}else {
if (tree[index].l == tree[index].r) return;
mid = (tree[index].l + tree[index].r) >> ;
if (r <= mid) changeb(index*,l,r,x);
else if (l >= mid + ) changeb(index*+,l,r,x);
else {
if (tree[index].l == tree[index].r) return;
changeb(index*,l,mid,x);
changeb(index*+,mid+,r,x);
}
pushup(index);
}
} inline LL query(LL index,LL l,LL r) {
LL mid;
pushdown(index);
if ((tree[index].l == l) && (tree[index].r == r)) return tree[index].sum;
else {
if (tree[index].l == tree[index].r) return ;
mid = (tree[index].l + tree[index].r) >> ;
if (r <= mid) return query(index*,l,r);
else if (l >= mid + ) return query(index*+,l,r);
else return (query(index*,l,mid) + query(index*+,mid+,r)) % P;
}
} int main() { read(N); read(P); for (i = ; i <= N; i++) read(a[i]); build(,,N); read(Q); while (Q--) {
read(opt);
if (opt == ) {
read(l); read(r); read(x);
changea(,l,r,x);
}else if (opt == ) {
read(l); read(r); read(x);
changeb(,l,r,x);
}else {
read(l); read(r);
write(query(,l,r));
puts("");
}
} return ; }

【JSOI 2014】序列维护的更多相关文章

  1. BZOJ 5039: [Jsoi2014]序列维护

    5039: [Jsoi2014]序列维护 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 282  Solved: 169[Submit][Status ...

  2. 「JSOI2014」序列维护

    「JSOI2014」序列维护 传送门 其实这题就是luogu的模板线段树2,之所以要发题解就是因为学到了一种比较NB的 \(\text{update}\) 的方式.(参见这题) 我们可以把修改操作统一 ...

  3. bzoj5039:[Jsoi2014]序列维护

    做做bzoj上的新题(不存在的) 同bzoj1798: [Ahoi2009]维护序列,样例都一样的...我能想象到的唯一的新的考察意义就是模数是2e9不是1e9,于是加法的时候需要转long long ...

  4. P1091 合唱队形 DP 最长升序列维护

    题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...

  5. 解题:APIO 2014 序列分割

    题面 拆开式子我们发现切割顺序不影响答案,所以可以设计出一个$dp[i][j]$表示到$i$为止切了$j$刀的最大收益之类的,然后做个前缀和就可以转移了. $dp[i][j]=min(dp[i][j] ...

  6. [APIO 2014] 序列分割

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3675 [算法] 首先 , 我们发现将一段序列切成若干段所获得的收益与顺序无关 于是我 ...

  7. sql server 2014 序列號

    亲测可用 27HMJ-GH7P9-X2TTB-WPHQC-RG79R

  8. 2018.10.02 NOIP模拟 序列维护(线段树+广义欧拉定理)

    传送门 一道比较好的线段树. 考试时线性筛打错了于是弃疗. 60分暴力中有20分的快速幂乘爆了于是最后40分滚粗. 正解并不难想. 每次区间加打懒标记就行了. 区间查询要用到广义欧拉定理. 我们会发现 ...

  9. 洛谷P3246 序列 [HNOI2016] 莫队/线段树+扫描线

    正解:莫队/线段树+扫描线 解题报告: 传送门! 似乎是有两种方法的,,,所以分别港下好了QAQ 第一种,莫队 看到这种询问很多区间之类的就会自然而然地想到莫队趴?然后仔细思考一下,发现复杂度似乎是欧 ...

随机推荐

  1. AC日记——[USACO1.1]坏掉的项链Broken Necklace 洛谷 P1203

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  2. 小程序-列表块/类式ul-li格式(1)

    摘要 目前列表能布局出来,但是目前我个人还没解决的问题是:如果每个列表块都有详情页怎么解决呢? 1:我的效果图 2.正常的每个都能点击的html 注:上面的代码确实能够实现我的每个[menu2_vie ...

  3. android Containers控件

    1.RadioGroup 一组单选框容器 2.ListView 3.GridView 4.ExpandableListView 可折叠列表 5.ScrollView 上下滚动条 6.Horizonta ...

  4. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  5. T2602 最短路径问题 codevs

    http://codevs.cn/problem/2602/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold   题目描述 Description 平面上有n个点(n& ...

  6. LCD1602和LCD12864

    1602 字符型(字符型的模组) 只能显示数字与字符,不可以显示汉字 12864 点阵型(图形型的模组) 可根据需求任意显示字符 数字 汉字 图形 1602 为16字乘2行12864 为128列点阵乘 ...

  7. 一点点VIM

    VIM 当你喜欢它时,你会发现真的不错,不过配置真是麻烦, 不过万事开头难,当你熟练时真的会发现她的美. syntax on set nu colo evening set mouse=a set c ...

  8. java学习笔记总略

    二.正文(一)Java1.接口和抽象类的区别①抽象类里可以有构造方法,而接口内不能有构造方法.②抽象类中可以有普通成员变量,而接口中不能有普通成员变量.③抽象类中可以包含非抽象的普通方法,而接口中所有 ...

  9. VisualSVN Server 导入已存在的库

    http://blog.csdn.net/lidatgb/article/details/7984220         早些时候建立过一个SVN Server的库,后来觉得库的名字太长了,随意换了一 ...

  10. hdu 1689 Alien’s Necklace (bfs层次图剪枝)

    Alien's Necklace Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...