多损啊
#include<iostream>
using namespace std;

namespace SegmentTree {
    using ll = long long;
    using cints = const ll&;
    using uint = unsigned;
    static ll* a;
    constexpr ll maxn = 1e5;
    struct node {
        struct son {
            static inline uint lft(cints root) { return root << 1; }
            static inline uint rit(cints root) { return root << 1 | 1; }
            //static inline const tuple<uint, uint, uint>& info(cints root, cints l, cints r) { return { son::lft(root), son::rit(root), midof(l,r) }; }
        };
        struct tags {
            template<node* seg>
            static inline void pushdown(cints root, cints l, cints r) {
                const auto ls = son::lft(root), rs = son::rit(root), mid = midof(l, r);
                auto& lc = seg[ls], &rc = seg[rs];
                if (auto& add = seg[root].tag.add; add) {
                    lc.tag.add += add;
                    rc.tag.add += add;
                    lc.val.sum += add * (mid - l + 1);
                    rc.val.sum += add * (r - mid);
                    add = 0;
                }
            }
            ll add;
        }tag;
        struct vals {
            ll sum;
            template<node* seg>
            static inline void update(cints root) { 
                seg[root].val.sum = 
                    seg[son::lft(root)].val.sum + seg[son::rit(root)].val.sum;
            }
        }val;
        static inline void tie(ll* Arr) { a = Arr; }
        static inline const unsigned midof(cints l, cints r) { return ((r - l) >> 1) + l; }
        template<node* seg>
        static void build(cints root, cints l, cints r) {
            seg[root].tag.add = 0;
            if (l == r) return void(seg[root].val.sum = a[l]);
            node::build<seg>(son::lft(root), l, midof(l, r)), node::build<seg>(son::rit(root), midof(l, r) + 1, r);
            vals::update<seg>(root);
        }
        struct gets {
            template<node* seg>
            static unsigned sumof(cints root, cints ml, cints mr, cints ql, cints qr) {

                if (ql > mr || qr < ml) return 0;
                if (ql <= ml && mr <= qr) return seg[root].val.sum;
                tags::pushdown<seg>(root, ml, mr);
                ll ret = 0;
                const auto ls = son::lft(root), rs = son::rit(root), mid = midof(ml, mr);
                auto& lc = seg[ls], rc = seg[rs];
                if (mid >= ql)
                    ret += gets::sumof<seg>(ls, ml, mid, ql, qr);
                if (qr > mid)
                    ret += gets::sumof<seg>(rs, mid + 1, mr, ql, qr);
                return ret;
            }
            template<node* seg>
            static const vals pointof(cints root, cints l, cints r, cints on) {
                if (l == on && r == on) return seg[root].val;
                tags::pushdown<seg>(root, l, r);
                if (auto mid = midof(l, r); on <= mid) return gets::pointof<seg>(son::lft(root), l, mid, on);
                else return gets::pointof<seg>(son::rit(root), mid + 1, r, on);
            }
        };
        struct update {
            template<node* seg>
            static inline void add(cints root, cints ml, cints mr, cints ql, cints qr, cints val) {
                if (ql > mr || qr < ml) return;
                if (ql <= ml && qr >= mr)
                    return void(seg[root].tag.add += val),
                    void(seg[root].val.sum += val * (mr - ml + 1));
                tags::pushdown<seg>(root, ml, mr);
                update::add<seg>(son::lft(root), ml, midof(ml, mr), ql, qr, val);
                update::add<seg>(son::rit(root), midof(ml, mr) + 1, mr, ql, qr, val);
                vals::update<seg>(root);
            }
        };
    } nnd[maxn];
}using namespace SegmentTree;
ll arr[maxn], n, m, k, x, y, opt;
int main() {
    ios::sync_with_stdio(0), cin.tie(0), std::cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> arr[i];
    node::tie(arr);
    node::build<nnd>(1, 1, n);
    while (m--) {
        if (cin >> opt; opt == 2) {
            cin >> x >> y;
            cout << node::gets::sumof<nnd>(1, 1, n, x, y) << '\n';
        }
        else { 
            cin >> x >> y >> k;
            node::update::add<nnd>(1, 1, n, x, y, k); 
        }
    }
    return 0;
}

代码20行,auto\&\ a,b^{^{^{{TM}}}} aauto&bauto

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇