1 条题解

  • 0
    @ 2023-9-30 15:11:47

    一个“简单”的整数问题

    #define _CRT_SECURE_NO_WARNINGS 1
    #include <bits/stdc++.h>
    
    using namespace std;
    //#define int long long
    //#define map unordered_map
    typedef long long ll;
    const int N = 1e5 + 10, M = 350;
    int n, m;
    int a[N];
    ll sum[M], add[M];
    int len;
    
    int get(int i)
    {
        return i / len;
    }
    
    ll ask(int l, int r)
    {
        ll res = 0;
        int L = get(l), R = get(r);
        if (L == R)
        {
            for (int i = l; i <= r; ++i)
            {
                int I = get(i);
                res += (a[i] + add[I]);
            }
        }
        else
        {
            int i = l, j = r, I, J;
            while ((I = get(i)) == L) {
                res += (a[i] + add[I]), ++i;
            }
            while ((J = get(j)) == R) {
                res += (a[j] + add[J]), --j;
            }
            for (int k = I; k <= J; ++k) {
                res += sum[k];
            }
        }
        return res;
    }
    
    void modify(int l, int r, int d)
    {
        int L = get(l), R = get(r);
        if (L == R)
        {
            for (int i = l; i <= r; ++i)
            {
                int I = get(i);
                a[i] += d, sum[I] += d;
            }
        }
        else
        {
            int i = l, j = r, I, J;
            while ((I = get(i)) == L) {
                a[i] += d, sum[I] += d, ++i;
            }
            while ((J = get(j)) == R) {
                a[j] += d, sum[J] += d, --j;
            }
            for (int k = I; k <= J; ++k){
                sum[k] += (len * d), add[k] += d;
            }
        }
    }
    
    signed main()
    {
        cin >> n >> m;
        len = n / sqrt(n);
        for (int i = 1; i <= n; ++i)
        {
            int I = get(i);
            scanf("%d", &a[i]);
            sum[I] += a[i];
        }
    
        while (m--)
        {
            char op[2]; int l, r, d;
            scanf("%s%d%d", op, &l, &r);
            if (*op == 'Q')
            {
                printf("%lld\n", ask(l, r));
            }
            else
            {
                scanf("%d", &d);
                modify(l, r, d);
            }
        }
    
        return 0;
    }
    
    
    • 1

    信息

    ID
    154
    时间
    1000ms
    内存
    128MiB
    难度
    8
    标签
    递交数
    72
    已通过
    11
    上传者