题解:P12685 [国家集训队] 排队 加强版

· · 题解

使用蛮力,二维树状数组维护动态逆序对。

使劲卡自己的哈希,然后就可以过。

把序列想象为二维平面上的点,横坐标为下标,纵坐标为函数值。

所有操作都可以抽象为“加点”和“删点”。

“加点”为将答案加上这个点和别的点产生的逆序对数量,然后更新树状数组。

“删点”为将答案减去这个点和别的点产生的逆序对数量,然后更新树状数组。

更新树状数组好写。一个点和别的点产生的逆序对数就是这个点左上和右下的点的数量,这个东西是树状数组可以解决的。

二维树状数组的哈希需要自己写,稍微某个参数调的不对就会 TLE,乱交好几页就可以撞击出一个可以过的哈希参数组了。

具体逻辑请欣赏我的不如粑粑的代码。

#include <set>
#include <iostream>
#include <map>
#include <math.h>
#include <cstdint>
#define lowbit(x) ((x)&-(x))
using namespace std;
uint32_t n, a[200005];
long long ans;
#define P 81108863
int *pool = (int*)((void*)(new long long[(size_t)(P * 1.2) >> 1]));
unsigned long long X[(size_t)(P * 1.2)];
int bit1[200005];
#define A 4376135492512386ull
#define B 295623ull
inline void update(uint32_t x, const uint32_t &y, const int &K) {
    while (x <= n) {
        for (uint32_t i = y; i <= n; i += lowbit(i)) {
            uint32_t k = (x * A ^ i * B) % P;
            const uint64_t &k2 = x ^ uint64_t(i) << 20;
            while (X[k] && (X[k] ^ k2)) {
                k++;
            }
            X[k] = k2;
            pool[k] += K;
        }
        x += lowbit(x);
    }
}
inline int query(uint32_t x, const uint32_t &y) {
    int ans = 0;
    while (x) {
        for (uint32_t i = y; i; i ^= lowbit(i)) {
            uint32_t k = (x * A ^ i * B) % P;
            const uint64_t &k2 = x ^ uint64_t(i) << 20;
            while (X[k] && (X[k] ^ k2)) {
                k++;
            }
            ans += pool[k];
        }
        x ^= lowbit(x);
    }
    return ans;
}
inline int getans(const uint32_t &i, const uint32_t &x) {
    return i - 1 - query(i - 1, x) + query(n, x - 1) - query(i, x - 1);
}
signed main() {
    // ios::sync_with_stdio(0);
    // cin.tie(0);
    // cout.tie(0);
//  freopen("P12685.in", "r", stdin);
//  freopen("out2", "w", stdout);
    scanf("%d", &n);
    for (uint32_t i = 1; i <= n; i++) {
        scanf("%d", a + i);
    }
    set<uint32_t> st(a + 1, a + n + 1);
    uint32_t cnt = 0;
    map<uint32_t, uint32_t> mp;
    for (set<uint32_t>::iterator i = st.begin(); i != st.end(); i++) {
        mp[*i] = ++cnt;
    }
    for (uint32_t i = 1; i <= n; i++) {
        a[i] = mp[a[i]];
        ans += i - 1;
        for (int j = a[i]; j; j ^= lowbit(j)) {
            ans -= bit1[j];
        }
        for (int j = a[i]; j <= n; j += lowbit(j)) {
            bit1[j]++;
        }
        update(i, a[i], 1);
    }
    printf("%lld\n", ans);
    uint32_t T, u, v;
    scanf("%d", &T);
    // T /= 2;
    while (T--) {
        scanf("%d%d", &u, &v);
        if (u > v) {
            swap(u, v);
        }
        uint32_t au = a[u], av = a[v];
        swap(a[u], a[v]);
        ans -= getans(u, au);
        update(u, au, -1);
        update(u, av, 1);
        ans += getans(u, av);
        ans -= getans(v, av);
        update(v, av, -1);
        update(v, au, 1);
        ans += getans(v, au);
        printf("%lld\n", ans);
    }
    return 0;
}

Update 20250713

其实我们只需要关注中间的点就可以,因为其他地方的点无贡献。具体的贡献除了 (u,v) 这一对点,只有下面的这些。

统计二维平面上的子矩形数点,继续用二维树状数组,然后我们对每一个权值开一个平衡树维护上面的那两条的贡献。这样瓶颈处就是 8 次二维树状数组操作,可以更轻松第通过。

#include <set>
#include <cstring>
#include <iostream>
#include <map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define lowbit(x) ((x)&-(x))
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
__gnu_pbds::tree<u32,
                 __gnu_pbds::null_type,
                 less<int>,
                 __gnu_pbds::rb_tree_tag,
                 __gnu_pbds::tree_order_statistics_node_update> pos[200005];
u32 n, T, u, v, a[200005];
u64 ans;
u32 bit[200005];
#define P 71108863
#define A 4376135492512386ull
#define B 295623ull
u32 pool[(size_t)(P * 1.2)];
u64 X[(size_t)(P * 1.2)];
inline u32 query(u32 x, u32 y) {
    // i<=x, j<=y
    u32 ans = 0;
    while (x) {
        for (u32 i = y; i; i ^= lowbit(i)) {
            u32 k = (x * A ^ i * B) % P;
            u64 k2 = x ^ u64(i) << 20;
            while (X[k] && (X[k] ^ k2)) {
                k++;
            }
            ans += pool[k];
        }
        x ^= lowbit(x);
    }
    return ans;
}
inline void update(u32 x, u32 y, u32 K) {
    // i<=x, j<=y, +=K
    while (x <= n) {
        for (u32 i = y; i <= n; i += lowbit(i)) {
            u32 k = (x * A ^ i * B) % P;
            u64 k2 = x ^ u64(i) << 20;
            while (X[k] && (X[k] ^ k2)) {
                k++;
            }
            X[k] = k2;
            pool[k] += K;
        }
        x += lowbit(x);
    }
}
signed main() {
    scanf("%u", &n);
    for (u32 i = 1; i <= n; ++i) {
        scanf("%u", a + i);
    }
    set<u32> st(a + 1, a + n + 1);
    u32 cnt = 0;
    map<u32, u32> mp;
    for (set<u32>::iterator i = st.begin(); i != st.end(); i++) {
        mp[*i] = ++cnt;
    }
    for (u32 i = 1; i <= n; ++i) {
        a[i] = mp[a[i]];
        pos[a[i]].insert(i);
        ans += i - 1;
        for (u32 j = a[i]; j; j ^= lowbit(j)) {
            ans -= bit[j];
        }
        for (u32 j = a[i]; j <= n; j += lowbit(j)) {
            bit[j]++;
        }
        update(i, a[i], 1);
    }
    printf("%llu\n", ans);
    scanf("%u", &T);
    while (T--) {
        scanf("%u%u", &u, &v);
        if (a[u] == a[v]) {
            printf("%llu\n", ans);
            continue;
        }
        if (u > v) {
            swap(u, v);
        }
        if (a[u] < a[v]) {
            ++ans;
        } else {
            --ans;
        }
        update(u, a[u], -1);
        update(v, a[v], -1);
        pos[a[u]].erase(u);
        pos[a[v]].erase(v);
        if (u + 1 == v) {
            swap(a[u], a[v]);
            update(u, a[u], 1);
            update(v, a[v], 1);
            pos[a[u]].insert(u);
            pos[a[v]].insert(v);
            printf("%llu\n", ans);
            continue;
        }
        if (a[u] < a[v]) {
            ans += pos[a[u]].order_of_key(v) - pos[a[u]].order_of_key(u);
            ans += pos[a[v]].order_of_key(v) - pos[a[v]].order_of_key(u);
        } else {
            ans -= pos[a[u]].order_of_key(v) - pos[a[u]].order_of_key(u);
            ans -= pos[a[v]].order_of_key(v) - pos[a[v]].order_of_key(u);
        }
        if (min(a[u], a[v]) + 1 == max(a[u], a[v])) {
            swap(a[u], a[v]);
            update(u, a[u], 1);
            update(v, a[v], 1);
            pos[a[u]].insert(u);
            pos[a[v]].insert(v);
            printf("%llu\n", ans);
            continue;
        }
        u32 mid_cnt = query(v, max(a[u], a[v]) - 1)
                    - query(v, min(a[u], a[v]))
                    - query(u, max(a[u], a[v]) - 1)
                    + query(u, min(a[u], a[v]));
        // cout << "mid_cnt " << mid_cnt << endl;
        if (a[u] < a[v]) {
            ans += mid_cnt << 1;
        } else {
            ans -= mid_cnt << 1;
        }
        swap(a[u], a[v]);
        update(u, a[u], 1);
        update(v, a[v], 1);
        pos[a[u]].insert(u);
        pos[a[v]].insert(v);
        printf("%llu\n", ans);
    }
    return 0;
}