1 条题解

  • 1
    @ 2023-1-31 9:53:24
    #include <bits/stdc++.h>
    using namespace std;
    int n, opt, l, r, c;
    int a[55004], b[55004];
    int SZ, CNT, ID[55004], L[1004], R[1004], lazy[1004];
    void update(int l, int r, int c)
    {
        if (ID[l] == ID[r])
        {
            for (int i = l; i <= r; i++)
                a[i] += c;
            for (int i = L[ID[l]]; i <= R[ID[l]]; i++)
                b[i] = a[i];
            sort(b + L[ID[l]], b + R[ID[l]] + 1);
            return;
        }
        for (int i = l; i <= R[ID[l]]; i++)
            a[i] += c;
        for (int i = L[ID[l]]; i <= R[ID[l]]; i++)
            b[i] = a[i];
        sort(b + L[ID[l]], b + R[ID[l]] + 1);
        for (int i = ID[l] + 1; i <= ID[r] - 1; i++)
            lazy[i] += c;
        for (int i = L[ID[r]]; i <= r; i++)
            a[i] += c;
        for (int i = L[ID[r]]; i <= R[ID[r]]; i++)
            b[i] = a[i];
        sort(b + L[ID[r]], b + R[ID[r]] + 1);
    }
    int query(int l, int r, int c)
    {
        int res = 0;
        c *= c;
        if (ID[l] == ID[r])
        {
            for (int i = l; i <= r; i++)
                res += (a[i] + lazy[ID[i]]) < c;
            return res;
        }
        for (int i = l; i <= R[ID[l]]; i++)
            res += (a[i] + lazy[ID[i]]) < c;
        for (int i = L[ID[r]]; i <= r; i++)
            res += (a[i] + lazy[ID[i]]) < c;
        for (int i = ID[l] + 1; i <= ID[r] - 1; i++)
            res += lower_bound(b + L[i], b + R[i] + 1, c - lazy[i]) - b - L[i];
        return res;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        SZ = sqrt(n);
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
            ID[i] = (i - 1) / SZ + 1;
        }
        CNT = n / SZ + (n % SZ > 0);
        for (int i = 1; i <= CNT; i++)
        {
            L[i] = (i - 1) * SZ + 1;
            R[i] = i * SZ;
            sort(b + L[i], b + R[i] + 1);
        }
        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) << "\n";
        }
        return 0;
    }
    
    • 1

    信息

    ID
    873
    时间
    500ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    95
    已通过
    20
    上传者