8.20

· · 个人记录

cdq分治

该算法常用于解决偏序问题,分三步:

1、分治 (l, r) 为 (l, mid) 和 (mid + 1, r)

2、计算 (mid + 1, r) 与 (l, mid) 之间的贡献

3、递归 (l, mid) 和 (mid + 1, r)

时间复杂度为 O(n \log^2 n)

题目

P3810

板子题,对第一维排序,再对第二维归并,再用树状数组对第三维算答案即可。注意一下去重即可。

#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e5 + 10;

struct node{
    int a, b, c, s, x;
}a[N];

int n, k, m, tr[N], ans[N];

void add(int x, int y){
    for(; x <= k; x += (x & -x)) tr[x] += y;
}

int query(int x){ int res = 0;
    for(; x; x -= (x & -x)) res += tr[x];
    return res;
}

bool cmp(node a, node b){
    return a.b < b.b;
}

void cdq(int l, int r){
    if(l >= r) return ;
    int mid = (l + r) >> 1;
    cdq(l, mid), cdq(mid + 1, r);
    sort(a + l, a + mid + 1, cmp);
    sort(a + mid + 1, a + r + 1, cmp);
    int i = l, j = mid + 1;
    while(i <= mid && j <= r){
        if(a[j].b >= a[i].b){
            add(a[i].c, a[i].s), i++;
        }else{
            a[j].x += query(a[j].c), j++;
        }
    }
    while(i <= mid) add(a[i].c, a[i].s), i++;
    while(j <= r) a[j].x += query(a[j].c), j++;
    for(int i = l; i <= mid; i++) add(a[i].c, -a[i].s);
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> m >> k;
    for(int i = 1, x, y, z; i <= m; i++){
        cin >> x >> y >> z; a[i] = {x, y, z};
    }
    sort(a + 1, a + 1 + m, [](const node &a, const node &b){
        return a.a < b.a || (a.a == b.a && (a.b < b.b || a.b == b.b && a.c < b.c));
    });
    for(int i = 1, s = 0; i <= m; i++){ s++;
        if(a[i].a != a[i + 1].a || a[i].b != a[i + 1].b || a[i].c != a[i + 1].c) a[++n] = a[i], a[n].s = s, s = 0;
    } 
    cdq(1, n); 
    for(int i = 1; i <= n; i++){
        ans[a[i].x + a[i].s] += a[i].s;
    }
    for(int i = 1; i <= m; i++){
        cout << ans[i] << '\n';
    }
    return 0;
}

P3157

把下标、a 值和删除顺序作为三维然后两边cdq分治算出每个点影响的逆序对数量算出来,再挨个减掉即可。

#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e5 + 10;

struct node{
    int a, b, c;
}a[N];

int n, k, m, tr[N], id[N], ans[N];

void add(int x, int y){
    for(; x <= n + 1; x += (x & -x)) tr[x] += y;
}

int query(int x){ int res = 0;
    for(; x; x -= (x & -x)) res += tr[x];
    return res;
}

bool cmp(node a, node b){
    return a.b < b.b;
}

void cdq(int l, int r){
    if(l >= r) return ;
    int mid = (l + r) >> 1;
    cdq(l, mid), cdq(mid + 1, r);
    sort(a + l, a + mid + 1, cmp);
    sort(a + mid + 1, a + r + 1, cmp);
    int i = l, j = mid + 1;
    while(i <= mid && j <= r){
        if(a[j].b >= a[i].b) add(a[i].c, 1), i++;
        else ans[a[j].c] += i - l - query(a[j].c), j++;
    }
    while(i <= mid) add(a[i].c , 1), i++;
    while(j <= r) ans[a[j].c] += i - l - query(a[j].c), j++;
    for(int i = l; i <= mid; i++) add(a[i].c, -1);
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m; int sum = 0;
    for(int i = 1; i <= n; i++){
        cin >> a[i].a; a[i].c = m + 1;
        id[a[i].a] = i, a[i].b = i;
        sum += query(n) - query(a[i].a), add(a[i].a, 1);
    }
    for(int i = 1; i <= n + 2; i++) tr[i] = 0;
    for(int i = 1, x; i <= m; i++){
        cin >> x; 
        a[id[x]].c = i;
    }
    sort(a + 1, a + 1 + n, [](const node &a, const node &b){
        return a.a > b.a;
    });
    cdq(1, n);
    for(int i = 1; i <= n; i++) a[i].b = n - a[i].b + 1;
    sort(a + 1, a + 1 + n, [](const node &a, const node &b){
        return a.a < b.a;
    });
    cdq(1, n);
    for(int i = 1; i <= m; i++) cout << sum << '\n', sum -= ans[i];
    return 0;
}

P4169

将 x,y 坐标与加入顺序作为三维,然后对于四个象限利用坐标变换到统一象限计算最小值,偏序后直接用树状数组维护 x+y 最大值即可。

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 10, M = 1e6 + 10;

struct node{
    int a, b, c, op;
}a[N];

int n, k, m, tr[M], ans[N];

void add(int x, int y){
    for(; x < M; x += (x & -x)) tr[x] = max(tr[x], y);
}

int query(int x){ int res = -1e9;
    for(; x; x -= (x & -x)) res = max(tr[x], res);
    return res;
}

void zrr(int x){
    for(; x < M; x += (x & -x)) tr[x] = -1e9;
}

bool cmp(node a, node b){
    return a.b < b.b;
}

void cdq(int l, int r){
    if(l >= r) return ;
    int mid = (l + r) >> 1;
    cdq(l, mid), cdq(mid + 1, r);
    sort(a + l, a + mid + 1, cmp);
    sort(a + mid + 1, a + r + 1, cmp);
    int i = l, j = mid + 1;
    while(i <= mid && j <= r){
        if(a[j].b >= a[i].b){
            if(a[i].op != 2) add(a[i].c, a[i].b + a[i].c); i++;
        }else{
            ans[a[j].a] = min(a[j].b + a[j].c - query(a[j].c), ans[a[j].a]), j++;
        }
    }
    while(i <= mid){if(a[i].op != 2) add(a[i].c, a[i].b + a[i].c); i++;}
    while(j <= r) ans[a[j].a] = min(a[j].b + a[j].c - query(a[j].c), ans[a[j].a]), j++;
    for(int i = l; i <= mid; i++) if(a[i].op != 2) zrr(a[i].c);
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> a[i].b >> a[i].c, a[i].b++, a[i].c++;
    }
    fill(tr + 1, tr + M, -1e9);
    for(int op, x, y, i = 1; i <= m; i++){
        cin >> op >> x >> y; x++, y++;
        a[n + i] = {i, x, y, op}, ans[i] = 1e9;
    } n += m;
    sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; }); cdq(1, n);
    for(int i = 1; i <= n; i++) a[i].b = 1e6 - a[i].b + 2;
    sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; }); cdq(1, n);
    for(int i = 1; i <= n; i++) a[i].c = 1e6 - a[i].c + 2;
    sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; }); cdq(1, n);
    for(int i = 1; i <= n; i++) a[i].b = 1e6 - a[i].b + 2;
    sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; }); cdq(1, n);
    sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; });
    for(int i = 1; i <= n; i++){
        if(a[i].op == 2) cout << ans[a[i].a] << '\n';
    }
    return 0;
}

P4093

设 dp_i 为以 i 结尾的子序列的最大长度,那么 dp_i = max_{j < i, b_j < a_i, a_j < c_i} dp_j + 1,其中 b_i,c_i 分别为 i 能变的最大值和最小值,再加上拓扑序又形成了三维偏序,按同样的操作DP即可,注意要先递归 (l, mid),做完操作后再递归即可。

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10, M = 1e5 + 10;

int n, k, m, tr[N], id[N], a[N], b[N], c[N], dp[N], op[N];

void add(int x, int y){ x++;
    for(; x < M; x += (x & -x)) tr[x] = max(tr[x], y);
}

int query(int x){ int res = -1e9; x++;
    for(; x; x -= (x & -x)) res = max(tr[x], res);
    return res;
}

void zrr(int x){ x++;
    for(; x < M; x += (x & -x)) tr[x] = -1e9;
}

void cdq(int l, int r){
    if(l >= r){
        if(l == r) dp[id[l]] = max(dp[id[l]], 1);
        return ;
    }
    int mid = (l + r) >> 1; cdq(l, mid);
    sort(id + l, id + mid + 1, [](const int &x, const int &y){ return b[x] < b[y];});
    sort(id + mid + 1, id + r + 1, [](const int &x, const int &y){ return a[x] < a[y];});
    int i = l, j = mid + 1;
    while(i <= mid && j <= r){
        if(a[id[j]] >= b[id[i]]){
            add(a[id[i]], dp[id[i]]), i++;
        }else{
            dp[id[j]] = max(dp[id[j]], query(c[id[j]]) + 1), j++;
        }
    }
    while(i <= mid) add(a[id[i]], dp[id[i]]), i++;
    while(j <= r) dp[id[j]] = max(dp[id[j]], query(c[id[j]]) + 1), j++;
    for(int i = l; i <= mid; i++) zrr(a[id[i]]);
    sort(id + mid + 1, id + r + 1);
    cdq(mid + 1, r);
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> a[i], b[i] = c[i] = a[i], id[i] = i;
    }
    fill(tr + 1, tr + M, -1e9);
    for(int i = 1, x, y; i <= m; i++){
        cin >> x >> y;
        b[x] = max(b[x], y), c[x] = min(c[x], y);
    } 
    cdq(1, n);
    cout << *max_element(dp + 1, dp + 1 + n);
    return 0;
}