加法乘法线段树模板

P2023 [AHOI2009]维护序列

指定一个区间 加上或者乘以 V,

查询一个区间所有元素和%P

与纯加法线段树不同的是,lazy_tag 的传递

(x + y) * v = xv + yv。

所以每次乘法,都要把加法的lazy_tag * v
而加法与加法线段树的操作一样

#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
const int MAXN = 100005 + 5;
LL N, P, M;
LL A[MAXN];
struct seg { LL l, r, v, lz, mz; } t[MAXN << 2];
LL lch(LL k) { return k << 1; };
LL rch(LL k) { return k << 1 | 1; };
inline void add(LL& a, LL b) { a = (a + b) % P; };
inline void mul(LL& a, LL b) { a = (a * b) % P; };
void push_up(LL k) { t[k].v = (t[lch(k)].v + t[rch(k)].v) % P; };
void push_down(LL k) {
    if (t[k].mz == 1 && !t[k].lz) return;
    LL mid = (t[k].r + t[k].l) >> 1, lz = t[k].lz, mz = t[k].mz;
    t[lch(k)].v = (t[lch(k)].v * mz + lz * (mid - t[k].l + 1))%P;
    t[rch(k)].v = (t[rch(k)].v * mz + lz * (t[k].r - mid))%P;
    mul(t[lch(k)].mz, mz);
    mul(t[rch(k)].mz, mz);
    t[lch(k)].lz = (t[lch(k)].lz * mz + lz) % P;
    t[rch(k)].lz = (t[rch(k)].lz * mz + lz) % P;
    t[k].lz = 0; t[k].mz = 1;
}
void build(LL k, LL l, LL r) {
    t[k].l = l, t[k].r = r, t[k].lz = 0, t[k].mz = 1;
    if (l == r) {
        t[k].v = A[l];
        return;
    }
    LL mid = (r + l) >> 1;
    build(lch(k), l, mid);
    build(rch(k), mid + 1, r);
    push_up(k);
}
void update(LL k, LL l, LL r, LL v, LL f) {
    if (t[k].l >= l && t[k].r <= r) {
        if (f == 1) {
            mul(t[k].v, v);
            mul(t[k].mz, v);
            mul(t[k].lz, v);
        }
        else {
            add(t[k].v, v * (t[k].r - t[k].l + 1));
            add(t[k].lz,v);
        }
        return;
    }
    push_down(k);
    if (t[lch(k)].r >= l) update(lch(k), l, r, v, f);
    if (t[rch(k)].l <= r) update(rch(k), l, r, v, f);
    push_up(k);
}
LL query(LL k, LL l, LL r) {
    LL ans = 0;
    if (t[k].l >= l && t[k].r <= r) return t[k].v;
    push_down(k);
    if (t[lch(k)].r >= l) add(ans, query(lch(k), l, r));
    if (t[rch(k)].l <= r) add(ans, query(rch(k), l, r));
    return ans;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> P;
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
    }
    build(1, 1, N);
    LL a, b, c, d;
    cin >> M;
    for (int i = 0; i < M; i++) {
        cin >> a;
        if (a == 1) {
            cin >> b >> c >> d;
            update(1, b, c, d, 1);
        }
        else if (a == 2) {
            cin >> b >> c >> d;
            update(1, b, c, d, 2);
        }
        else {
            cin >> b >> c;
            cout << query(1, b, c) << endl;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/--zz/p/11473962.html

时间: 2024-08-30 06:10:05

加法乘法线段树模板的相关文章

ZOJ 2671 -Cryptography ( 矩阵乘法 + 线段树 )

ZOJ 2671 - Cryptography ( 矩阵乘法 + 线段树 ) 题意: 给定模数r, 个数n, 询问数m 然后是n个矩阵,每次询问,输出矩阵联乘之后的结果. 分析: 矩阵乘法 + 线段树优化 这里线段树只有询问没有更新操作. PS:顺便仰慕一下watashi.... 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using names

线段树模板(结构体)

线段树研究了两天了,总算有了点眉目,今天也把落下的题,补了一下. 贴一份线段树模板 线段树的特点: 1. 每一层都是区间[a, b]的一个划分,记 L = b - a 2. 一共有log2L层 3. 给定一个点p,从根到叶子p上的所有区间都包含点p,且其他区间都不包含点p. 4. 给定一个区间[l; r],可以把它分解为不超过2log2 L条不相交线段的并. 总结来说:线段树最近本的应用是4点: 1.单点更新:单点替换.单点增减 2.单点询问 3.区间询问:区间之和.区间最值 4.区间更新:区间

[ACM] 线段树模板

#include<iostream> #include<cmath> using namespace std; #define maxn 200005 class Node{ public: int l,r; int add;//附加值 int sum; }node[maxn]; int getRight(int n){//获得满足2^x>=n的最小x[从0层开始,给编号获得层数] return ceil(log10(n*1.0)/log10(2.0)); } void bu

[POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; int n,q,tot,a[110000]; in

HDU 5068 Harry And Math Teacher( 矩阵乘法 + 线段树维护 )

HDU 5068 Harry And Math Teacher( 矩阵乘法 + 线段树维护 ) 题意: 首先是这题题意理解错误,,其次是这题无法理解状态... 已经不是英文有多烂的情况了,是中文没学好啊.....大学不学语文才是真正的硬伤啊 题目意思 有一个城堡有很多层楼, 每层楼有2个门,每个门里面又有两个楼梯,可以通往上一层的两个门 问,从x层楼到y层楼有多少中方法(不能返回) 具体看图吧,,,已经不会说话了 1 #include <cstdio> 2 #include <cstri

hdu 4819 二维线段树模板

/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int Max, Min; }; int locx[MAXN], l

【HDU 4819】Mosaic 二维线段树模板

二维线段树的模板题,和一维一样的思路,更新的时候注意一下细节. 存模板: /* 二维线段树模板整理 */ #include<cstdio> #include<algorithm> using namespace std; #define lson (pos<<1) #define rson (pos<<1|1) const int maxn = 805; const int INF = (1 << 30); int n; int posX[max

ZOJ 2671 Cryptography 矩阵乘法+线段树

B - Cryptography Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 2671 Description Young cryptoanalyst Georgie is planning to break the new cipher invented by his friend Andie. To do this, he must

LA 2191电位计(线段树模板题)

线段树模板题,没啥好说的.....注意输出是case之间空一行就行.........之前一直没注意,一直wa 代码如下: #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #includ