1 solutions

  • -3
    @ 2025-7-19 23:06:17

    放假回家 题解

    权值线段树。

    part 1

    显然会选取 1∼i−11 \sim i-1 中数字最小的若干个数字,若对每个 ii 都对 1∼i−11 \sim i-1 重新排序,或者使用插入排序后再统计答案,期望得分为 30 分。

    part 2

    但注意到第 4 第 5 个数据点 MM 小于等于 100,所以我们使用插入排序时可以只维护前 100 个数字(若存在 a[i]=0a[i]=0 的特殊处理)的排序后的结果就好,期望得分 50 分。

    part 3

    满分做法可以使用权值线段树,线段树上每个结点维护对应权值区间范围内有多少个数字以及这些数字的和是多少,查询时每次判断左子树对应区间范围内有多少数字,相加后的和是否超过限制,超过则向左递归否则向有递归。时间复杂度 O(NlogN)O(NlogN),期望得分 100 分。

    #include <bits/stdc++.h>
    using namespace std;
    #define endl '\n'
    #define ll long long
    const int N = 1000 + 10;
    const int maxn = 2e5 + 10;
    const int inf = 0x3f3f3f3f;
     
    struct DynamicTree
    {
        struct tree
        {
            int l, r;
            ll sum, cnt;
        } tr[10 * maxn];
        int idx = 0;
     
        void pushup(int p)
        {
            tr[p].sum = tr[tr[p].l].sum + tr[tr[p].r].sum;
            tr[p].cnt = tr[tr[p].l].cnt + tr[tr[p].r].cnt;
        }
     
        void update(int &p, int l, int r, int pos, int x)
        {
            if (!p)
                p = ++idx;
            if (l == r)
            {
                tr[p].sum += pos * x;
                tr[p].cnt += x;
                return;
            }
            int mid = l + r >> 1;
            if (pos <= mid)
                update(tr[p].l, l, mid, pos, x);
            else
                update(tr[p].r, mid + 1, r, pos, x);
            pushup(p);
        }
     
        ll query(int p, int l, int r, ll sum) // 统计区间内数的个数
        {
            if (!p)
                return 0;
            if (l == r)
                return sum / (tr[p].sum / tr[p].cnt);
            int mid = l + r >> 1;
            ll ans = 0;
            if (tr[tr[p].l].sum > sum)
                return query(tr[p].l, l, mid, sum);
            return tr[tr[p].l].cnt + query(tr[p].r, mid + 1, r, sum - tr[tr[p].l].sum);
        }
     
        int kth(int p, int l, int r, int k)
        {
            if (l == r)
                return l;
            int mid = l + r >> 1;
            if (tr[tr[p].l].cnt >= k)
                return kth(tr[p].l, l, mid, k);
            else
                return kth(tr[p].r, mid + 1, r, k - tr[tr[p].l].cnt);
        }
     
        void clear()
        {
            for (int i = 0; i <= idx; i++)
                tr[i] = {0, 0, 0, 0};
            idx = 0;
        }
    } t;
     
    int a[maxn];
     
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int T;
        cin >> T;
        while (T--)
        {
            ll n, m;
            cin >> n >> m;
            t.clear();
            for (int i = 1; i <= n; i++)
                cin >> a[i];
            int rt = 0, tot = 0;
            for (int i = 1; i <= n; i++)
            {
                ll ans = t.query(rt, 1, 1e9, m - a[i]);
                cout << tot - ans << ' ';
                if (a[i])
                {
                    t.update(rt, 1, 1e9, a[i], 1);
                    tot++;
                }
            }
            cout << endl;
        }
    }
    
    • 1

    Information

    ID
    9
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    (None)
    # Submissions
    36
    Accepted
    6
    Uploaded By