题解:AT_abc440_f [ABC440F] Egoism

· · 题解

Blog

写这题的感觉堪比吃了一斤鸵鸟史。

注意到乘积的其中一项只能是 1, 2,考虑直接把 2 的系数分配给心情值大的马。

一般情况下这些 2 是可以全部分完的,但是如果 2 的系数全在心情值最大的那几个身上,就有一个无法被分完。此时需要删掉心情值最大的那几个中,最小的心情值;然后再加上其余心情值里,最大的心情值。

可以使用权值线段树 + 线段树上二分维护。细节很多,讲了也没啥用,直接看代码吧。

时间复杂度 O(n\log V)。

#include <bits/stdc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc (p << 1)
#define rc ((p << 1) | 1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef __int128 i128;
using pi = pair<int, int>;
const int N = 1000005, V = 1000000 + 1;
struct Node{
    int l, r;
    ll sm, cnt2, mx, num;
};
struct Segtree{
    Node tr[4 * N];
    void pushup(Node &p, Node ls, Node rs)
    {
        p.sm = ls.sm + rs.sm;
        p.cnt2 = ls.cnt2 + rs.cnt2;
        p.mx = max(ls.mx, rs.mx);
        p.num = ls.num + rs.num;
    }
    void build(int p, int ln, int rn)
    {
        tr[p] = {ln, rn, 0, 0, 0, 0};
        if(ln == rn) return;
        int mid = (ln + rn) >> 1;
        build(lc, ln, mid);
        build(rc, mid + 1, rn);
        pushup(tr[p], tr[lc], tr[rc]);
    }
    void update(int p, int pos, int typ, int x)
    {
        if(tr[p].l == pos && tr[p].r == pos)
        {
            if(typ == 1) tr[p].sm += x * pos, tr[p].num += x;
            else tr[p].cnt2 += x;
            if(tr[p].num == 0) tr[p].mx = 0;
            else tr[p].mx = pos;
            return;
        }
        int mid = (tr[p].l + tr[p].r) >> 1;
        if(pos <= mid) update(lc, pos, typ, x);
        else update(rc, pos, typ, x);
        pushup(tr[p], tr[lc], tr[rc]);
    }
    Node query(int p, int ln, int rn)
    {
        if(ln <= tr[p].l && tr[p].r <= rn) return tr[p];
        int mid = (tr[p].l + tr[p].r) >> 1;
        if(rn <= mid) return query(lc, ln, rn);
        if(ln >= mid + 1) return query(rc, ln, rn);
        Node tmp;
        pushup(tmp, query(lc, ln, rn), query(rc, ln, rn));
        return tmp;
    }
    int binary(int p, int k, int &cur)
    {
        if(cur + tr[p].num < k)
        {
            cur += tr[p].num;
            return -1;
        }
        if(tr[p].l == tr[p].r) return tr[p].l;
        int res = binary(lc, k, cur);
        if(res != -1) return res;
        res = binary(rc, k, cur);
        return res;
    }
} tr1;
ll n, q, a[N], b[N], cnt2;
void solve()
{
    ll lst = n - cnt2;
    int tmp = 0;
    int p = tr1.binary(1, lst, tmp);
    int pre = tr1.query(1, 0, p).cnt2;
    ll ans = tr1.tr[1].sm;
    if(cnt2 == 0)
    {
        cout << ans << "\n";
        return;
    }
    ans += tr1.query(1, p + 1, V).sm;
    tmp = 0;
    int nowp = tr1.binary(1, tr1.query(1, 0, p).num + 1, tmp);
    if(!pre)
    {
        tmp = 0;
        int nowp = tr1.binary(1, tr1.query(1, 0, p).num + 1, tmp);
        ans -= nowp;
        ans += tr1.query(1, 0, p).mx;
    }
    else
        ans += (cnt2 - tr1.query(1, p + 1, V).num) * p;
    cout << ans << "\n";
}
int main()
{
    //freopen("sample.in", "r", stdin);
    //freopen("sample.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    tr1.build(1, 0, V);
    cin >> n >> q;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i] >> b[i];
        tr1.update(1, a[i], 1, 1);
        if(b[i] == 2) tr1.update(1, a[i], 2, 1), cnt2++;
    }
    for(int i = 1; i <= q; i++)
    {
        int id, x, y;
        cin >> id >> x >> y;
        tr1.update(1, a[id], 1, -1);
        if(b[id] == 2) tr1.update(1, a[id], 2, -1), cnt2--;
        a[id] = x; b[id] = y;
        tr1.update(1, a[id], 1, 1);
        if(b[id] == 2) tr1.update(1, a[id], 2, 1), cnt2++;        
        solve();
    }
    return 0;
}