【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 第一种,莫队 看到这种询问很多区间之类的就会自然而然地想到莫队趴?然后仔细思考一下,发现复杂度似乎是欧 ...
随机推荐
- JStorm学习
一.简介 JStorm是一个分布式实时计算引擎.JStorm是一个类似于Hadoop MapReduce的系统,用户按照指定的接口实现一个任务,然后将这个任务交给JStorm系统,JStorm将这个任 ...
- HDU1800 Flying to the Mars 【贪心】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 聊聊WiFi Hacks:为何你的Karma攻击不好使了
0.前言 三年前我发表了一篇文章<黑客有办法让你自动连上陌生WiFi>,介绍Karma攻击可以让你的无线设备自动连上黑客的WiFi.当时引起了还算比较热烈的讨论,关于WiFi安全,关于Ka ...
- 【Todo】已经打开的页面需要清掉的坑
下面是当前我浏览器里面打开的技术文章.需要清掉.一个坑一个坑地填吧. 微信文件传输里面也有几篇12.6号的<Akuna Capital电面面经><2016最流行的Java EE服务器 ...
- BUPT复试专题—网络传输(2014网研)
题目描述 网络的高效互联与智能传输是提升海量用户服务请求映射效率的重要措施.在这个任务中,你需耍在最小的传输时间内,将数据源传输到指定的网络节点中.我们给定的网络一共包含N个节点,其中节点1为数据源. ...
- [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)
Do you get lost when working with functions and the new keyword? Prototypal inheritance can be compl ...
- 怎样使用ListView?
怎样使用ListView? watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFC ...
- WCF 内存入口检查失败 Memory gates checking failed
在做JC系统时,出现这样的错误: 出现该错误信息的原因是因为WCF服务激活之前,系统应该具有的最小内存量不足config文件中设置的百分比.我是在本机调试的时候出现的. 解决方法: 关闭 ...
- Java字符串String 集合的迭代器
Java字符串String 我们知道Java的字符窜是Immutable(不可变)的,一旦创建就不能更改其内容了:平常我们对字符串的操作是最多的,其实对字符串的操作,返回的字符串都是新建的字符串对象, ...
- 浏览器开发者工具Chrome Developer Tool
开发者工具Chrome Developer Tool https://developers.google.com/chrome-developer-tools/docs/profiles 一直被墙 ...