1 条题解

  • 0
    @ 2024-03-21 20:20:51
    #include <cstdio>
    #include <algorithm>
    
    #define MAXN 100010
    #define ls(p) p << 1
    #define rs(p) p << 1 | 1
    #define ll long long
    #define inf 0x3f3f3f3f
    
    namespace Cirno {
        ll n, m;
        ll a[MAXN];
    
        struct SegmentTree {
            ll sum[MAXN << 2];
            ll Min[MAXN << 2];
            ll Max[MAXN << 2];
            ll tag[MAXN << 2];
    
            void push_up(ll p) {
                sum[p] = sum[ls(p)] + sum[rs(p)];
                Min[p] = std::min(Min[ls(p)], Min[rs(p)]);
                Max[p] = std::max(Max[ls(p)], Max[rs(p)]);
            }
    
            void build(ll p, ll l, ll r) {
                if (l == r) {
                    sum[p] = a[l], Min[p] = a[l], Max[p] = a[l];
                    return;
                }
                ll mid = (l + r) >> 1;
                build(ls(p), l, mid);
                build(rs(p), mid + 1, r);
                push_up(p);
            }
    
            void add_tag(ll p, ll l, ll r, ll k) {
                tag[p] += k;
                sum[p] += k * (r - l + 1);
            }
    
            void push_down(ll p, ll l, ll r) {
                if (tag[p]) {
                    ll mid = (l + r) >> 1;
                    add_tag(ls(p), l, mid, tag[p]);
                    add_tag(rs(p), mid + 1, r, tag[p]);
                    tag[p] = 0;
                }
            }
    
            void update(ll L, ll R, ll p, ll l, ll r, ll k) {
                if (L <= l && R >= r) {
                    add_tag(p, l, r, k);
                    return;
                }
                push_down(p, l, r);
                ll mid = (l + r) >> 1;
                if (L <= mid) update(L, R, ls(p), l, mid, k);
                if (R > mid) update(L, R, rs(p), mid + 1, r, k);
                push_up(p);
            }
    
            ll get_max(ll L, ll R, ll p, ll l, ll r) {
                if (L <= l && R >= r) return Max[p];
                push_down(p, l, r);
                ll mid = (l + r) >> 1;
                ll res = 0;
                if (L <= mid) res = std::max(res, get_max(L, R, ls(p), l, mid));
                if (R > mid) res = std::max(res, get_max(L, R, rs(p), mid + 1, r));
                return res;
            }
    
            ll get_min(ll L, ll R, ll p, ll l, ll r) {
                if (L <= l && R >= r) return Min[p];
                push_down(p, l, r);
                ll mid = (l + r) >> 1;
                ll res = inf;
                if (L <= mid) res = std::min(res, get_min(L, R, ls(p), l, mid));
                if (R > mid) res = std::min(res, get_min(L, R, rs(p), mid + 1, r));
                return res;
            }
    
            ll query(ll L, ll R, ll p, ll l, ll r) {
                if (L <= l && R >= r) return sum[p];
                push_down(p, l, r);
                ll mid = (l + r) >> 1;
                ll res = 0;
                if (L <= mid) res += query(L, R, ls(p), l, mid);
                if (R > mid) res += query(L, R, rs(p), mid + 1, r);
                return res;
            }
        } T;
    
        void init() {
            scanf("%lld %lld", &n, &m);
            for (int i = 1; i <= n; i++)
                scanf("%lld", &a[i]);
            for (int i = 1; i <= (n << 2); i++)
                T.Max[i] = 0, T.Min[i] = inf;
            T.build(1, 1, n);
            while (m--) {
                ll opt, l, r;
                scanf("%lld %lld %lld", &opt, &l, &r);
                if (opt == 1) {
                    printf("%lld\n", T.get_max(l, r, 1, 1, n));
                } else if (opt == 2) {
                    printf("%lld\n", T.get_min(l, r, 1, 1, n));
                } else if (opt == 3) {
                    ll k = T.get_max(l, r, 1, 1, n) - T.get_min(l, r, 1, 1, n);
                    T.update(l, r, 1, 1, n, k);
                } else {
                    printf("%lld\n", T.query(l, r, 1, 1, n) / (r - l + 1));
                }
            }
        }
    }
    
    int main() {
        Cirno::init();
        return 0;
    }
    

    //std

    [
        {
            "data": [
                {
                    "name": ["N", "M"],
                    "range": {
                        "N": [1, 8],
                        "M": [1, 10]
                    },
                    "count": 1
                },
                {
                    "name": ["a"],
                    "range": {
                        "a": [1, "N"]
                    },
                    "count": "N"
                },
                {
                    "name": ["opt", "L", "R"],
                    "range": {
                        "opt": [1, 4],
                        "L": [1, "N"],
                        "R": ["L", "N"]
                    },
                    "count": "M"
                }
            ],
            "count": 3,
            "memory_limit": 536870912,
            "time_limit": 1
        },
        {
            "data": [
                {
                    "name": ["N", "M"],
                    "range": {
                        "N": [1, 1000],
                        "M": [1, 10000]
                    },
                    "count": 1
                },
                {
                    "name": ["a"],
                    "range": {
                        "a": [1, "N"]
                    },
                    "count": "N"
                },
                {
                    "name": ["opt", "L", "R"],
                    "range": {
                        "opt": [1, 4],
                        "L": [1, "N"],
                        "R": ["L", "N"]
                    },
                    "count": "M"
                }
            ],
            "count": 4,
            "memory_limit": 536870912,
            "time_limit": 1
        },
        {
            "data": [
                {
                    "name": ["N", "M"],
                    "range": {
                        "N": [1, 100000],
                        "M": [1, 100000]
                    },
                    "count": 1
                },
                {
                    "name": ["a"],
                    "range": {
                        "a": [1, "N"]
                    },
                    "count": "N"
                },
                {
                    "name": ["opt", "L", "R"],
                    "range": {
                        "opt": [1, 4],
                        "L": [1, "N"],
                        "R": ["L", "N"]
                    },
                    "count": "M"
                }
            ],
            "count": 3,
            "memory_limit": 536870912,
            "time_limit": 1
        }
    ]
    
  • 1

琪露诺的完美算术教室 / 【模版】基础线段树

信息

ID
1049
难度
(无)
分类
线段树 点击显示
标签
递交数
0
已通过
0
通过率
?
被复制
1
上传者