【JSOI 2014】序列维护
【题目链接】
【算法】
线段树
注意标记下传
【代码】
#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】序列维护的更多相关文章
- BZOJ 5039: [Jsoi2014]序列维护
5039: [Jsoi2014]序列维护 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 282 Solved: 169[Submit][Status ...
- 「JSOI2014」序列维护
「JSOI2014」序列维护 传送门 其实这题就是luogu的模板线段树2,之所以要发题解就是因为学到了一种比较NB的 \(\text{update}\) 的方式.(参见这题) 我们可以把修改操作统一 ...
- bzoj5039:[Jsoi2014]序列维护
做做bzoj上的新题(不存在的) 同bzoj1798: [Ahoi2009]维护序列,样例都一样的...我能想象到的唯一的新的考察意义就是模数是2e9不是1e9,于是加法的时候需要转long long ...
- P1091 合唱队形 DP 最长升序列维护
题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...
- 解题:APIO 2014 序列分割
题面 拆开式子我们发现切割顺序不影响答案,所以可以设计出一个$dp[i][j]$表示到$i$为止切了$j$刀的最大收益之类的,然后做个前缀和就可以转移了. $dp[i][j]=min(dp[i][j] ...
- [APIO 2014] 序列分割
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3675 [算法] 首先 , 我们发现将一段序列切成若干段所获得的收益与顺序无关 于是我 ...
- sql server 2014 序列號
亲测可用 27HMJ-GH7P9-X2TTB-WPHQC-RG79R
- 2018.10.02 NOIP模拟 序列维护(线段树+广义欧拉定理)
传送门 一道比较好的线段树. 考试时线性筛打错了于是弃疗. 60分暴力中有20分的快速幂乘爆了于是最后40分滚粗. 正解并不难想. 每次区间加打懒标记就行了. 区间查询要用到广义欧拉定理. 我们会发现 ...
- 洛谷P3246 序列 [HNOI2016] 莫队/线段树+扫描线
正解:莫队/线段树+扫描线 解题报告: 传送门! 似乎是有两种方法的,,,所以分别港下好了QAQ 第一种,莫队 看到这种询问很多区间之类的就会自然而然地想到莫队趴?然后仔细思考一下,发现复杂度似乎是欧 ...
随机推荐
- HUNAN 11569 Just Another Knapsack Problem(AC自动机+dp)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11569&courseid=0 给出目标串,每个子串和 ...
- android Containers控件
1.RadioGroup 一组单选框容器 2.ListView 3.GridView 4.ExpandableListView 可折叠列表 5.ScrollView 上下滚动条 6.Horizonta ...
- P1067 多项式输出 (模拟)
题目描述 一元nn次多项式可用如下的表达式表示: 其中,a_i x^i 称为i次项,ai 称为i次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 多项式中自变量 ...
- 未能加载文件或程序集“System.EnterpriseServices, Version=4.0.0.0或2.0.0.0
未能加载文件或程序集“System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50 ...
- Angular 路由⑦要素
cnzt http://www.cnblogs.com/zt-blog/p/7919185.html http://www.cnblogs.com/zt-blog/p/7919185.ht ...
- Java 复制字符串到系统剪贴板
原文:http://www.open-open.com/code/view/1453188435933 public static void main(String[] args) { StringS ...
- mysql大数据量分页查询优化
参考文章:https://www.dexcoder.com/selfly/article/293 Mysql的分页查询十分简单,但是当数据量大的时候一般的分页就吃不消了. 传统分页查询:SELECT ...
- 使用MVP模式重构代码
之前写了两篇关于MVP模式的文章,主要讲得都是一些概念,这里谈谈自己在Android项目中使用MVP模式的真实感受,并以实例的形式一起尝试来使用MVP模式去重构我们现有的代码. 有兴趣的童鞋可以先去阅 ...
- 【Lucene】具体解释Lucene全文检索的信息写入与读取
Lucene的大致结构图: 信息写入索引库的过程: 读取信息的过程: 以下是一个向索引库写入信息与读取信息的样例: public void testCreateIndex() throws Excep ...
- Oracle 12c agent install for linux
安装Agent代理 在EM11g时,agent安装是通过在被监制主机端下载agent代理并安装,在EM12c版本号上,能够在EM12c服务端.通过"推送"的方式把agent代理在远 ...