P3157 [CQOI2011] 动态逆序对 题解

题目链接:动态逆序对

常见经典题,理解一个数对逆序对贡献计算即可。

对于一个数而言,它在一个序列中的逆序对有两部分贡献,一部分为前面比它严格大的数,另一部分为后面比它严格小的数,有道二莫题也是基于此去考虑的。

考虑最开始知道了总逆序数,每次删除一个数逆序数会减少两部分值,显然就是上述说的两部分值,那么我们基于答案差分。这玩意也是二莫最常见的思想,就是统计每个答案较上一个答案而言减少了的多少,最后前缀和就能得到确切答案。

然后考虑两种贡献分别是哪些数,考虑三个方面,下标,删除时间,值大小:

  1. \(idx<curr,val>curr,delTime>curr\)\(curr\) 表示当前位置对应的信息。

  2. \(idx>curr,val<curr,delTime>curr\)\(curr\) 表示当前位置对应的信息。

那么第一个序很好处理我们不动就行了。注意到左边的数可以对当前数贡献,右边的数也可以对当前数贡献,所以我们 cdq 分治考虑左对右和右对左计算两次偏序贡献。第二维我们考虑删除时间降序排列。最后一维查询,我们考虑树状数组上查询和值域有关的节点信息即可。初始的,将没有被删除的点的删除时间设为最后一次删除之后即可。删除时间 \(\le m\) 也即为查询,不需要单独拿个 \(id\) 记录。最后记得,奇葩询问,问的是删除之......\(ans[i-1]\) 才是答案。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 1e5 + 10;
int n, m, x;
int bit[N];

inline void add(int x, const int val)
{
    while (x <= n)bit[x] += val, x += lowBit(x);
}

inline int query(int x)
{
    int ans = 0;
    while (x)ans += bit[x], x -= lowBit(x);
    return ans;
}

int pos[N];
ll ans[N];

struct Node
{
    int val, delTime;

    bool operator<(const Node& other) const
    {
        return delTime > other.delTime;
    }
} node[N];

inline void cdq(const int L, const int R)
{
    const int mid = L + R >> 1;
    if (L == R)return;
    cdq(L, mid), cdq(mid + 1, R);
    stable_sort(node + L, node + mid + 1), stable_sort(node + mid + 1, node + R + 1);
    int l = L;
    forn(r, mid+1, R)
    {
        auto [val,delTime] = node[r];
        while (l <= mid and node[l].delTime >= delTime)add(node[l++].val, 1);
        if (delTime <= m)ans[delTime] -= l - L - query(val);
    }
    forn(i, L, l-1)add(node[i].val, -1);
    int r = mid + 1;
    forn(l, L, mid)
    {
        auto [val,delTime] = node[l];
        while (r <= R and node[r].delTime >= delTime)add(node[r++].val, 1);
        if (delTime <= m)ans[delTime] -= query(val - 1);
    }
    forn(i, mid+1, r-1)add(node[i].val, -1);
}

inline void solve()
{
    cin >> n >> m;
    forn(i, 1, n)
    {
        cin >> x, node[i].val = x, node[i].delTime = m + 1, pos[x] = i;
        ans[0] += i - 1 - query(x), add(x, 1);
    }
    memset(bit, 0, sizeof bit);
    forn(i, 1, m)
    {
        cin >> x;
        node[pos[x]].delTime = i;
    }
    cdq(1, n);
    forn(i, 1, m)ans[i] += ans[i - 1];
    forn(i, 1, m)cout << ans[i - 1] << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[时间复杂度为:\ O(n\log^2{n}) \]

posted @ 2024-02-16 21:49  Athanasy  阅读(20)  评论(0编辑  收藏  举报