算法总结——分治

· · 算法·理论

CDQ 分治(归并)

它可以解决

  1. 多维偏序问题
  2. 动态规划优化
  3. 数据结构替代,比如三维偏序可以代替树套树
  4. 时间轴分治,离线操作,把时间轴看作维度

过程

对于一个区间 [l,r],它会做

  1. 递归求解 [l,\text{mid}],[\text{mid}+1,r] 内部贡献,得到端点都在两边同一个区间的答案
  2. 计算跨过区间的贡献

时间复杂度和归并排序一致,为 O(n\log n)。

T1 【模板】三维偏序 / 陌上花开

思路

首先按照 a_i 排序,去除第一维 a_i 的限制。

考虑用 \text{CDQ} 解决后两个维度。类似归并排序,用 [l,\text{mid}] 和 [\text{mid+1},r] 的局部单调性,去除第二维的限制,再用树状数组处理第三维。

树状数组单次 O(\log),答案为 O(n\log^2n)。

代码

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define lowbit(x) (x&-x)
const int N = 1e5+5,M = 2e5+5;
struct node {int a,b,c,cnt,ans;} a[N],tmp[N];
int n,before_n,m,sum[M],ans[N],total[N];

void change(int x,int add_val)
{
    while(x <= m)
    {
        sum[x] += add_val;
        x += lowbit(x);
    }
    return ;
}

int get_sum(int l,int r)
{
    int the_ans = 0;
    while(r)
    {
        the_ans += sum[r];
        r -= lowbit(r);
    }
    l--;
    while(l)
    {
        the_ans -= sum[l];
        l -= lowbit(l);
    }
    return the_ans;
}

void fz_solve(int l,int r)
{
    if(l == r) return;
    int mid = (l+r)/2;
    fz_solve(l,mid);
    fz_solve(mid+1,r);
    int ptl = l,ptr = mid+1,pt = l-1;
    while(ptl <= mid && ptr <= r)
    {
        if(a[ptl].b <= a[ptr].b)
        {
            change(a[ptl].c,a[ptl].cnt);
            tmp[++pt] = a[ptl++];
        }else
        {
            a[ptr].ans += get_sum(1,a[ptr].c);
            tmp[++pt] = a[ptr++];
        }
    }
    while(ptl <= mid)
    {
        change(a[ptl].c,a[ptl].cnt);
        tmp[++pt] = a[ptl++];
    }
    while(ptr <= r)
    {
        a[ptr].ans += get_sum(1,a[ptr].c);
        tmp[++pt] = a[ptr++];
    }
    for(int i = l;i <= mid;i++) change(a[i].c,-a[i].cnt);
    for(int i = l;i <= r;i++) a[i] = tmp[i];
    return;
}

void input(void)
{
    cin >> n >> m;
    before_n = n;
    for(int i = 1;i <= n;i++) cin >> a[i].a >> a[i].b >> a[i].c;
    for(int i = 1;i <= n;i++) a[i].cnt = 1;
    for(int i = 1;i <= n;i++) a[i].ans = 0;
    sort(a+1,a+n+1,[](node x,node y)
    {
        if(x.a != y.a) return x.a < y.a;
        if(x.b != y.b) return x.b < y.b;
        return x.c < y.c;
    });
    int _n = 1;
    for(int i = 2;i <= n;i++)
    {
        if(a[_n].a == a[i].a && a[_n].b == a[i].b && a[_n].c == a[i].c) a[_n].cnt++;
        else a[++_n] = a[i];
    }
    n = _n;
    return ;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    input();
    fz_solve(1,n);
    for(int i = 1;i <= n;i++) total[a[i].ans+a[i].cnt-1] += a[i].cnt;
    for(int i = 0;i < before_n;i++) cout << total[i] << '\n';
    return 0;
}

T2 [CQOI2011] 动态逆序对

思路

考虑时光倒流,删除变为加入。

令元素 a_i 的加入时间为 t_i,那么它对 [t_i,m] 产生的贡献为满足 i<j,a_i>a_j,t_i \ge t_j 或者 i>j,a_i<a_j,t_i \ge t_j 的 j 的数量。

两遍 \text{CDQ} 分治即可。时间复杂度 O(n\log^2n)

代码

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define lowbit(x) (x&-x)
const int N = 1e5+5,Q = 50005;
struct node {int a,b,c,ans;} a[N],tmp[N];
int n,T,pos[N],sum[Q],ans[Q];

void change(int x,int add_val)
{
    x++;
    while(x <= T+1)
    {
        sum[x] += add_val;
        x += lowbit(x);
    }
    return ;
}

int get_sum(int l,int r)
{
    l++;
    r++;
    int the_ans = 0;
    while(r)
    {
        the_ans += sum[r];
        r -= lowbit(r);
    }
    l--;
    while(l)
    {
        the_ans -= sum[l];
        l -= lowbit(l);
    }
    return the_ans;
}

void fz_solve(int l,int r)
{
    if(l == r) return;
    int mid = (l+r)/2;
    fz_solve(l,mid);
    fz_solve(mid+1,r);
    int ptl = l,ptr = mid+1,pt = l-1;
    while(ptl <= mid && ptr <= r)
    {
        if(a[ptl].b <= a[ptr].b)
        {
            change(a[ptl].c,1);
            tmp[++pt] = a[ptl++];
        }else
        {
            a[ptr].ans += get_sum(0,a[ptr].c);//曾经唐错:值域写成 [1,a[ptr].c]
            tmp[++pt] = a[ptr++];
        }
    }
    while(ptl <= mid)
    {
        change(a[ptl].c,1);
        tmp[++pt] = a[ptl++];
    }
    while(ptr <= r)
    {
        a[ptr].ans += get_sum(0,a[ptr].c);
        tmp[++pt] = a[ptr++];
    }
    for(int i = l;i <= mid;i++) change(a[i].c,-1);
    for(int i = l;i <= r;i++) a[i] = tmp[i];
    return;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> T;
    for(int i = 1;i <= n;i++) cin >> a[i].b;
    for(int i = 1;i <= n;i++) a[i].a = i;
    for(int i = 1;i <= n;i++) pos[a[i].b] = i;
    for(int pid = 1;pid <= T;pid++)
    {
        int x;
        cin >> x;
        a[pos[x]].c = T-pid+1;
    }
    for(int i = 1;i <= n;i++) a[i].ans = 0;
    reverse(a+1,a+n+1);
    fz_solve(1,n);
    for(int i = 1;i <= n;i++) ans[a[i].c] += a[i].ans;
    sort(a+1,a+n+1,[](node x,node y) {return x.a < y.a;});
    for(int i = 1;i <= n;i++) a[i].b = n-a[i].b+1;
    for(int i = 1;i <= n;i++) a[i].ans = 0;
    fz_solve(1,n);
    for(int i = 1;i <= n;i++) ans[a[i].c] += a[i].ans;
    ans[0] /= 2;
    for(int i = 1;i <= T;i++) ans[i] += ans[i-1];
    reverse(ans+1,ans+T+1);
    for(int i = 1;i <= T;i++) cout << ans[i] << '\n';
    return 0;
}

T3 [Violet] 天使玩偶/SJY摆棋子

思路

把时间看作一个维度,把贡献分为四个象限,限制分别为 $x_i \le x_j,y_i\le y_j,t_i\le t_j$、$x_i \ge x_j,y_i\le y_j,t_i\le t_j$、$x_i \ge x_j,y_i\ge y_j,t_i\le t_j$、$x_i \le x_j,y_i\le y_j,t_i\ge t_j$,计数改为最大值即可。 时间复杂度 $O(n\log^2n)$。 ### 代码 ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define lowbit(x) (x&-x) const int N = 6e5+5,Q = 3e5+5; struct node {int x,y,t,ans,opt;} a[N],tmp[N]; int n,T,maxx_sum[Q],ans[Q]; void change(int x,int nval) { x++; while(x <= T+1) { maxx_sum[x] = max(maxx_sum[x],nval); x += lowbit(x); } return ; } void clear_bit(int x) { x++; while(x <= T+1) { maxx_sum[x] = -1e18; x += lowbit(x); } return ; } int get_max(int x) { x++; int the_ans = -1e18; while(x) { the_ans = max(the_ans,maxx_sum[x]); x -= lowbit(x); } return the_ans; } void fz_solve(int l,int r) { if(l == r) return; int mid = (l+r)/2; fz_solve(l,mid); fz_solve(mid+1,r); int ptl = l,ptr = mid+1,pt = l-1; while(ptl <= mid && ptr <= r) { if(a[ptl].y <= a[ptr].y) { if(a[ptl].opt == 1) change(a[ptl].t,a[ptl].x+a[ptl].y); tmp[++pt] = a[ptl++]; }else { if(a[ptr].opt == 2) a[ptr].ans = min(a[ptr].ans,a[ptr].x+a[ptr].y-get_max(a[ptr].t)); tmp[++pt] = a[ptr++]; } } while(ptl <= mid) { if(a[ptl].opt == 1) change(a[ptl].t,a[ptl].x+a[ptl].y); tmp[++pt] = a[ptl++]; } while(ptr <= r) { if(a[ptr].opt == 2) a[ptr].ans = min(a[ptr].ans,a[ptr].x+a[ptr].y-get_max(a[ptr].t)); tmp[++pt] = a[ptr++]; } for(int i = l;i <= mid;i++) if(a[i].opt == 1) clear_bit(a[i].t);//曾经唐错:和下面一行写反,i写成ptl for(int i = l;i <= r;i++) a[i] = tmp[i]; return; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> T; for(int i = 1;i <= n;i++) cin >> a[i].x >> a[i].y; for(int i = 1;i <= n;i++) a[i].t = 0; for(int i = 1;i <= n;i++) a[i].ans = 1e18; for(int i = 1;i <= n;i++) a[i].opt = 1; for(int i = 1;i <= T+1;i++) maxx_sum[i] = -1e18; for(int pid = 1;pid <= T;pid++) { int op,x,y; cin >> op >> x >> y; a[++n] = {x,y,pid,(int)1e18,op}; } sort(a+1,a+n+1,[](node x,node y) { if(x.x != y.x) return x.x < y.x; if(x.y != y.y) return x.y < y.y; return x.t < y.t; }); fz_solve(1,n); for(int i = 1;i <= n;i++) a[i].x *= -1; sort(a+1,a+n+1,[](node x,node y) { if(x.x != y.x) return x.x < y.x; if(x.y != y.y) return x.y < y.y; return x.t < y.t; }); fz_solve(1,n); for(int i = 1;i <= n;i++) a[i].y *= -1; sort(a+1,a+n+1,[](node x,node y) { if(x.x != y.x) return x.x < y.x; if(x.y != y.y) return x.y < y.y; return x.t < y.t; }); fz_solve(1,n); for(int i = 1;i <= n;i++) a[i].x *= -1; sort(a+1,a+n+1,[](node x,node y) { if(x.x != y.x) return x.x < y.x; if(x.y != y.y) return x.y < y.y; return x.t < y.t; }); fz_solve(1,n); int pt = 0; sort(a+1,a+n+1,[](node x,node y) {return x.t < y.t;}); for(int i = n-T+1;i <= n;i++) if(a[i].opt == 2) ans[++pt] = a[i].ans; for(int i = 1;i <= pt;i++) cout << ans[i] << '\n'; return 0; } ```