• BZOJ1176: [Balkan2007]Mokia CDQ分治


    最近很不对啊=w= 写程序全是bug啊

    ans数组开小了竟然一直不知道,小数据没问题大数据拍不过,交上去RE

    蛋疼半天

    这个主要把每次询问拆成3个询问。

    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #define dout printf
    using namespace std;
    
    const int Maxw=2000000+10,Maxm=160000+10,Maxq=10000+10;
    typedef long long ll;
    int S,W,tot,C[Maxw],ans[Maxm+Maxq*4];
    
    void Add(int x,const int&d){
        for(;0<x&&x<=W;x+=x&-x)C[x]+=d;
    }
    int Query(int x){
        int ret=0;
        for(;0<x&&x<=W;x-=x&-x)ret+=C[x];
        return ret;
    }
    
    const int Hmod=1e5+7;
    struct node{
        bool tp;//0 for modify and 1 for query
        int x,y,ans,id;
        node(int x,int y,int tp,int ans,int id):tp(tp),x(x),y(y),ans(ans),id(id){}
        node(){ans=0;}
    }da[Maxq*4+Maxm],q[Maxq*4+Maxm];
    
    struct prob{
        int s1,s2,s3,s4,t;
        void init(int x1,int y1,int x2,int y2){
            ++tot,da[s1=tot]=node(x2,y2,1,0,tot);
            if(x1==1||y1==1)s2=0;
            else ++tot,da[s2=tot]=node(x1-1,y1-1,1,0,tot);
            if(x1==1)s3=0;
            else ++tot,da[s3=tot]=node(x1-1,y2,1,0,tot);
            if(y1==1)s4=0;
            else ++tot,da[s4=tot]=node(x2,y1-1,1,0,tot);
            t=(x2-x1+1)*(y2-y1+1);
        }
        int calc(){
            return ans[s1]+ans[s2]-(ans[s3]+ans[s4])+t*S;
        }
    }pr[Maxq];int totpr=0;
    
    inline bool cmp(const node&a,const node&b){
        if(a.x!=b.x)return a.x<b.x;
        return a.y<b.y;
    }
    void init(){
        scanf("%d%d",&S,&W);
        for(int x1,y1,x2,y2,d,opt;~scanf("%d",&opt)&&opt!=3;){
            if(opt==1)scanf("%d%d%d",&x1,&y1,&d),++tot,da[tot]=node(x1,y1,0,d,tot);
            else scanf("%d%d%d%d",&x1,&y1,&x2,&y2),pr[++totpr].init(x1,y1,x2,y2);
        }
    }
    
    void CDQ(int l,int r){
        if(l==r)return;
        int mid=(l+r)>>1;
        CDQ(l,mid);
        CDQ(mid+1,r);
        int i=l;
        for(int j=mid+1;j<=r;j++){
            for(;i<=mid&&da[i].x<=da[j].x;i++)if(!da[i].tp)Add(da[i].y,da[i].ans);
            if(da[j].tp)da[j].ans+=Query(da[j].y);
        }
        for(i--;i>=l;i--)if(!da[i].tp)Add(da[i].y,-da[i].ans);
        merge(da+l,da+mid+1,da+mid+1,da+r+1,q,cmp);
        memcpy(da+l,q,sizeof(da[0])*(r-l+1));
    }
    int main(){
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
        
        init();
        CDQ(1,tot);
        for(int i=1;i<=tot;i++)ans[da[i].id]=da[i].ans;
        for(int i=1;i<=totpr;i++)printf("%d
    ",pr[i].calc());
        
        return 0;
    }
    View Code
    原文出处http://www.cnblogs.com/showson/
  • 相关阅读:
    mac的端口被占用
    php中的运算符、控制结构
    文档模式影响浏览器的渲染
    ubuntu 命令 tips 来自于 ubuntu中文论坛
    用好 Emacs 中的 register
    [转载] Rsync命令参数详解
    使用 python 遍历目录下的文件
    灵活的左移位( << )操作
    使用 iperf 测试两台机器间的最大带宽
    Emacs server 新启动方式 (仅在emacs daemon未启动时才启动daemon)
  • 原文地址:https://www.cnblogs.com/showson/p/4278805.html
Copyright © 2020-2023  润新知