ACdream群OJ 1157 cdq分治

【题目链接】

http://acdream.info/problem?pid=1157

【思路】

因为线段是二维的,我们不妨考虑一个二维平面。这样的话,增加线段 [ l, r ] ,视为点(l,r)+1,删除则看做-1。那么查询[ l , r ]可以看做求点(l,r)到点(∞,∞)这个矩形的所有点的和。貌似看起来是一个二维树状数组或线段树。可惜,即使端点离散化,范围还是太大。再加上空间的极限优化,即不修改的点不建立,需要的时候再建立,空间也是很大。这个时候,cdq分治就起作用了,其实,这题就是一个很裸的cdq分治的运用。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF=1000111222;
const double INFF=1e200;
const double eps=1e-8;
const int mod=1000000007;
const int NN=100010;
const int MM=100010;
/* ****************** */

int line[NN][2];
int ans[NN];
struct node
{
    int l,r,w,id;
    node(int a=0,int b=0,int c=0,int d=0):l(a),r(b),w(c),id(d){}
}a[NN],b[NN];
int c[NN*2];
int tf[NN*2];

bool cmp(node xx,node yy)
{
    if(xx.l==yy.l)
        return xx.w < yy.w;
    return xx.l<yy.l;
}

int lowbit(int x)
{
    return x&(-x);
}
void modify(int x,int ad)
{
    int i;
    for(i=x;i>0;i-=lowbit(i))
        c[i]+=ad;
}
int get_sum(int x)
{
    int sum = 0;
    int n = 200000;
    for(;x<=n;x+=lowbit(x))
    {
        sum += c[x];
    }
    return sum;
}

void cdq_fz(int l,int r)
{
    if(l==r)return;

    int i, t, mid=(l+r)>>1;
    int tol = 0;

    cdq_fz(l,mid);

    for(i=l;i<=r;i++)
    {
        b[tol++] = a[i];
    }

   // printf("go to solve (%d %d)\n",l,r);

    sort(b,b+tol,cmp);
    for(i=0;i<tol;i++)
    {
        if(b[i].id<=mid)
        {
            if(b[i].w!=10)
            {
                modify(b[i].r,b[i].w);
             //   printf("dian==%d add==%d\n",)
            }
        }
        else
        {
            if(b[i].w==10)
            {
                t = get_sum(b[i].r);
                ans[b[i].id] += t;

              //  printf("[%d,%d] %d -> %d \n",l,r,b[i].id,t);
            }
        }
    }
    for(i=0;i<tol;i++)
    {
        if(b[i].id<=mid)
        {
            if(b[i].w!=10)
            {
                modify(b[i].r,-b[i].w);
            }
        }
    }


    cdq_fz(mid+1,r);
}

int main()
{
    int n,m,i,tol,t;
    char op[10];
    while(scanf("%d",&n)!=EOF)
    {
        tol = 0;
        for(i=1;i<=n;i++)
        {
            ans[i] = 0;

            scanf("%s",op);
            if(op[0]=='D')
            {
                tol++;
                scanf("%d%d",&line[tol][0],&line[tol][1]);
                a[i] = node(line[tol][0],line[tol][1],1,i);
            }
            else if(op[0]=='C')
            {
                scanf("%d",&t);
                a[i] = node(line[t][0],line[t][1],-1,i);
            }
            else
            {
                scanf("%d%d",&a[i].l,&a[i].r);
                a[i].w = 10;
                a[i].id = i;
            }

            tf[i] = a[i].l;
            tf[i+n] = a[i].r;
        }

        sort(tf+1,tf+1+n+n);

        m = unique(tf+1,tf+1+n+n)-tf-1;

//        cout<<"m=="<<m<<endl;
//        for(i=1;i<=m;i++)
//            printf("%d%c",tf[i],i==m?'\n':' ');

        for(i=1;i<=n;i++)
        {
            a[i].l = lower_bound(tf+1,tf+1+m,a[i].l)-tf;
            a[i].r = lower_bound(tf+1,tf+1+m,a[i].r)-tf;
        }

//        for(i=1;i<=n;i++)
//        {
//            cout<<"l r "<<a[i].l<<" "<<a[i].r<<endl;
//        }

        memset(c,0,sizeof(c));
        cdq_fz(1,n);

        for(i=1;i<=n;i++)
        {
            if(a[i].w==10)
                printf("%d\n",ans[i]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值