cdq分治总结(题目合集)

1、The Preliminary Contest for ICPC Asia Nanjing 2019 A. The beautiful values of the palace

题意:给出一个n*n(n为奇数)的蛇形矩阵,将其中的m个点的值改为各位数之后,给出p组询问,查询子矩阵的和(必须是m个点集中的点,其他的点可看成值为0)。

分析:数组求和或者矩阵求和,一般树状数组和线段树就可以做,这里用cdq分治写的。和子矩阵修改,子矩阵求和那题几乎是一样的。cdq分治总结可以参考https://www.cnblogs.com/Parsnip/p/10816330.html

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5,M=800020;
int n,m,cnt,tot,ans[M];
struct BIT
{
    int p[N];
    inline int lowbit(int x){return x&(-x);}
    inline void insert(int pos,int val)
    {
        for (;pos<=n;pos+=lowbit(pos))
            p[pos] += val;
    }
    inline int query(int pos)
    {
        int res = 0;
        for (;pos;pos-=lowbit(pos))
            res += p[pos];
        return res;
    }
    inline void cl() {
        memset(p,0,sizeof(p));
    }
}tree;
struct order
{
    int flag,x,y,val;
    bool operator < (order p)
    {
        if ( x ^ p.x ) return x < p.x;
        if ( y ^ p.y ) return y < p.y;
        return flag < p.flag;
    }
}a[M],cur[M];

int get(int x,int y)
{
    ll ans = 0,sum = 0;
    ll t = max(abs(x-(n+1)/2),abs(y-(n+1)/2));
    sum = 1LL*n*n - (2*t+1)*(2*t+1);
    if(x>=y) sum += abs((n+1)/2+t-x)+abs((n+1)/2+t-y)+1;
    else sum += 2*(2*t+1)-1+abs((n+1)/2-t-x)+abs((n+1)/2-t-y);
    while(sum)
        ans+=sum%10,sum/=10;
    return ans;
}


inline void cdq(int l,int r)
{
    if ( l == r ) return;
    int mid = l+r >> 1 , p = l , q = mid+1 , t = l-1;
    cdq(l,mid); cdq(mid+1,r);
    while ( p <= mid && q <= r )
    {
        if ( a[p] < a[q] )
        {
            if ( a[p].flag == 1 ) tree.insert(a[p].y,a[p].val);
            cur[++t] = a[p++];
        }
        else
        {
            if ( a[q].flag == 2 ) ans[ a[q].val ] += tree.query(a[q].y);
            if ( a[q].flag == 3 ) ans[ a[q].val ] -= tree.query(a[q].y);
            cur[++t] = a[q++];
        }
    }
    while ( q <= r )
    {
        if ( a[q].flag == 2 ) ans[ a[q].val ] += tree.query(a[q].y);
        if ( a[q].flag == 3 ) ans[ a[q].val ] -= tree.query(a[q].y);
        cur[++t] = a[q++];
    }
    for (int i=l;i<p;i++)
        if ( a[i].flag == 1 )
            tree.insert(a[i].y,-a[i].val);
    while ( p <= mid ) cur[++t] = a[p++];
    for (int i=l;i<=r;i++) a[i] = cur[i];
}

int T,p;
inline void input() {
    scanf("%d",&T);
    while(T--) {
        cnt = 0;
        tot = 0;
        tree.cl();
        memset(ans,0,sizeof(ans));
        scanf("%d%d%d",&n,&m,&p);
        for(int i=1;i<=m;i++) {
            int x,y;
            scanf("%d%d",&x,&y);
            int val = get(x,y);
            a[++cnt] = (order){1,x,y,val};
        }
        for(int i=1;i<=p;i++) {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            a[++cnt] = (order){2,x2,y2,++tot};
            a[++cnt] = (order){2,x1-1,y1-1,tot};
            a[++cnt] = (order){3,x1-1,y2,tot};
            a[++cnt] = (order){3,x2,y1-1,tot};
        }
        cdq(1,cnt);
        for (int i=1;i<=tot;i++)
            printf("%d\n",ans[i]);
    }
}

int main(void) {
    input();
    return 0;
}

 

### 关于CDQ分治的基础练习题目 #### 三维偏序问入门示例 考虑一个经典的三维偏序问,给定n个三元组(a_i,b_i,c_i),求对于每一个i(1≤ i ≤ n),有多少个j满足a_j ≤ a_i, b_j ≤ b_i 和 c_j ≤ c_i (j ≠ i)[^1]。 这个问可以通过CDQ分治来高效解决。核心思路在于将原问分解成更小规模的子问,并通过处理这些子问之间的关系得到最终解。具体来说,在每次划分过程中,先按照某一维度排序(比如a),再利用已排序的结果递归地解决问的一半对另一半的影响。 ```cpp #include <algorithm> using namespace std; struct node { int x,y,z,id; }p[200005],tmp[200005]; int bit[200005],ans[200005]; bool cmp_x(const node& a,const node& b){ return a.x<b.x||(a.x==b.x&&a.y<b.y)||(a.x==b.x&&a.y==b.y&&a.z<b.z); } void add(int pos,int val){ while(pos<=200000){ bit[pos]+=val; pos+=pos&(-pos); } } int query(int pos){ int ret=0; while(pos>0){ ret+=bit[pos]; pos-=pos&(-pos); } return ret; } void cdq(int l,int r){ if(l>=r)return ; int mid=(l+r)>>1,i=l,j=mid+1,k=l; cdq(l,mid);cdq(mid+1,r); sort(p+l,p+mid+1,cmp_y); sort(p+mid+1,p+r+1,cmp_y); for(;k<=r;k++){ tmp[k]=p[j]; j++; }else{ ans[p[i].id]+=query(p[i].z); add(p[i].z,1); tmp[k++]=p[i++]; } for(i=l;i<j;i++)if(tmp[i].id<=mid)add(tmp[i].z,-1); copy(tmp+l,tmp+r+1,p+l); } ``` 上述代码片段展示了如何应用CDQ分治方法去计算三维偏序中的逆序对数量。这里`cdq()`函数实现了主要逻辑,它不仅解决了当前区间内的部分问,还负责统计左区间的元素对右区间产生的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值