【HDU4052】【ZOJ3540】Adding New Machine 线段树+扫描线

题意:W*H的格子上放着一些旧机器,现在要往图上放1*M的新机器,问放机器的方式有多少种
做法:把每个旧机器的范围扩展一下,向左&向下扩展m-1个格子,这样就转化为了求矩阵面积并的问题。同时要把矩形位置坐标转化为线的坐标。
注意考虑long long…

#include<bits/stdc++.h>
#define mid int m = (l+r)>>1;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
typedef long long ll;
int M, N, tot;
ll W, H;
const int maxn = 100001;
struct node{
    int xl,yl,xr,yr;
}rec[maxn];
struct line{
    int l,r,pos,add;
    line(){}
    line(int l,int r,int pos,int add):l(l),r(r),pos(pos),add(add){}
    bool operator < (const line &l)const{
        return pos < l.pos;
    }
};
vector<line>li;
vector<int>hs;
int flag[maxn<<2],sum[maxn<<2];
void pushup(int l, int r, int rt){
    if(flag[rt]) sum[rt] = hs[r] - hs[l-1];
    else if(l == r) sum[rt] = 0;
    else sum[rt] = sum[ls] + sum[rs];
}
void bulid(int l, int r, int rt){
    memset(flag,0,sizeof(flag));
    memset(sum,0,sizeof(sum));
}
void update(int L, int R, int l, int r, int rt, int x){
    if(L <= l && r <= R){
        flag[rt] += x;
        pushup(l,r,rt);
        return;
    }
    mid;
    if(L<=m) update(L, R, lson, x);
    if(m < R) update(L, R, rson,x);
    pushup(l, r, rt);
}
ll solve(){
    if(N == 0){
        return H * (W - M + 1);
    }
    ll ret =0 ;
    sort(li.begin(),li.end());
    sort(hs.begin(),hs.end());
    hs.erase(unique(hs.begin(),hs.end()),hs.end());
    int sz = li.size() - 1;
    int hh = hs.size();
    bulid(1,hh,1);
    for(int i = 0;i<sz;i++){
        int l = lower_bound(hs.begin(),hs.end(),li[i].l) - hs.begin();
        int r = lower_bound(hs.begin(),hs.end(),li[i].r) - hs.begin();
        update(l+1,r,1,hh,1,li[i].add);
        ret +=(ll)sum[1]* (li[i+1].pos - li[i].pos) ;        
    }
    return W * H - ret;
}
int main(){
    while(~scanf("%lld%lld%d%d", &W, &H, &N, &M)){
        ll ans = 0;
        for(int i=1;i<=N;i++){
            scanf("%d%d%d%d", &rec[i].xl, &rec[i].yl, &rec[i].xr, &rec[i].yr);
            rec[i].xl--;
            rec[i].yl--;
        }
        li.clear();
        hs.clear();
        for(int i = 1;i<=N;i++){
            li.push_back(line(max(0,rec[i].yl-M+1),rec[i].yr,rec[i].xl , 1));
            li.push_back(line(max(0,rec[i].yl-M+1),rec[i].yr,rec[i].xr , -1));
            hs.push_back(li[li.size()-1].l);
            hs.push_back(li[li.size()-1].r);
        }
        li.push_back(line(max(0,(int)H-M+1),H,0,1));
        li.push_back(line(max(0,(int)H-M+1),H,W,-1));
        hs.push_back(li[li.size()-1].l);
        hs.push_back(li[li.size()-1].r);
        ans += solve();
        swap(H,W);
        li.clear();
        hs.clear();
        for(int i = 1;i<=N;i++){
            li.push_back(line(max(0,rec[i].xl-M+1),rec[i].xr,rec[i].yl , 1));
            li.push_back(line(max(0,rec[i].xl-M+1),rec[i].xr,rec[i].yr , -1));
            hs.push_back(li[li.size()-1].l);
            hs.push_back(li[li.size()-1].r);
        }
        li.push_back(line(max(0,(int)H-M+1),H,0,1));
        li.push_back(line(max(0,(int)H-M+1),H,W,-1));
        hs.push_back(li[li.size()-1].l);
        hs.push_back(li[li.size()-1].r);
        ans += solve();
        if(M == 1)
            ans /= 2;
        printf("%lld\n",ans);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值