当前位置:网站首页>【集训DAY9】Maze【线段树】
【集训DAY9】Maze【线段树】
2022-07-21 07:15:00 【VL——MOESR】
思路:
我们发现n<=5,所以我们可以考虑用线段树把每一列都存起来。
如果要从i走到i+1列,只要在线段树中把两列的值都调出来,然后做Floyd就行了
我们考虑计算答案。
考虑在同一列走的情况,那就是向上或向下走。枚举走的步数,如果发现走不到那就是-1。
如果不是同一列,那就先查询线段树,将所有列都合并起来,在走最后一列就行了。
c o d e code code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 2e5 + 10;
int n, m, q;
bool bz;
int a[6][MAXN];
struct SgT {
int p[6][6];
}tree[MAXN * 4], ans;
SgT bing(SgT A, SgT B) {
SgT C;
memset(C.p, 1, sizeof(C.p));
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
for(int k = 1; k <= n; k ++)
C.p[i][j] = min(C.p[i][j], A.p[i][k] + B.p[k][j]);
return C;
}
void update(int k, int x) {
memset(tree[k].p, 1, sizeof(tree[k].p));
for(int i = 1; i <= n; i ++) {
int up = 0, down = 0;
if(!a[i][x]) continue;
for(int j = 0; j < n; j ++) {
if(i + j > n || a[i + j][x] == 0) up = 1;
if(i - j < 1 || a[i - j][x] == 0) down = 1;
if(up && down) break;
if(!up) tree[k].p[i + j][i] = j + 1;
if(!down) tree[k].p[i - j][i] = j + 1;
}
}
}
void build(int k, int l, int r) {
if(l == r) {
update(k, l);
return ;
}
int mid = l + r >> 1;
build(k * 2, l, mid);
build(k * 2 + 1, mid + 1, r);
tree[k] = bing(tree[k * 2], tree[k * 2 + 1]);
}
void Update(int k, int l, int r, int x) {
if(l == r) {
update(k, l);
return ;
}
int mid = l + r >> 1;
if(x <= mid) Update(k * 2, l, mid, x);
else Update(k * 2 + 1, mid + 1, r, x);
tree[k] = bing(tree[k * 2], tree[k * 2 + 1]);
}
void query_(int k, int l, int r, int x, int y) {
if(l == x && y == r) {
if(!bz) ans = tree[k], bz = 1;
else ans = bing(ans, tree[k]);
return ;
}
int mid = l + r >> 1;
if(y <= mid) query_(k * 2, l, mid, x, y);
else if(x > mid) query_(k * 2 + 1, mid + 1, r, x, y);
else query_(k * 2, l, mid, x, mid), query_(k * 2 + 1, mid + 1, r, mid + 1, y);
}
int main() {
freopen("maze.in", "r", stdin);
freopen("maze.out", "w", stdout);
scanf("%d%d%d", &n, &m, &q);
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++) scanf("%d", &a[i][j]);
build(1, 1, m);
while(q --) {
int ch, l, r, x, y;
scanf("%d%d%d", &ch, &l, &r);
if(ch == 1) {
a[l][r] = 1 - a[l][r];
Update(1, 1, m, r);
}
else {
scanf("%d%d", &x, &y);
if(y < r) {
printf("-1\n"); continue; }
if(y == r) {
bool flag = 1;
if(l > x) swap(l, x);
for(int i = l; i <= x; i ++) if(a[i][r] == 0) {
flag = 0; break; }
if(flag) printf("%d\n", x - l);
else printf("-1\n");
}
else {
bz = 0;
query_(1, 1, m, r, y);
int Ans = ans.p[l][x] - 1;
if(Ans == 1e9 || Ans > n * m) printf("-1\n");
else printf("%d\n", Ans);
}
}
}
return 0;
}
边栏推荐
- (pytorch advanced road VI) implementation of swing transformer
- oracle 19c datagruad复制备库RMAN-05535 ORA-01275
- Mysql05 (view)
- LeetCode 1911. 最大子序列交替和
- 查看IAR工程所用版本号
- PYQT5打包出错,缺少文件,例ImportError: OpenCV loader: missing configuration file: [‘config.py‘]. Check
- Lecture 1 Overview
- 创建并发索引时失败,遗留了一个失效的索引 ,如何查找
- Framework - WindowManager (window management service) practice of WMS
- Ala-PNA丙氨酸改性PNA肽核酸|Ac-Ala-PNA的合成路线
猜你喜欢
token和session的区别,这个经典面试题值得一个更深入的回答
PO模式在Selenium自动化测试中怎么应用
NetworkX的基本用法
vector的常见接口介绍
Installation du gestionnaire de loisirs
逍遙管理器安裝
Niu Ke brushes question 01 - Kiki de duplication of integers and sorting (C language)
uniapp 引入腾讯地图
罗丹明B标记肽核酸PNA|罗丹明B-PNA|生物素修饰肽核酸pna|生物素修饰pna肽核酸|规格信息
使用CompletableFuture实现异步回调
随机推荐
mysql docker安装 主从部署
ModuleNotFoundError: No module named ‘pysat.solvers‘(已解决)
【SemiDrive源码分析】【模块BringUp】37 - LCM 驱动 Bringup 流程
Problems and principle analysis of audio AGC
(pytorch advanced road VII) attention based seq2seq
A 股指数历史数据 API 数据接口
Mysql06 (sequence)
Software resources Encyclopedia
铜牛机房项目的优势和劣势
Qt:qstring and qstringlist
Vs2022 shortcut key settings
Statistics for each month of the year
Introduction to common interfaces of vector
Cloud foundry 4: application lifecycle
百度工程师眼中的云原生可观测性追踪技术
LeetCode 1928. 规定时间内到达终点的最小花费
[completion course] development process of IOT / embedded / MCU graduation design project
The sandbox teamed up with agoria to build agoriaverse
Word2vec simple summary
45:第四章:开发文件服务:6:第三方云存储解决方案【阿里云OSS】;(购买OSS服务;开通服务;创建一个Bucket;)