• bzoj1176: [Balkan2007]Mokia


    http://www.lydsy.com/JudgeOnline/problem.php?id=1176

    CDQ分治

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    
    #define lowbit(x) x&-x 
    
    using namespace std;
    
    #define N 160001
    #define M 10001
    
    typedef long long LL;
    
    int w;
    LL c[2000001];
    
    struct node
    {
        int id;
        int x,y,bl,mul;
        bool ty;
    }e[N+M*4],tmp[N+M*4];
    
    LL ans[M];
    
    void read(int &x)
    {
        x=0; int f=1; char c=getchar();
        while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
        while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
        x*=f;
    }
    
    bool cmp(node p,node q)
    {
        if(p.x!=q.x) return p.x<q.x;
        return p.ty<q.ty;
    }
    
    void change(int x,int k)
    {
        while(x<=w)
        {
            c[x]+=k;
            x+=lowbit(x);
        }
    }
    
    LL query(int x)
    {
        LL sum=0;
        while(x)
        {
            sum+=c[x];
            x-=lowbit(x);
        }
        return sum;
    }
    
    void solve(int l,int r)
    {
        if(l==r) return;
        int mid=l+r>>1;
        int sum=0;
        for(int i=l;i<=r;++i)
        {
            if(!e[i].ty && e[i].id<=mid) change(e[i].y,e[i].mul);
            else if(e[i].ty && e[i].id>mid) ans[e[i].bl]+=e[i].mul*query(e[i].y); 
        }
        for(int i=l;i<=r;++i)
        {
            if(!e[i].ty && e[i].id<=mid) change(e[i].y,-e[i].mul);
        }
        int L=l,R=mid+1;
        for(int i=l;i<=r;++i)
        {
            if(e[i].id<=mid) tmp[L++]=e[i];
            else tmp[R++]=e[i];
        }
        for(int i=l;i<=r;++i) e[i]=tmp[i];
        solve(l,mid);
        solve(mid+1,r);
    }
    
    int main()
    {
        int s;
        read(s); read(w);
        int ty,lx,ly,rx,ry,k;
        int tot=0,cnt=0;
        while(scanf("%d",&ty))
        {
            if(ty==1)
            {
                e[++tot].id=tot;
                read(e[tot].x); 
                read(e[tot].y);
                read(e[tot].mul);
            }
            else if(ty==2)
            {
                read(lx); read(ly); read(rx); read(ry);
                cnt++;
                e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=1; e[tot].ty=true; e[tot].x=rx; e[tot].y=ry;
                e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=1; e[tot].ty=true; e[tot].x=lx-1; e[tot].y=ly-1;
                e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=-1; e[tot].ty=true; e[tot].x=lx-1; e[tot].y=ry;
                e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=-1; e[tot].ty=true; e[tot].x=rx; e[tot].y=ly-1;
                
            }
            else break;
        }
        sort(e+1,e+tot+1,cmp);
        solve(1,tot);
        for(int i=1;i<=cnt;++i) cout<<ans[i]<<'
    ';
    }
  • 相关阅读:
    【转载】关于sql server 代理(已禁用代理xp)
    Silverlight中将WriteableBitmap互转byte数组
    思迅Pay PC ,WIN7 ,KB3042058
    微软新Edge浏览器 WIN7 无法登录
    List<SelectListItem> 转为 SelectList
    图片jpg,png转为BASE64编码
    “Newtonsoft.Json”已拥有为“Microsoft.CSharp”定义的依赖项。
    mac os 10.15.1 懒人 .CDR
    微信刷脸SDK获取sub_openid
    win7 安装 visual studio 2019 时闪退(VS2019)
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8057698.html
Copyright © 2020-2023  润新知