CF1368H2.Breadboard Capacity (hard version)

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

This is a harder version of the problem H with modification queries.

Lester and Delbert work at an electronics company. They are currently working on a microchip component serving to connect two independent parts of a large supercomputer.

The component is built on top of a breadboard — a grid-like base for a microchip. The breadboard has nn rows and mm columns, and each row-column intersection contains a node. Also, on each side of the breadboard there are ports that can be attached to adjacent nodes. Left and right side have nn ports each, and top and bottom side have mm ports each. Each of the ports is connected on the outside to one of the parts bridged by the breadboard, and is colored red or blue respectively.

Ports can be connected by wires going inside the breadboard. However, there are a few rules to follow:

  • Each wire should connect a red port with a blue port, and each port should be connected to at most one wire.
  • Each part of the wire should be horizontal or vertical, and turns are only possible at one of the nodes.
  • To avoid interference, wires can not have common parts of non-zero length (but may have common nodes). Also, a wire can not cover the same segment of non-zero length twice.

The capacity of the breadboard is the largest number of red-blue wire connections that can be made subject to the rules above. For example, the breadboard above has capacity 77, and one way to make seven connections is pictured below.

Up to this point statements of both versions are identical. Differences follow below.

As is common, specifications of the project change a lot during development, so coloring of the ports is not yet fixed. There are qq modifications to process, each of them has the form of "colors of all ports in a contiguous range along one of the sides are switched (red become blue, and blue become red)". All modifications are persistent, that is, the previous modifications are not undone before the next one is made.

To estimate how bad the changes are, Lester and Delbert need to find the breadboard capacity after each change. Help them do this efficiently.

这是问题 H 的一个更难版本,增加了修改查询。

莱斯特和德尔伯特在一家电子公司工作。他们目前正在为一台大型超级计算机研发一种微芯片组件,用于连接该超级计算机中两个相互独立的部分。

该组件构建在一个面包板(breadboard)之上——一种网格状的微芯片基板。该面包板具有 nn 行和 mm 列,每个行列交点处均有一个节点。此外,在面包板的每一边上都设有端口,可连接至相邻的节点。左右两侧各有 nn 个端口,上下两侧各有 mm 个端口。每个端口在外部连接至被面包板所桥接的两个部分之一,并分别染成红色或蓝色。

端口可通过布设于面包板内部的导线相互连接。但需遵循以下规则:

  • 每根导线必须连接一个红色端口与一个蓝色端口,且每个端口最多只能连接一根导线;
  • 导线的每一部分必须为水平或垂直方向,且仅可在某个节点处发生转向;
  • 为避免干扰,任意两根导线不得拥有长度大于零的公共路径段(但允许共享节点);此外,一根导线自身也不得重复覆盖长度大于零的同一路径段。

面包板的容量定义为:在满足上述所有约束条件下,所能实现的最大红-蓝端口连接对数。例如,上图所示的面包板容量为 77,下图展示了一种实现七对连接的方式。

至此,两个版本的问题描述完全相同。差异如下所述。

如同常见情形,项目规格在开发过程中频繁变更,因此端口的颜色尚未最终确定。接下来需处理 qq 次修改操作,每次操作的形式为:“将某一边上一段连续范围内的所有端口颜色翻转(红色变为蓝色,蓝色变为红色)”。所有修改均为持久化操作,即前一次修改的效果不会在下一次修改之前被撤销。

为评估这些变更的影响程度,莱斯特和德尔伯特需要在每次修改后计算面包板的当前容量。请帮助他们高效地完成这一任务。

输入格式

The first line contains three integers n,m,qn, m, q (1≤n,m≤1051 \leq n, m \leq 10^5, 0≤q≤1050 \leq q \leq 10^5) — the number of rows and columns of the breadboard, and the number of modifications respectively.

The next four lines describe initial coloring of the ports. Each character in these lines is either R or B, depending on the coloring of the respective port. The first two of these lines contain nn characters each, and describe ports on the left and right sides respectively from top to bottom. The last two lines contain mm characters each, and describe ports on the top and bottom sides respectively from left to right.

The next qq lines describe modifications. Each of these lines contains a character ss, followed by two integers ll and rr. If ss is L or R, the modification is concerned with ports on the left/right side respectively, ll and rr satisfy 1≤l≤r≤n1 \leq l \leq r \leq n, and ports in rows between ll and rr (inclusive) on the side switch colors. Similarly, if ss is U or D, then 1≤l≤r≤m1 \leq l \leq r \leq m, and ports in columns between ll and rr (inclusive) on the top/bottom side respectively switch colors.

第一行包含三个整数 n,m,qn, m, q(1≤n,m≤1051 \leq n, m \leq 10^5,0≤q≤1050 \leq q \leq 10^5),分别表示面包板的行数、列数以及修改操作的次数。

接下来四行描述端口的初始着色。每行中的每个字符为 R 或 B,表示对应端口的颜色。前两行每行包含 nn 个字符,分别从上到下描述左侧和右侧的端口;后两行每行包含 mm 个字符,分别从左到右描述顶部和底部的端口。

接下来 qq 行描述修改操作。每行包含一个字符 ss 和两个整数 ll、rr。若 ss 为 L 或 R,则该修改针对左侧或右侧的端口,且满足 1≤l≤r≤n1 \leq l \leq r \leq n,此时该侧第 ll 行至第 rr 行(含端点)的所有端口颜色翻转;若 ss 为 U 或 D,则满足 1≤l≤r≤m1 \leq l \leq r \leq m,此时顶部或底部第 ll 列至第 rr 列(含端点)的所有端口颜色翻转。

输出格式

Print q+1q + 1 integers, one per line — the breadboard capacity after 0,…,q0, \ldots, q modifications have been made to the initial coloring.

输出 q+1q + 1 个整数,每行一个——分别表示在对初始着色进行 0,…,q0, \ldots, q 次修改后的面包板容量。

输入输出样例

  • 输入#1

    4 5 4
    BBRR
    RBBR
    BBBBB
    RRRRR
    L 2 3
    R 3 4
    U 1 5
    D 1 5

    输出#1

    7
    7
    9
    4
    9

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

首页