1 条题解

  • 1
    @ 2024-11-18 23:55:11
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int N = 100010;
    LL a[N], b[N], s[N];
    int n, m;
    LL res, ans;
    void insert(int l, int r)
    {
        b[l] += 1;
        b[r + 1] -= 1;
    }
    int main()
    {
        cin >> n;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%lld", &a[i]);
            s[i] += s[i - 1] + a[i];
        }
        cin >> m;
        for (int i = 1; i <= m; ++i)
        {
            int l, r;
            scanf("%d%d", &l, &r);
            insert(l, r);
            res += (s[r] - s[l - 1]);
        }
        //cout<<res<<endl;
        for (int i = 1; i <= n; ++i) b[i] += b[i - 1];
        sort(b + 1, b + n + 1);
        sort(a + 1, a + n + 1);
        for (int i = 1; i <= n; ++i)
        {
            ans += b[i] * a[i];
        }
        cout << ans - res << endl;
        return 0;
    }
    

    信息

    ID
    5490
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    (无)
    递交数
    1
    已通过
    2
    上传者