吉司机线段树学习笔记

· · 科技·工程

吉司机线段树

假设有这样一道题目:

给定一个长度为 n 的序列 \{a_i\},有 q 次操作,可能的操作如下:

  1. 1 x 表示对于所有的 a_i,使 a_i = \max(a_i, x)
  2. 2 p x 表示使 a_p = x

在所有操作结束后,输出进行操作之后的序列 \{a_i\}。

首先注意到有一个操作 a_i = \max(a_i, x),这操作很新颖啊,但是也是有迹可循的,显然会有两种区间不符合条件:

但是这样还是可能被卡到单次 O(n) 的复杂度,我们不妨多维护一个区间次小值 w[u].mn2 来看看情况,此时就有如下几种情况:

时间复杂度是严格 O(q \log n) 的。

例题

洛谷 P10639

给定一个长度为 n 的序列 a,编号从 1 到 n。要求支持下面几种操作:

  1. 给一个区间 [l, r] 加上一个数 x;
  2. 把一个区间 [l, r] 里小于 x 的数变成 x;
  3. 把一个区间 [l, r] 里大于 x 的数变成 x;
  4. 求区间 [l, r] 的和;
  5. 求区间 [l, r] 的最大值;
  6. 求区间 [l, r] 的最小值。

我们使用类比地使用上述方法,在这道题中甚至能跑过一些实现比较差的吉司机线段树。

code :

#include <bits/stdc++.h>
#define ll long long
#define db double
#define inf 1000000000000000000
#define endl "\n"

namespace fastio {
    char buf[1 << 21], *p1 = buf, *p2 = buf;

    const ll getc() {
        return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;
    }

    const ll read() {
        ll x = 0, f = 1;

        char ch = getc();

        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1; ch = getc();
        }

        while (ch >= '0' && ch <= '9') {
            x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
        }

        return x * f;
    }

    const void write(ll x) {
        if (x < 0) {
            putchar('-'), x = -x;
        }

        ll sta[35], top = 0;

        do {
            sta[top++] = x % 10, x /= 10;
        } while (x);

        while (top) putchar(sta[--top] + 48);
    }
}

using namespace std;

ll n, q, a[500005], l, r, x;

struct segment {
    ll l, r, len, sum, mx, mn, lzy1, lzy2 = inf;
    // lzy1 是区间和 tag, lzy2 是区间修改 tag
} w[2000005];

void pushup(ll u) { 
    w[u].sum = w[u * 2].sum + w[u * 2 + 1].sum;
    w[u].mx = max(w[u * 2].mx, w[u * 2 + 1].mx);
    w[u].mn = min(w[u * 2].mn, w[u * 2 + 1].mn);
}

void build(ll u, ll l, ll r) {
    w[u].l = l, w[u].r = r, w[u].len = r - l + 1;

    if (l == r) {
        w[u].sum = w[u].mx = w[u].mn = a[l];

        return ;
    }

    ll mid = l + ((r - l) >> 1);

    build(u * 2, l, mid), build(u * 2 + 1, mid + 1, r);

    pushup(u);
}

void maketag1(ll u, ll x) { // 维护区间和的 tag  
    if(w[u].lzy2 == inf) {
        w[u].lzy1 += x;
    } else {
        w[u].lzy2 += x;
    }

    w[u].sum += w[u].len * x, w[u].mx += x, w[u].mn += x;
}

void maketag2(ll u, ll x) { // 维护全部修改的 tag
    w[u].sum = w[u].len * x, w[u].lzy2 = x, w[u].mx = w[u].mn = x;

    w[u].lzy1 = 0;
}

void pushdown(ll u) {
    if(w[u].lzy2 == inf) {
        maketag1(u * 2, w[u].lzy1);
        maketag1(u * 2 + 1, w[u].lzy1);
        w[u].lzy1 = 0;
    } else {
        maketag2(u * 2, w[u].lzy2);
        maketag2(u * 2 + 1, w[u].lzy2);
        w[u].lzy2 = inf;
    }
}

ll query(ll u, ll opt) {
    if (l <= w[u].l && w[u].r <= r) {
        if (opt == 1) {
            return w[u].sum;
        } else if (opt == 2) {
            return w[u].mx;
        } else if (opt == 3) {
            return w[u].mn;
        }
    }

    pushdown(u);

    ll mid = w[u].l + ((w[u].r - w[u].l) >> 1), res;

    if (opt == 1) res = 0; // sum

    if (opt == 2) res = LLONG_MIN; // max

    if (opt == 3) res = LLONG_MAX; // min

    if (l <= mid) {
        if (opt == 1) {
            res += query(u * 2, opt);
        } else if (opt == 2) {
            res = max(res, query(u * 2, opt));
        } else if (opt == 3) {
            res = min(res, query(u * 2, opt));
        }
    }

    if (r > mid) {
        if (opt == 1) {
            res += query(u * 2 + 1, opt);
        } else if (opt == 2) {
            res = max(res, query(u * 2 + 1, opt));
        } else if (opt == 3) {
            res = min(res, query(u * 2 + 1, opt));
        }
    }

    return res;
}

void update(ll u) {
    if (l <= w[u].l && w[u].r <= r) {
        maketag1(u, x); return ;
    }

    pushdown(u);

    ll mid = w[u].l + ((w[u].r - w[u].l) >> 1);

    if (l <= mid) {
        update(u * 2);
    }

    if (r > mid) {
        update(u * 2 + 1);
    }

    pushup(u);
}

void upd(ll u, ll opt) {
    if (opt == 1) { //a[i] = max(a[i], x)
        if (w[u].mn >= x) {
            return ;
        }

        if (w[u].mx <= x) {
            maketag2(u, x); return ;
        }
    }

    if (opt == 2) { // a[i] = min(a[i], x)
        if (w[u].mx <= x) {
            return ;
        }

        if (w[u].mn >= x) {
            maketag2(u, x); return ;
        }
    }

    pushdown(u);

    upd(u * 2, opt), upd(u * 2 + 1, opt);

    pushup(u);

    return ;
}

void modify(ll u, ll opt) {
    if (l <= w[u].l && w[u].r <= r) {
        upd(u, opt); return ;
    }

    pushdown(u);

    ll mid = w[u].l + ((w[u].r - w[u].l) >> 1);

    if (l <= mid) {
        modify(u * 2, opt);
    }

    if (r > mid) {
        modify(u * 2 + 1, opt);
    }

    pushup(u);  
}

int main() {
    n = fastio::read();

    for (ll i = 1; i <= n; i++) {
        a[i] = fastio::read();
    }

    build(1, 1, n);

    q = fastio::read();

    while (q--) {
        ll opt = fastio::read(); l = fastio::read(), r = fastio::read();

        if (opt == 1) {
            x = fastio::read(), update(1);
        } else if (opt == 2) {
            x = fastio::read(), modify(1, 1);
        } else if (opt == 3) {
            x = fastio::read(), modify(1, 2);
        } else if (opt == 4) {
            fastio::write(query(1, 1)), puts("");
        } else if (opt == 5) {
            fastio::write(query(1, 2)), puts("");
        } else if (opt == 6) {
            fastio::write(query(1, 3)), puts("");
        }
    }
}

但是卡掉这种方法是简单的,我们需要一个复杂度正确的做法。也就是吉司机线段树。

那么什么是吉司机线段树?