1 条题解

  • 1
    @ 2023-1-31 9:54:02
    #include <bits/stdc++.h>
    #define int long long
    using namespace std;
    int n, opt, l, r, c;
    int a[50004];
    int SZ, CNT, ID[50004], L[1004], R[1004], lazy[1004], sum[1004];
    void update(int l, int r, int c)
    {
        if (ID[l] == ID[r])
        {
            for (int i = l; i <= r; i++)
                a[i] += c, sum[ID[l]] += c;
            return;
        }
        for (int i = l; i <= R[ID[l]]; i++)
            a[i] += c, sum[ID[l]] += c;
        for (int i = ID[l] + 1; i <= ID[r] - 1; i++)
            lazy[i] += c, sum[i] += c * SZ;
        for (int i = L[ID[r]]; i <= r; i++)
            a[i] += c, sum[ID[r]] += c;
    }
    // sum(a[l],a[r]) % c
    int query(int l, int r, int c)
    {
        int res = 0;
        if (ID[l] == ID[r])
        {
            for (int i = l; i <= r; i++)
                res = (res + a[i] + lazy[ID[l]]) % c;
            return res;
        }
        for (int i = l; i <= R[ID[l]]; i++)
            res = (res + a[i] + lazy[ID[l]]) % c;
        for (int i = ID[l] + 1; i <= ID[r] - 1; i++)
            res = (res + sum[i]) % c;
        for (int i = L[ID[r]]; i <= r; i++)
            res = (res + a[i] + lazy[ID[r]]) % c;
        return res;
    }
    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        SZ = sqrt(n);
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            // 1: [1, SZ], 2: [SZ + 1, 2SZ]
            ID[i] = (i - 1) / SZ + 1;
            sum[ID[i]] += a[i];
        }
        CNT = n / SZ + (n % SZ > 0);
        for (int i = 1; i <= CNT; i++)
        {
            L[i] = (i - 1) * SZ + 1;
            R[i] = i * SZ;
        }
        for (int i = 1; i <= n; i++)
        {
            cin >> opt >> l >> r >> c;
            if (opt == 0)
                update(l, r, c);
            else
                cout << query(l, r, c + 1) << "\n";
        }
        return 0;
    }
    
    • 1

    信息

    ID
    875
    时间
    500ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    57
    已通过
    16
    上传者