P3157 [CQOI2011動的逆順(次元部分の問題を解決するCDQ順)

P3157 [CQOI2011]ダイナミック逆順

タイトル説明

逆の対数として定義される配列Aのための私が満たす<jは、とアイ> Ajが番号(i、j)の数です。nに1を与えて配置され、削除されたm個の要素の特定の順序に応じて、あなたのタスクは、前の要素のそれぞれ削除の全体の逆シーケンスの数をカウントすることです。

入力形式

最初の行の入力は二つの整数nおよびm、即ち、要素の初期数および除去要素の数を含んでいます。次のn行nと1との間の正の整数、すなわち、初期配向を含んでいます。除去し、各要素に続く各正の整数であり、次のm行。

出力フォーマット

各要素、逆の順序の番号を削除する前に続いてm本の出力線を備えます。

サンプル入力と出力

入力#1コピー

5 4
1
5
3
4
2
5
1
4
2

出力#1コピー

5
2
2
1

样例解释
(1,5,3,4,2)(1,3,4,2)(3,4,2)(3,2)(3)。

説明/ヒント

N <= 100000、M <= 50000

アイデア:

あなたが本当にCDQを理解していない場合、私はこれを見てお勧めします。

関連する問題点を解決するためのCDQの分割統治

そのは、三次元の半順序の問題を解決するために非常に裸CDQの分割統治であります

我々は逆の位置がフラット二次元座標系上に配置することができる、アレイ内の点は、POS xは、yは点の値を持っていることを知っています

二点$(X1、Y1)$、$(X2、Y2)$

満足している場合:

$ X1 <X2の$しばらく$ Y1> Y2 $

したがって、この時点では逆のペアです。

だから、3次元CDQの問題は、それぞれことがあります

時間は、X、Y座標、座標。

第一の溶液は、直接ソートを維持X昇順に解決マージソートを調整するために、ツリーデータ構造体溶液中のアレイのy座標。

時間複雑:$ O(N *ログイン^ 2(n))は$

コードには、いくつかのメモを持っています。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node {
    int t;
    int x;
    int y;
    node() {}
    node(int tt, int xx, int yy)
    {
        t = tt;
        x = xx;
        y = yy;
    }
    bool operator < (const node &bb )const
    {
        if (x != bb.x) {
            return x < bb.x;
        } else {
            return y < bb.y;
        }
    }
} a[maxn], b[maxn];
bool cmpx(node &aa, node &bb)
{
    return aa.t < bb.t;
}
int n, m;
int pos[maxn];
ll ans[maxn];

ll tree[maxn];
int lowbit(int x)
{
    return -x & x;
}
ll ask(int x)
{
    ll res = 0ll;
    while (x) {
        res += tree[x];
        x -= lowbit(x);
    }
    return res;
}
void add(int x, ll val)
{
    while (x < maxn) {
        tree[x] += val;
        x += lowbit(x);
    }
}
void clear(int x)
{
    while (x < maxn) {
        tree[x] = 0ll;
        x += lowbit(x);
    }
}
void cdq(int l, int r)
{
    if (l == r) {
        return ;
    }
    int mid = (l + r) >> 1;
    cdq(l, mid);
    cdq(mid + 1, r);
    int ql = l;
    int qr = mid + 1;
    // 计算在其左边,数值比其大的贡献
    //
    // 下面是类似归并排序方式计算贡献
    repd(i, l, r) {
        if (qr > r || (ql <= mid && a[ql] < a[qr])) {
            add(a[ql].y, 1);
            b[i] = a[ql++];
        } else {
            ans[a[qr].t] += ask(n) - ask(a[qr].y );
            b[i] = a[qr++];
        }
    }
    repd(i, l, mid) {
        clear(a[i].y);
    }
    repd(i, l, r) {
        a[i] = b[i];
    }
    // 计算在其右边,数值比其小的贡献
    // 因为上面已经归并排序过了,所以这个区间中是以x为升序的。
    for (int i = r; i >= l; --i) {
        if (a[i].t <= mid) {
            add(a[i].y, 1);
        } else {
            ans[a[i].t] += ask(a[i].y);
        }
    }
    repd(i, l, r) {
        clear(a[i].y);
    }
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    du2(n, m);
    repd(i, 1, n) {
        a[i].x = i;
        du1(a[i].y);
        pos[a[i].y] = i;
    }
    int tm = n;
    repd(i, 1, m) {
        int x;
        du1(x);
        a[pos[x]].t = tm--;
    }
    repd(i, 1, n) {
        if (!a[i].t) {
            a[i].t = tm--;
        }
    }
    sort(a + 1, a + 1 + n, cmpx);// 按照time排序,确保时间维度time是升序的。
    cdq(1, n);
    repd(i, 1, n) {
        ans[i] += ans[i - 1];
    }
    for (int i = n; i >= n - m + 1; --i) {
        printf("%lld\n", ans[i]);
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



おすすめ

転載: www.cnblogs.com/qieqiemin/p/11605173.html