题解:P14379 【MX-S9-T2】「LAOI-16」摩天大楼

· · 题解

Blog

赛时怒挂 60pts,而且还是随机挂分。结果最后发现是自己树状数组写越界了。

比较小清新的题目,想清楚了计算方式应该不难实现。

首先这个式子显然是不可以毛毛虫火箭嗯维护的,我们结合部分分的特殊性质,观察一下它的性质:

使用 set 和 BIT 进行维护,即可做到 O(n\log n) 的时间复杂度。代码实现上可以分加入 1、加入 2、删除 1、删除 2 这四个函数进行实现,具体可以看代码。

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
using pi = pair<int, int>;
const int N = 1000005, inf = 0x3f3f3f3f;
int n, q, a[N];
set<int> pos1, pos2;
ll ans = 0, res1 = 0, res2 = 0;
int lowbit(int x)
{
    return (x & (-x));
}
struct BIT{
    int tr[N];
    void update(int p, int v)
    {
        p++;
        while(p <= n + 2)
        {
            tr[p] += v;
            p += lowbit(p);
        }
    }
    int query(int p)
    {
        p++;
        int res = 0;
        while(p)
        {
            res += tr[p];
            p -= lowbit(p);
        }
        return res;
    }
    int Rquery(int l, int r)
    {
        return query(r) - query(l - 1);
    }
}tr1;
void Add1(int p)
{
    auto nxtit = pos1.lower_bound(p);
    auto preit = prev(nxtit);
    int len = (*nxtit) - (*preit) - 1;
    res1 -= 1ll * len * (len - 1) / 2;
    len = p - (*preit) - 1;
    res1 += 1ll * len * (len - 1) / 2;
    len = (*nxtit) - p - 1;
    res1 += 1ll * len * (len - 1) / 2;

    nxtit = pos2.lower_bound(p);
    preit = prev(nxtit);
    res2 += tr1.Rquery(*preit, *nxtit);

    tr1.update(p, 1);
    pos1.insert(p);
}
void Add2(int p)
{
    auto nxtit = pos2.lower_bound(p);
    auto preit = prev(nxtit);
    res2 -= 1ll * tr1.Rquery(*preit, p) * tr1.Rquery(p, *nxtit);

    pos2.insert(p);
}
void Del1(int p)
{
    tr1.update(p, -1);
    pos1.erase(p);

    auto nxtit = pos1.lower_bound(p);
    auto preit = prev(nxtit);
    int len = (*nxtit) - (*preit) - 1;
    res1 += 1ll * len * (len - 1) / 2;
    len = p - (*preit) - 1;
    res1 -= 1ll * len * (len - 1) / 2;
    len = (*nxtit) - p - 1;
    res1 -= 1ll * len * (len - 1) / 2;

    nxtit = pos2.lower_bound(p);
    preit = prev(nxtit);
    res2 -= tr1.Rquery(*preit, *nxtit);
}
void Del2(int p)
{
    pos2.erase(p);

    auto nxtit = pos2.lower_bound(p);
    auto preit = prev(nxtit);
    res2 += 1ll * tr1.Rquery(*preit, p) * tr1.Rquery(p, *nxtit);
}
int main()
{
    // freopen("P14379.in", "r", stdin);
    // freopen("P14379.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    ans = res1 = 1ll * n * (n - 1) / 2;
    pos1.insert(0); pos1.insert(n + 1);
    pos2.insert(0); pos2.insert(n + 1);
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if(a[i] == 1) Add1(i);
        else if(a[i] == 2) Add2(i);
    }
    while(q--)
    {
        int x, y;
        cin >> x >> y;
        if(a[x] == y)
        {
            cout << ans - res1 - res2 << "\n";
            continue;
        }
        if(a[x] == 1) Del1(x);
        if(a[x] == 2) Del2(x);
        a[x] = y;
        if(a[x] == 1) Add1(x);
        if(a[x] == 2) Add2(x);
        cout << ans - res1 - res2 << "\n";  
    }
    return 0;
}