#FT0000174. Những dải đèn trang trí (Garlands)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những dải đèn trang trí (Garlands)
Garlands
Source: Codeforces
Version: Phước Hưng OJ Extended
Problem Statement
Alesha places garlands on an 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 contributes its weight when its garland is on, and contributes 0 otherwise. All garlands are initially switched on.
Process events in chronological order:
SWITCH i: toggle the on/off state of the entire -th garland.ASK x1 y1 x2 y2: find the total weight of currently lit bulbs within the inclusive rectangle .
Bulbs never move or change their weights. The number of ASK events is guaranteed to be at most 2000.
Input
The first line contains . For each garland, a line gives its length len, followed by len lines x y w listing bulbs in their chain order. An integer follows, then 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): ; ; ; ; at most 30
ASKevents; , ; no cell holds multiple bulbs, consecutive bulbs in a garland are side-adjacent; all coordinates are valid, with and . - Subtask 2 (30 points): ; ; ; ; at most 500
ASKevents; , ; no cell holds multiple bulbs, consecutive bulbs in a garland are side-adjacent; all coordinates are valid, with and . - Subtask 3 (50 points): ; ; ; ; at most 2000
ASKevents; , ; no cell holds multiple bulbs, consecutive bulbs in a garland are side-adjacent; all coordinates are valid, with and .
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 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 , 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 . 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.