PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000174. Những dải đèn trang trí (Garlands)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Những dải đèn trang trí (Garlands)

Garlands

Source: Codeforces

Version: Phước Hưng OJ Extended

Problem Statement

Alesha places KK garlands on an N×MN\times M grid. Each garland is a chain of light bulbs; consecutive bulbs of a garland occupy orthogonally adjacent cells. Every grid cell contains at most one bulb across all garlands. A bulb at (x,y)(x,y) contributes its weight ww when its garland is on, and contributes 0 otherwise. All garlands are initially switched on.

Process QQ events in chronological order:

  • SWITCH i: toggle the on/off state of the entire ii-th garland.
  • ASK x1 y1 x2 y2: find the total weight of currently lit bulbs within the inclusive rectangle [x1,x2]×[y1,y2][x1,x2]\times[y1,y2].

Bulbs never move or change their weights. The number of ASK events is guaranteed to be at most 2000.

Input

The first line contains N,M,KN,M,K. For each garland, a line gives its length len, followed by len lines x y w listing bulbs in their chain order. An integer QQ follows, then QQ lines of events SWITCH i or ASK x1 y1 x2 y2.

Output

For every ASK event, print the total weight of lit bulbs in the queried rectangle at that point in time.

Subtask

  • Subtask 1 (20 points): 1≤N≤101\le N\le 10; 1≤M≤101\le M\le 10; 1≤K≤101\le K\le 10; 1≤Q≤601\le Q\le 60; at most 30 ASK events; 1≤leni≤20001\le len_i\le2000, 1≤w≤1091\le w\le10^9; no cell holds multiple bulbs, consecutive bulbs in a garland are side-adjacent; all coordinates are valid, with x1≤x2x_1\le x_2 and y1≤y2y_1\le y_2.
  • Subtask 2 (30 points): 1≤N≤1001\le N\le 100; 1≤M≤1001\le M\le 100; 1≤K≤1001\le K\le 100; 1≤Q≤15001\le Q\le 1500; at most 500 ASK events; 1≤leni≤20001\le len_i\le2000, 1≤w≤1091\le w\le10^9; no cell holds multiple bulbs, consecutive bulbs in a garland are side-adjacent; all coordinates are valid, with x1≤x2x_1\le x_2 and y1≤y2y_1\le y_2.
  • Subtask 3 (50 points): 1≤N≤20001\le N\le 2000; 1≤M≤20001\le M\le 2000; 1≤K≤20001\le K\le 2000; 1≤Q≤10000001\le Q\le 1000000; at most 2000 ASK events; 1≤leni≤20001\le len_i\le2000, 1≤w≤1091\le w\le10^9; no cell holds multiple bulbs, consecutive bulbs in a garland are side-adjacent; all coordinates are valid, with x1≤x2x_1\le x_2 and y1≤y2y_1\le y_2.

Examples

Example 1

Input

3 3 2
2
1 1 5
1 2 3
1
3 3 10
7
ASK 1 1 3 3
SWITCH 1
ASK 1 1 3 3
ASK 1 1 2 2
SWITCH 2
SWITCH 1
ASK 1 1 3 3

Output

18
10
0
8

Explanation

Initially both garlands are on: the first contributes 5+3=85+3=8 and the second contributes 10, giving 18. Toggling the first off leaves 10. The next query covers only rows and columns 1–2 and excludes the second garland’s bulb at (3,3)(3,3), giving 0. Turning the second garland off and the first back on makes the final full-grid sum 8.

Example 2

Input

2 3 1
3
1 1 1
1 2 2
1 3 3
5
ASK 1 2 2 3
SWITCH 1
ASK 1 1 2 3
SWITCH 1
ASK 1 1 1 2

Output

5
0
3

Explanation

The only garland starts on. Its bulbs in columns 2 and 3 sum to 2+3=52+3=5. When it is switched off, the full-grid query gives 0. Switching it back on, the first-row rectangle covering columns 1–2 contains values 1 and 2, totaling 3.