CF1661E.Narrow Components

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a matrix aa, consisting of 33 rows and nn columns. Each cell of the matrix is either free or taken.

A free cell yy is reachable from a free cell xx if at least one of these conditions hold:

  • xx and yy share a side;
  • there exists a free cell zz such that zz is reachable from xx and yy is reachable from zz.

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 qq queries about the matrix. Each query is the following:

  • ll rr — count the number of connected components of the matrix, consisting of columns from ll to rr of the matrix aa, inclusive.

Print the answers to all queries.

给你一个由 33 行 nn 列组成的矩阵 aa。矩阵中的每个单元格要么是空闲的,要么是被占据的。

若满足以下任一条件,则称空闲单元格 yy 可从空闲单元格 xx 到达:

  • xx 与 yy 共享一条边;
  • 存在某个空闲单元格 zz,使得 zz 可从 xx 到达,且 yy 可从 zz 到达。

连通分量是指矩阵中一组空闲单元格,其中任意两个单元格均可互相到达,但向该集合中添加任何其他空闲单元格都会破坏这一性质。

你需要处理关于该矩阵的 qq 个查询。每个查询的形式如下:

  • ll rr —— 计算矩阵 aa 中第 ll 列至第 rr 列(含端点)所构成子矩阵的连通分量数量。

请输出所有查询的答案。

输入格式

The first line contains an integer nn (1≤n≤5⋅1051 \le n \le 5 \cdot 10^5) — the number of columns of matrix aa.

The ii-th of the next three lines contains a description of the ii-th row of the matrix aa — a string, consisting of nn characters. Each character is either 11 (denoting a free cell) or 00 (denoting a taken cell).

The next line contains an integer qq (1≤q≤3⋅1051 \le q \le 3 \cdot 10^5) — the number of queries.

The jj-th of the next qq lines contains two integers ljl_j and rjr_j (1≤lj≤rj≤n1 \le l_j \le r_j \le n) — the description of the jj-th query.

第一行包含一个整数 nn(1≤n≤5⋅1051 \le n \le 5 \cdot 10^5)——矩阵 aa 的列数。

接下来三行中的第 ii 行描述矩阵 aa 的第 ii 行——一个由 nn 个字符组成的字符串。每个字符为 11(表示空闲单元格)或 00(表示已被占据的单元格)。

下一行包含一个整数 qq(1≤q≤3⋅1051 \le q \le 3 \cdot 10^5)——查询的数量。

接下来 qq 行中的第 jj 行包含两个整数 ljl_j 和 rjr_j(1≤lj≤rj≤n1 \le l_j \le r_j \le n)——第 jj 个查询的描述。

输出格式

Print qq integers — the jj-th value should be equal to the number of the connected components of the matrix, consisting of columns from ljl_j to rjr_j of the matrix aa, inclusive.

输出 qq 个整数——第 jj 个值应等于由矩阵 aa 的第 ljl_j 列到第 rjr_j 列(含端点)所组成的子矩阵的连通块数量。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页