P4145 上帝造题的七分钟 2 / 花神游历各国

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
#define ls u << 1
#define rs u << 1 | 1

LL a[N];

struct Node {
    int l, r;
    LL sum;
} tr[N << 2];

void pushup(int u) {
    tr[u].sum = tr[ls].sum + tr[rs].sum;
}

void build(int u, int l, int r) {
    tr[u] = {l, r, 0};

    if (l == r) {
        tr[u].sum = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(ls, l, mid), build(rs, mid + 1, r);
    pushup(u);
}

void modify(int u, int l, int r) {
    if (tr[u].sum == (tr[u].r - tr[u].l + 1)) return;
    if (tr[u].l == tr[u].r) {
        tr[u].sum = sqrt(tr[u].sum);
        return;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid) modify(ls, l, r);
    if (r > mid) modify(rs, l, r);
    pushup(u);
}

LL query(int u, int l, int r) {
    if (r < tr[u].l || l > tr[u].r) return 0;
    if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum;
    return query(ls, l, r) + query(rs, l, r);
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n, q, cas = 1;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);

    cin >> q;
    while (q--) {
        int c, l, r;
        cin >> c >> l >> r;
        if (l > r) swap(l, r);
        if (c == 0)
            modify(1, l, r);
        else
            printf("%lld\n", query(1, l, r));
    }
    printf("\n");
    return 0;
}
posted @ 2022-04-28 11:17  糖豆爸爸  阅读(24)  评论(0编辑  收藏  举报
Live2D