CSES Dynamic Range Sum Queries
// compile: make data
// run: ./data < data.in > data.out && diff data.out data.ans
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
#else
#define debug(x...)
#define Debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define double long double
#define int long long
#define MAXN 200010

struct SegTree {
    struct node {
        int sum, tag;
        int l, r;
        node *ch[2];
    };
    int cnt = 0;
    node *newnode(int l, int r) {
        node *x = &datalist[cnt++];
        x->l = l, x->r = r;
        x->ch[0] = x->ch[1] = nullptr;
        return x;
    }
    vector<int> a;
    vector<node> datalist;
    node *root;
    SegTree(vector<int> arr = {}) {
        datalist.resize(sz(arr) * 2);
        if (sz(arr)) a = arr, build();
    }
    void pushup(node *x) {
        if (!x->ch[0] && !x->ch[1]) return;
        auto getsum = [&](node *c) -> int {
            if (!c) return 0;
            if (!c->tag) return c->sum;
            return c->tag * (c->r - c->l + 1);
        };
        x->sum = getsum(x->ch[0]) + getsum(x->ch[1]);
    }
    void pushdown(node *x) {
        if (!x->tag) return;
        x->sum = x->tag * (x->r - x->l + 1);
        if (x->ch[0]) x->ch[0]->tag = x->tag;
        if (x->ch[1]) x->ch[1]->tag = x->tag;
        x->tag = 0;
    }
    int query(int ql, int qr) {
        function<long long(node*)> dfs = [&](node *x) -> int {
            if (ql > x->r || qr < x->l) return 0;
            pushdown(x);
            if (x->l >= ql && x->r <= qr) return x->sum;
            int lc = dfs(x->ch[0]), rc = dfs(x->ch[1]);
            return lc + rc;
        };
        return dfs(root);
    }
    void modify(int ql, int qr, int val) {
        function<void(node*)> dfs = [&](node *x) -> void {
            if (ql > x->r || qr < x->l) return;
            pushdown(x);
            if (x->l >= ql && x->r <= qr) {
                x->tag = val;
                return;
            }
            dfs(x->ch[0]), dfs(x->ch[1]);
            pushup(x);
        };
        dfs(root);
    }
    void build() {
        datalist.resize(sz(a) * 2);
        function<node*(int, int)> dfs = [&](int l, int r) {
            node *x = newnode(l, r);
            if (r > l) x->ch[0] = dfs(l, (l+r) >> 1), x->ch[1] = dfs(((l+r)>>1) + 1, r);
            else x->sum = a[l];
            pushup(x);
            return x;
        };
        root = dfs(0, sz(a)-1);
    }
    void graphviz_dump(string filename = "graph.dot") {
        FILE *f = fopen(filename.c_str(), "w");
        fprintf(f, "digraph {\n");
        rep(i, 0, cnt) {
            node *x = &datalist[i];
            fprintf(f, "    %lld", i, x->l, x->r);
            vector<string> desired = {
                "sum=" + to_string(x->sum),
                "tag=" + to_string(x->tag)
            };
            for (auto s: desired) fprintf(f, " | %s", s.c_str());
            fprintf(f, "\" shape=\"record\"];\n");
        }
        rep(i, 0, cnt) {
            node *x = &datalist[i];
            if (x->ch[0]) fprintf(f, "    %lld -> %ld;\n", i, x->ch[0] - &datalist[0]);
            if (x->ch[1]) fprintf(f, "    %lld -> %ld;\n", i, x->ch[1] - &datalist[0]);
        }
        fprintf(f, "}\n");
    }
};

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int n, q; cin >> n >> q;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    SegTree t(a);
    while (q--) {
        int op, l, r; cin >> op >> l >> r;
        if (op == 1) t.modify(l-1, l-1, r);
        else cout << t.query(l-1, r-1) << endl;
    }

    return 0;
}
No Comments

Send Comment Edit Comment


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