COGS577

CDQ分治

const int maxn = 2e5+100;
const int maxm = 5e5+100;
int n,w;
int tree[maxn];
void Add(int p,int x){
    while(p <= w){
        tree[p] += x;
        p += lowbit(p);
    }
}
int Sum(int p){
    int sum = 0;
    while(p > 0){
        sum += tree[p];
        p -= lowbit(p);
    }
    return sum;
}

struct nd{
    int x1,y1,x2,y2,z,id,op;
};
bool operator <(const nd &a,const nd &b){
    return (a.x1 < b.x1)||(a.x1==b.x1&&a.op < b.op);
}
nd A[maxm],B[maxm];

int ans[maxn];
void CDQ(int L,int R){
    if(L == R) return ;
    int M = (L+R)>>1;
    CDQ(L,M);CDQ(M+1,R);
    int cnt = 0;
    for(int i = L;i <= M; ++i)
        if(A[i].op == 1) 
            B[cnt++] = A[i];
    for(int i = M+1;i <= R; ++i)
        if(A[i].op == 2)
            {
                B[cnt] = A[i];B[cnt].x1--,cnt++;
                B[cnt] = A[i];B[cnt].x1 = B[cnt].x2,B[cnt].op =3,cnt++;
            }

    sort(B,B+cnt);
    for(int i = 0;i < cnt; ++i){
        if(B[i].op == 1)        Add(B[i].y1,B[i].z);
        else if(B[i].op == 2)   ans[B[i].id] -= Sum(B[i].y2)-Sum(B[i].y1-1);
        else                    ans[B[i].id] += Sum(B[i].y2)-Sum(B[i].y1-1);
    }

}
int main(void){

    scanf("%d%d",&w,&n);

    for(int i = 1;i <= n; ++i){
        scanf("%d",&A[i].op);
        if(A[i].op == 1) scanf("%d%d%d",&A[i].x1,&A[i].y1,&A[i].z);
        else scanf("%d%d%d%d",&A[i].x1,&A[i].y1,&A[i].x2,&A[i].y2);
        A[i].id = i;
    }
    CDQ(1,n);

    for(int i = 1;i <= n; ++i){
        if(A[i].op == 2)
            printf("%d\n",ans[A[i].id]);
    }
}


全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 17:28
25届每天都在焦虑找工作的事情0offer情绪一直很低落硬撑着面了一个岗位岗位有应酬的成分面试的时候hr给我出各种场景题问的问题比较犀利&nbsp;有点压力面的感觉感觉有点回答不上来本来就压抑的情绪瞬间爆发了呢一瞬间特别想哭觉得自己特别没用没绷住掉眼泪了事后想想觉得自己挺有病的&nbsp;真的破大防了
喜欢唱跳rap小刺猬...:我觉得没关系吧,之前有一次面试leader给我压力面,我顶住了压力,结果入职的时候发现组里氛围很差,果断跑路。其实从面试就能大概看出组的情况,面试体验好的组倒是不一定好,但是面试体验不好的组。。。就很难说
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务