CF707E.Garlands

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Like all children, Alesha loves New Year celebration. During the celebration he and his whole family dress up the fir-tree. Like all children, Alesha likes to play with garlands — chains consisting of a lightbulbs.

Alesha uses a grid field sized n × m for playing. The rows of the field are numbered from 1 to n from the top to the bottom and columns are numbered from 1 to m from the left to the right.

Alesha has k garlands which he places at the field. He does so in the way such that each lightbulb of each garland lies in the center of some cell in the field, and each cell contains at most one lightbulb. Of course lightbulbs, which are neighbours in some garland, appears in cells neighbouring by a side.

The example of garland placing.

Each garland is turned off or turned on at any moment. If some garland is turned on then each of its lightbulbs is turned on, the same applies for garland turned off. Each lightbulb in the whole garland set is unique, and thus, being turned on, brings Alesha some pleasure, described by an integer value. Turned off lightbulbs don't bring Alesha any pleasure.

Alesha can turn garlands on and off and wants to know the sum of pleasure value which the lightbulbs, placed in the centers of the cells in some rectangular part of the field, bring him. Initially all the garlands are turned on.

Alesha is still very little and can't add big numbers. He extremely asks you to help him.

和所有孩子一样,阿列沙喜欢庆祝新年。在庆祝活动中,他和全家人一起装饰圣诞树。和所有孩子一样,阿列沙喜欢玩彩灯链——由灯泡组成的链状结构。

阿列沙使用一个大小为 n×mn \times m 的网格区域来玩耍。网格的行从上到下编号为 11 到 nn,列从左到右编号为 11 到 mm。

阿列沙有 kk 条彩灯链,并将它们放置在该网格上。放置方式满足:每条彩灯链上的每个灯泡都恰好位于某个格子的中心,且每个格子中至多包含一个灯泡。当然,同一条彩灯链中相邻的灯泡,其所处的格子也必须是上下左右四连通的相邻格子。

彩灯链放置的一个示例。

任意时刻,每条彩灯链要么全部开启,要么全部关闭。若某条彩灯链处于开启状态,则其所有灯泡均开启;若处于关闭状态,则其所有灯泡均关闭。整个彩灯链集合中的每个灯泡都是唯一的,因此当某个灯泡开启时,会为阿列沙带来一个由整数表示的愉悦值;而关闭的灯泡则不带来任何愉悦值。

阿列沙可以随时开启或关闭任意彩灯链,并希望快速获知:位于某个矩形区域(即网格中某连续子矩形)内所有格子中心处的灯泡,当前所贡献的愉悦值总和是多少。初始状态下,所有彩灯链均为开启状态。

阿列沙年纪尚小,还无法计算很大的数字。他非常恳请你的帮助!

输入格式

The first line of the input contains three integers n, m and k (1 ≤ n, m, k ≤ 2000) — the number of field rows, the number of field columns and the number of garlands placed at the field respectively.

Next lines contains garlands set description in the following format:

The first line of a single garland description contains a single integer len (1 ≤ len ≤ 2000) — the number of lightbulbs in the garland.

Each of the next len lines contains three integers i, j and w (1 ≤ i ≤ n, 1 ≤ j ≤ m, 1 ≤ w ≤ 109) — the coordinates of the cell containing a lightbullb and pleasure value Alesha gets from it if it is turned on. The lightbulbs are given in the order they are forming a chain in the garland. It is guaranteed that neighbouring lightbulbs are placed in the cells neighbouring by a side.

The next line contains single integer q (1 ≤ q ≤ 106) — the number of events in Alesha's game. The next q lines describes events in chronological order. The i-th of them describes the i-th event in the one of the following formats:

  • SWITCH i — Alesha turns off i-th garland if it is turned on, or turns it on if it is turned off. It is guaranteed that 1 ≤ i ≤ k.
  • ASK _x_1 _y_1 _x_2 _y_2 — Alesha wants to know the sum of pleasure values the lightbulbs, placed in a rectangular part of the field. Top-left cell of a part has coordinates (_x_1, _y_1) and right-bottom cell has coordinates (_x_2, _y_2). It is guaranteed that 1 ≤ _x_1 ≤ _x_2 ≤ n and 1 ≤ _y_1 ≤ _y_2 ≤ m. There is no more than 2000 events of this type in the input.

All the numbers in the input are integers.

Please note that the input is quite large, so be careful while using some input ways. In particular, it's not recommended to use cin in codes on C++ and class Scanner in codes on Java.

输入的第一行包含三个整数 nn、mm 和 kk(1 ≤ n, m, k ≤ 20001 ≤ n, m, k ≤ 2000),分别表示场地的行数、列数以及放置在场上的彩灯串数量。

接下来若干行描述彩灯串的设置,格式如下:

每条彩灯串的描述以一行开始,该行包含一个整数 lenlen(1 ≤ len ≤ 20001 ≤ len ≤ 2000)—— 表示该彩灯串中灯泡的数量。

随后的 lenlen 行,每行包含三个整数 ii、jj 和 ww(1 ≤ i ≤ n1 ≤ i ≤ n, 1 ≤ j ≤ m1 ≤ j ≤ m, 1 ≤ w ≤ 1091 ≤ w ≤ 10^9)—— 表示灯泡所在格子的坐标及若该灯泡被点亮时 Alesha 获得的愉悦值。灯泡按其在彩灯串中构成链状结构的顺序给出。保证相邻灯泡所在的格子在网格中是上下左右相邻的(即共享一条边)。

接下来一行包含一个整数 qq(1 ≤ q ≤ 1061 ≤ q ≤ 10^6)—— 表示 Alesha 游戏中发生的事件总数。随后 qq 行按时间顺序描述这些事件。其中第 ii 行描述第 ii 个事件,格式为以下二者之一:

  • SWITCH i —— 若第 ii 条彩灯串当前处于开启状态,则 Alesha 将其关闭;若处于关闭状态,则将其开启。保证 1 ≤ i ≤ k1 ≤ i ≤ k。
  • ASK x1 y1 x2 y2 —— Alesha 想知道位于场地某矩形区域内的所有灯泡的愉悦值之和。该矩形区域的左上角格子坐标为 (x1, y1)(x_1, y_1),右下角格子坐标为 (x2, y2)(x_2, y_2)。保证 1 ≤ x1 ≤ x2 ≤ n1 ≤ x_1 ≤ x_2 ≤ n 且 1 ≤ y1 ≤ y2 ≤ m1 ≤ y_1 ≤ y_2 ≤ m。整个输入中此类事件至多有 2000 个。

输入中所有数字均为整数。

请注意:输入规模较大,请谨慎选择输入方式。特别地,在 C++ 代码中不建议使用 cin,在 Java 代码中不建议使用 Scanner 类。

输出格式

For each ASK operation print the sum Alesha wants to know in a separate line. Print the answers in chronological order.

对于每个 ASK 操作,请在单独一行中输出阿列沙想要知道的和。请按时间顺序输出答案。

输入输出样例

  • 输入#1

    4 4 3
    5
    1 1 2
    1 2 3
    2 2 1
    2 1 4
    3 1 7
    4
    1 3 1
    2 3 3
    2 4 3
    1 4 1
    7
    4 1 1
    4 2 9
    3 2 8
    3 3 3
    4 3 4
    4 4 1
    3 4 1
    2
    ASK 2 2 3 3
    ASK 1 1 4 4

    输出#1

    15
    52
  • 输入#2

    4 4 1
    8
    4 1 1
    3 1 2
    2 1 1
    1 1 7
    1 2 5
    2 2 4
    2 3 1
    1 3 1
    3
    ASK 1 1 3 2
    SWITCH 1
    ASK 1 1 3 2

    输出#2

    19
    0

说明/提示

This image illustrates the first sample case.

该图展示了第一个样例。

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

首页