A15058 | Narrow Components
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a matrix $a$ , consisting of $3$ rows and $n$ columns. Each cell of the matrix is either free or taken.
A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions hold:
- $x$ and $y$ share a side;
- there exists a free cell $z$ such that $z$ is reachable from $x$ and $y$ is reachable from $z$ .
A connected component is a set of free cells of the matrix such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.
You are asked $q$ queries about the matrix. Each query is the following:
- $l$ $r$ — count the number of connected components of the matrix, consisting of columns from $l$ to $r$ of the matrix $a$ , inclusive.
Print the answers to all queries.
A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions hold:
- $x$ and $y$ share a side;
- there exists a free cell $z$ such that $z$ is reachable from $x$ and $y$ is reachable from $z$ .
A connected component is a set of free cells of the matrix such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.
You are asked $q$ queries about the matrix. Each query is the following:
- $l$ $r$ — count the number of connected components of the matrix, consisting of columns from $l$ to $r$ of the matrix $a$ , inclusive.
Print the answers to all queries.
输入格式
The first line contains an integer $n$ ( $1 \le n \le 5 \cdot 10^5$ ) — the number of columns of matrix $a$ .
The $i$ -th of the next three lines contains a description of the $i$ -th row of the matrix $a$ — a string, consisting of $n$ characters. Each character is either $1$ (denoting a free cell) or $0$ (denoting a taken cell).
The next line contains an integer $q$ ( $1 \le q \le 3 \cdot 10^5$ ) — the number of queries.
The $j$ -th of the next $q$ lines contains two integers $l_j$ and $r_j$ ( $1 \le l_j \le r_j \le n$ ) — the description of the $j$ -th query.
The $i$ -th of the next three lines contains a description of the $i$ -th row of the matrix $a$ — a string, consisting of $n$ characters. Each character is either $1$ (denoting a free cell) or $0$ (denoting a taken cell).
The next line contains an integer $q$ ( $1 \le q \le 3 \cdot 10^5$ ) — the number of queries.
The $j$ -th of the next $q$ lines contains two integers $l_j$ and $r_j$ ( $1 \le l_j \le r_j \le n$ ) — the description of the $j$ -th query.
输出格式
Print $q$ integers — the $j$ -th value should be equal to the number of the connected components of the matrix, consisting of columns from $l_j$ to $r_j$ of the matrix $a$ , inclusive.
输入输出样例
输入 #1
12 100101011101 110110010110 010001011101 8 1 12 1 1 1 2 9 9 8 11 9 12 11 12 4 6
输出 #1
7 1 1 2 1 3 3 3
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted