【题目链接】

点击打开链接

【算法】

线段树

注意标记下传

【代码】

#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. Codeforces 961 D Pair Of Lines

    题目描述 You are given nn points on Cartesian plane. Every point is a lattice point (i. e. both of its c ...

  2. 配置maven从私服上下载构件

    maven 本地setting.xml配置 <!--配置maven从私服上下载构件 --> <profiles> <profile> <repositorie ...

  3. UICollectionView 使用 介绍

    1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView n   不直接等效于NSCollectionView ...

  4. 【转】构造HTTP请求Header实现“伪造来源IP”

    构造 HTTP请求 Header 实现“伪造来源 IP ” 在阅读本文前,大家要有一个概念,在实现正常的TCP/IP 双方通信情况下,是无法伪造来源 IP 的,也就是说,在 TCP/IP 协议中,可以 ...

  5. PAT 1003 Sharing (25)

    题目描写叙述 To store English words, one method is to use linked lists and store a word letter by letter. ...

  6. 怎样使用1M的内存排序100万个8位数

    今天看到这篇文章.颇为震撼.感叹算法之"神通". 借助于合适的算法能够完毕看似不可能的事情. 最早这个问题是在Stack Overflow站点上面给出的(Sorting numbe ...

  7. 带GPG签名的Git tag

    原文地址http://airk000.github.io/git/2013/09/30/git-tag-with-gpg-key Git tag ###Tag用来做什么? Tag即标签,用以给项目仓储 ...

  8. Android逆向利器和smali代码修改出错举例-入参类型

    当smali修改代码出错举例1,log如下: 虚拟机层次: 1.本身做出了预测,寄存器v2是符合要求入参,暗示你这个也许是你想要的.VFY: register1 v2 type 17, wanted ...

  9. linux输入子系统(6)-input子系统介绍及结构图

    注:本系列转自: http://www.ourunix.org/post/290.html input子系统介绍         输入设备(如按键,键盘,触摸屏,鼠标,蜂鸣器等)是典型的字符设备,其一 ...

  10. mysql 环境变量之 group_concat_max_len

    今天使用mysql group_concat()函数,对查询的数据进行字符串连接操作. 不过由于查询的结果较多,连接后的结果很长导致不能完全显示. 查询手册发现如下说明: (先说说group_conc ...