HDU - 6992 Lawn of the Dead 杭电多校

原题链接
思路:我们以可达性思考,每个格子的可达性来自于左边的格子和上面的格子,我们以第一行为例,为什么第一行的第一个地雷右边的所有点都无法到达?因为这颗地雷割断了剩下所有点来自左边的方案,并且上方没有方案,因此剩下所有的格子可达性都为0。
同理,剩下的所有格子也都可以这么思考。在某一行碰到地雷之后,我们需要看一下剩下的格子,在他们上面是否有可达的格子,如果上面有格子可达,那么这个格子也可达,并且因为这个格子可达,因此在他右边的所有格子(直到该行的下一个地雷的位置)就因为他多了一个来自左边的可达性,因此该位置直到下个地雷的格子全部都变成可达。即使那些格子上面的格子都是不可达的也不影响,因为上面的格子给予他们的是来自上方的可达性,而左边那个可达的格子已经给了他们来自左边的可达性了,只要上、左任何一个格子可达,那么这个格子就是可达的。因此,我们在遇见一个地雷后,需要求出的就是该地雷到下一个地雷的位置上,有无来自上方的可达格子。如果有,那么从这个格子到下一个地雷-1的位置就会全部可达。
如图,只要两个地雷中间存在一个来自上方的可达点,那么直到下一个地雷的位置都是可达的。
在这里插入图片描述
我们需要查询的就是i + 1 ~ x - 1的位置上,上一行有无可达点,如可达,那么pos ~ x - 1全部赋值为1
如何同时枚举两颗地雷的位置?我们可以定义一个l为上一个地雷,初始l=0,地雷也代表着不可达位置,因此定义0也是可以的。同时再枚举下一个地雷就可以。
我使用线段树实现。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <set>
#include <queue>

using namespace std;
#define endl '\n'
#define lson node << 1
#define rson node << 1 | 1
const int maxn = 1e5 + 5;
const int inf = 1e9;
struct node{
    int val,laz;
}t[2][maxn << 2];
int n,m,k;
vector<int> g[maxn];
void pushup(int c,int node){
    t[c][node].val = t[c][lson].val + t[c][rson].val;
}
void spread(int c,int node,int l,int r){
    if (t[c][node].laz == -1)   return;
    int mid = l + r >> 1;
    int &k = t[c][node].laz;
    t[c][lson].val = (mid - l + 1) * k;
    t[c][rson].val = (r - mid) * k;
    t[c][lson].laz = t[c][rson].laz = k;
    k = -1;
}
void build(int c,int l,int r,int node){
    t[c][node].laz = -1;
    if (l == r){
        t[c][node].val = 0;
        return;
    }
    int mid = l + r >> 1;
    build(c,l,mid,lson);
    build(c,mid + 1,r,rson);
    pushup(c,node);
}
void update(int c,int l,int r,int x,int y,int node,int k){
    if (x <= l && r <= y){
        t[c][node].val = (r - l + 1) * k;
        t[c][node].laz = k;
        return;
    }
    int mid = l + r >> 1;
    spread(c,node,l,r);
    if (x <= mid) update(c,l,mid,x,y,lson,k);
    if (y > mid) update(c,mid + 1,r,x,y,rson,k);
    pushup(c,node);
}
int query(int c,int l,int r,int x,int y,int node){
    if (!t[c][node].val)    return inf;
    if (l == r) return l;
    int mid = l + r >> 1;
    spread(c,node,l,r);
    if (x <= l && r <= y){
        if (t[c][lson].val) return query(c,l,mid,x,y,lson);
        else return query(c,mid + 1,r,x,y,rson);
    }
    int ans = inf;
    if (x <= mid) ans = min(ans, query(c,l,mid,x,y,lson));
    if (y > mid)  ans = min(ans, query(c,mid + 1,r,x,y,rson));
    return ans;
}
void solve(){
    cin >> n >> m >> k;
    for (int i = 1; i <= n; ++i) {
        g[i].clear();
    }
    long long ans = 0;
    for (int i = 1; i <= k; ++i) {
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
    }
    for (int i = 0; i <= 1; ++i) {
        build(i,1,m,1);
    }
    int now = 0;
    update(now,1,m,1,1,1,1);
    now ^= 1;
    for (int i = 1; i <= n; ++i) {
        sort(g[i].begin(),g[i].end());
        int l = 0;
        for(auto x : g[i]){
            if (l + 1 <= x - 1){
                int pos = query(now ^ 1,1,m,l + 1,x - 1,1);
                if (pos != inf) update(now,1,m,pos,x - 1,1,1);
            }
            l = x;
        }
        if (l + 1 <= m){
            int pos = query(now ^ 1,1,m,l + 1,m,1);
            if (pos != inf) update(now,1,m,pos,m,1,1);
        }
        ans += t[now][1].val;
        update(now ^ 1,1,m,1,m,1,0);
        now ^= 1;
    }
    cout << ans << endl;

}
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值