跳到正文

科技·工程

新的线段树赤石科技!

dk_qwq约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年4月26日
收录
2026年8月27日
更新
2026年11月11日
篇幅
约 6 分钟

版本

  1. v1当前

众所周知线段树的懒标记复合操作十分的复杂!!!

那么我们可以使用形如暴力的做法完成以下操作

C++
using F = function<Info(Info)>;
F lz[N * 4];
void push(int p, F f) {
    t[p] = f(t[p]);
    if(lz[p]) {
        F old = lz[p];
        lz[p] = [old, f](auto info){
            return f(old(info));
        };
    }
    else lz[p] = f;
}
void pushdown(int p) {
    if(!lz[p]) return ;
    push(p << 1, lz[p]), push(p << 1 | 1, lz[p]);
    lz[p] = nullptr;
}

是不是相较于原版就简单了很多呢(

https://www.luogu.com.cn/record/275735895

C++
#include <bits/stdc++.h>
#include <functional>
using namespace std;
using ll = long long;
ll mod;
const int N = 1e5 + 5;
struct Info{
    ll sum;
    int len;
    Info operator+(const Info& other) {
        return {
            (sum + other.sum) % mod,
            (len + other.len)
        };
    }
}t[N * 4];
using F = function<Info(Info)>;
F lz[N * 4];
int a[N];
void build(int p, int l, int r) {
    if(l == r) return t[p] = {a[l], 1}, void();
    int mid = (l + r) >> 1;
    build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
    t[p] = t[p << 1] + t[p << 1 | 1];
}
void push(int p, F f) {
    t[p] = f(t[p]);
    if(lz[p]) {
        F old = lz[p];
        lz[p] = [old, f](auto info){
            return f(old(info));
        };
    }
    else lz[p] = f;
}
void pushdown(int p) {
    if(!lz[p]) return ;
    push(p << 1, lz[p]), push(p << 1 | 1, lz[p]);
    lz[p] = nullptr;
}
void modify(int p, int l, int r, int ql, int qr, F f) {
    if(ql <= l && r <= qr) return push(p, f);
    pushdown(p);
    int mid = (l + r) >> 1;
    if(ql <= mid) modify(p << 1, l, mid, ql, qr, f);
    if(mid < qr) modify(p << 1 | 1, mid + 1, r, ql, qr, f);
    t[p] = t[p << 1] + t[p << 1 | 1];
}
ll query(int p, int l, int r, int ql, int qr) {
    if(ql <= l && r <= qr) return t[p].sum;
    pushdown(p);
    int mid = (l + r) >> 1;
    ll ans = 0;
    if(ql <= mid) ans += query(p << 1, l, mid, ql, qr);
    if(mid < qr) ans += query(p << 1 | 1, mid + 1, r, ql, qr);
    t[p] = t[p << 1] + t[p << 1 | 1];
    return ans % mod;
}
int n, q;
void solve() {
    cin >> n >> q >> mod;
    for(int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while(q--) {
        int opt, x, y, k;
        cin >> opt >> x >> y;
        if(opt != 3) cin >> k;

        if(opt == 1) {
            modify(1, 1, n, x, y, [k](Info info) -> Info{
                (info.sum *= k) %= mod;
                return info;
            });
        }
        if(opt == 2) {
            modify(1, 1, n, x, y, [k](Info info) -> Info{
                (info.sum += 1ll * k * info.len % mod) %= mod;
                return info;
            });
        }
        if(opt == 3) {
            cout << query(1, 1, n, x, y) << endl;
        }
        
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    // freopen("A.in", "r", stdin);
    // freopen("A.out", "w", stdout);
    int T = 1;
    // cin >> T;
    while(T--) solve();
}
原文