[cdq分治习题练习]

博客探讨了CDQ分治算法在解决二维前缀和、逆序对计数等不同问题中的应用。通过分析bzoj1935、bzoj1176、bzoj3295和洛谷P4169等题目,详细阐述了如何利用CDQ分治结合树状数组有效地处理这些问题。对于逆序对问题,强调了处理左右元素对贡献的不同策略,并展示了如何通过排序和CDQ分治来计算贡献。
摘要由CSDN通过智能技术生成

bzoj1935
题意:给出n棵树的位置(xi,yi),有m次询问,每次询问一个以(Xi,Yi)为左下角,(Li,Ri)为右上角的矩形内树的个数

题解:显然的三维偏序问题,一维是询问的时间T,一维是横坐标,一维是纵坐标。把初始的n棵树的位置当作插入,把询问当成4个二维前缀和相加减,由于时间是按照输入的顺序,所以第一维不需要排序,直接cdq分治处理第二维,树状数组维护第三维即可

/**************************************************************
    Problem: 1935
    User: Altria_
    Language: C++
    Result: Accepted
    Time:7168 ms
    Memory:278892 kb
****************************************************************/
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
//#define debug(x) cout << #x << " is " << x << endl;
 
const int maxn = 5e5+1000;
const int maxnw = 1e7+1000;
 
struct Op{
   
    ll x;
    ll y;
    int type;
    int id;
}op[maxn*5],op2[maxn*5];
 
ll tot,ans[maxn],tim,sz[maxnw],fl[maxnw],maxx;
 
void update(int x){
   
    for(int i=x;i<=maxx;i+=(i&(-i))){
   
        if(fl[i]!=tim){
   
            fl[i]=tim;
            sz[i]=0;
        }
        sz[i]++;
    }
}
 
ll query(int x){
   
    ll ac=0;
    for(int i=x;i>0;i-=(i&(-i))){
   
        if(fl[i]==tim){
   
            ac+=sz[i];
        }
    }
    return ac;
}
 
void cdq(int l,int r){
   
    if(l>=r)return;
 
    int mid=(l+r)>>1;
    cdq(l,mid);
    cdq(mid+1,r);
 
    int L=l;
    int R=mid+1;
    int t1=l;
    tim++;
    while(L<=mid&&R<=r){
   
        if(op[L].x<=op[R].x){
   
            if(op[L].type==0){
   
                update(op[L].y);
            }
            op2[t1++]=op[L++];
        }
        else{
   
            if(op[R].type)ans[op[R].id]+=op[R].type*query(op[R].y);
            op2[t1++]=op[R++];
        }
    }
    while(L<=mid){
   
        op2[t1++]=op[L++];
    }
    while(R<=r){
   
        if(op[R].type)ans[op[R].id]+=op[R].type*query(op[R].y);
        op2[t1++]=op[R++];
    }
    for(int i=l;i<=r;i++){
   
        op[i]=op2[i];
    }
}
 
int main() {
   
    int n,m;
    scanf("%d%d",&n,&m);
 
    for(int i=1;i<=n;i++){
   
        ++tot;
        scanf("%lld%lld",&op[tot].x,&op[tot].y);
        op[tot].x=op[tot].x+4;
        op[tot].y=op[tot].y+4;
        maxx=max(op[tot].y,maxx);
        op[tot].type=0;
        op[tot].id=0;
    }
    for(int i=1;i<=m;i++){
   
        ll a,b,c,d;
        ans[i]=0;
        scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
        a+=4;
        b+=4;
        c+=4;
        d+=4;
        maxx=max(max(b,d),maxx);
        op[++tot].id=i;
        op[tot].type=-1;
        op[tot].x=a-1;
        op[tot].y=d;
 
        op[++tot].id=i;
        op[tot].type=-1;
        op[tot].x=c;
        op[tot].y=b-1;
 
        op[++tot].id=i;
        op[tot].type=1;
        op[tot].x=a-1;
        op[tot].y=b-1;
 
        op[++tot].id=i;
        op[tot].type=1;
        op[tot].x=c;
        op[tot].y=d;
    }
 
    cdq(1,tot);
 
    for(int i=1;i<=m;i++){
   
        printf("%lld\n",ans[i]);
    }
    return 0;
}

bzoj1176
题意:有一个w*w的矩阵,每个格子内数字的初始值为s,有m次操作,操作有两种:1、格子(xi,yi)数字增加 2、求某个子矩阵格子数字之和

题解:和上题一样

/**************************************************************
    Problem: 1176
    User: Altria_
    Language: C++
    Result: Accepted
    Time:6184 ms
    Memory:46692 kb
****************************************************************/
 
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " is " << x << endl;
 
const int maxn = 2e5+1000;
const int maxnw = 2e6+1000;
 
struct Op{
   
    ll x;
    ll y;
    int type;
    ll val;
    int id;
}op[maxn],op2[maxn];
 
ll ans[maxn
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值