[LOJ6281] Introduction to Sequence Block 5-Block

Given a sequence of length \ (n \) \ (a_1, ..., a_n \) , there are \ (n \) operations, including rounding off the square root of the interval and summing the intervals

Solution

Considering that a number squared \ (\ log \) times will become \ (1 \) , so we record a value for each intervalmx

If an interval of mxnot more than \ (1 \) , then obviously we would not need to perform the operation range of prescribing

Otherwise, we violently prescribe all elements in this interval

Since each interval will be violently predicated \ (\ log V \) times, each element will be violently calculated no more than \ (O (n \ log V) \)

Overall time complexity \ (O (n (\ sqrt n + \ log V)) \)

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 200005;

int n, len, mx[N], a[N], b[N], s[N];

signed main() {
    ios::sync_with_stdio(false);
    cin >> n;
    len = ceil(sqrt(n));
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) b[i] = (i - 1) / len + 1;
    for (int i = 1; i <= n; i++) s[b[i]] += a[i], mx[b[i]] = max(mx[b[i]], a[i]);
    for (int i = 1; i <= n; i++) {
        int opt, l, r, c;
        cin >> opt >> l >> r >> c;
        if (!opt) {
            if (b[l] == b[r]) {
                for (int i = l; i <= r; i++) a[i] = sqrt(a[i]);
                mx[b[l]] = 0;
                s[b[l]] = 0;
                for (int i = b[l] * len - len + 1; i <= b[l] * len; i++)
                    mx[b[i]] = max(mx[b[i]], a[i]), s[b[i]] += a[i];
            } else {
                for (int i = b[l] + 1; i < b[r]; i++) {
                    if (mx[i] > 1) {
                        mx[i] = 0;
                        s[i] = 0;
                        for (int j = i * len - len + 1; j <= i * len; j++)
                            a[j] = sqrt(a[j]), mx[i] = max(mx[i], a[j]), s[i] += a[j];
                    }
                }
                for (int i = l; i <= b[l] * len; i++) a[i] = sqrt(a[i]);
                mx[b[l]] = 0;
                s[b[l]] = 0;
                for (int i = b[l] * len - len + 1; i <= b[l] * len; i++)
                    mx[b[i]] = max(mx[b[i]], a[i]), s[b[i]] += a[i];
                for (int i = b[r] * len - len + 1; i <= r; i++) a[i] = sqrt(a[i]);
                mx[b[r]] = 0;
                s[b[r]] = 0;
                for (int i = b[r] * len - len + 1; i <= b[r] * len; i++)
                    mx[b[i]] = max(mx[b[i]], a[i]), s[b[i]] += a[i];
            }
        } else {
            int ans = 0;
            if (b[l] == b[r]) {
                for (int i = l; i <= r; i++) ans += a[i];
            } else {
                for (int i = b[l] + 1; i < b[r]; i++) ans += s[i];
                for (int i = l; i <= b[l] * len; i++) ans += a[i];
                for (int i = b[r] * len - len + 1; i <= r; i++) ans += a[i];
            }
            cout << ans << endl;
        }
    }
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12702922.html