309314: CF1661E. Narrow Components

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Narrow Components

题意翻译

给定一个 $3\times n$ 的网格图,每个格子要么是黑色格子要么是白色格子,两个相邻的白色格子联通。 多组询问,每组询问给定 $l,r$,求 $l$ 列至 $r$ 列之间有多少个白色格子构成的联通块。 输入时,图中元素为 $1$ 的格子是白色格子,元素为 $0$ 的格子是黑色格子 $$1\le n \le 5\times 10^5$$ $$1\le m \le 3\times 10^5$$ Traslated by @[BFSDFS123](https://www.luogu.com.cn/user/358739)

题目描述

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.

输入输出格式

输入格式


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.

输出格式


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

Input

题意翻译

给定一个 $3\times n$ 的网格图,每个格子要么是黑色格子要么是白色格子,两个相邻的白色格子联通。 多组询问,每组询问给定 $l,r$,求 $l$ 列至 $r$ 列之间有多少个白色格子构成的联通块。 输入时,图中元素为 $1$ 的格子是白色格子,元素为 $0$ 的格子是黑色格子 $$1\le n \le 5\times 10^5$$ $$1\le m \le 3\times 10^5$$ Traslated by @[BFSDFS123](https://www.luogu.com.cn/user/358739)

加入题单

上一题 下一题 算法标签: