#P9014. [USACO23JAN] Following Directions S

[USACO23JAN] Following Directions S

题目描述

Note: The time limit for this problem is 8s, four times the default.

Farmer John has a big square field split up into an (N+1)×(N+1)(1N1500)(N+1) \times (N+1) (1 \le N \le 1500) grid of cells. Let cell (i,j)(i,j) denote the cell in the ii-th row from the top and jj-th column from the left. There is one cow living in every cell (i,j)(i,j) such that 1i,jN1 \le i,j \le N, and each such cell also contains a signpost pointing either to the right or downward. Also, every cell (i,j)(i,j) such that either i=N+1i=N+1 or j=N+1j=N+1, except for (N+1,N+1)(N+1,N+1), contains a vat of cow feed. Each vat contains cow feed of varying price; the vat at (i,j)(i,j) costs ci,j(1ci,j500)c_{i,j} (1 \le c_{i,j} \le 500) to feed each cow.

Every day at dinner time, Farmer John rings the dinner bell, and each cow keeps following the directions of the signposts until it reaches a vat, and is then fed from that vat. Then the cows all return to their original positions for the next day.

To maintain his budget, Farmer John wants to know the total cost to feed all the cows each day. However, during every day, before dinner, the cow in in some cell (i,j)(i,j) flips the direction of its signpost (from right to down or vice versa). The signpost remains in this direction for the following days as well, unless it is flipped back later.

Given the coordinates of the signpost that is flipped on each day, output the cost for every day (with QQ days in total, 1Q15001 \le Q \le 1500).

输入格式

The first line contains N(1N1500)N (1 \le N \le 1500).

The next N+1N+1 lines contain the rows of the grid from top to bottom, containing the initial directions of the signposts and the costs ci,j of each vat. The first NN of these lines contain a string of NN directions R or D (representing signposts pointing right or down, respectively), followed by the cost ci,N+1c_{i,N+1}. The (N+1)(N+1)-th line contains NN costs cN+1,jc_{N+1,j}.

The next line contains Q(1Q1500)Q(1 \le Q \le 1500).

Then follow QQ additional lines, each consisting of two integers ii and j(1i,jN)j (1 \le i,j \le N), which are the coordinates of the cell whose signpost is flipped on the corresponding day.

输出格式

Q+1Q+1 lines: the original value of the total cost, followed by the value of the total cost after each flip.

题目大意

题目描述

注:本题时限为 8s,是默认时限的四倍。

Farmer John 有一个正方形的草地,草地被划分为了 (N+1)×(N+1)(1N1500)(N + 1) \times (N + 1)(1 \leq N \leq 1500) 的格子。设 (i,j)(i, j) 为从上到下、从左到右第 ii 行,第 jj 列的格子。每个满足 1i,jn1 \leq i, j \leq n 的格子 (i,j)(i, j) 之中都住着一头牛,而且每个这样的格子上都有一个路标指向右或下。除此之外,所有满足 i=N+1i = N + 1j=N+1j = N + 1 的格子,除了 (N+1,N+1)(N + 1, N + 1) 都会有一个饲料桶。牛在每个饲料桶进食需要的价格不同;位置 (i,j)(i, j) 上的桶喂饱一只牛需要价格 ci,j(1ci,j500)c_{i, j}(1 \leq c_{i, j} \leq 500)

每天晚饭时间,Farmer John 摇响晚餐铃时,所有牛都沿着路标的指向前进,直到它们遇到了饲料桶,之后它们会在它们自己遇到的饲料桶那里进食。第二天,所有牛又会回到自己原来的位置。

为了维持预算,Farmer John 想要知道每天喂食需要的价钱。然而,每天晚饭之前,总会有一头牛 (i,j)(i, j) 翻转它那里的路标(原来向下则变成向右,反之亦然)。被翻转的路标指向将在后面的日子里保持不变,除非它又被进行了翻转。

给出每天被翻转的路标的坐标,请输出每天喂食需要的价格(总共有 QQ 天,1Q15001 \leq Q \leq 1500)。

输入格式

第一行为 N(1N1500)N(1 \leq N \leq 1500)

接下来的 N+1N + 1 行从上到下输入初始的路标朝向和每个饲料桶的价格 ci,jc_{i, j}。前 NN 行每行包含一个长度为 NN 的字符串,其中每个字符只能是 RDR 表示向右,D 表示向下),之后是一个数,表示价格 ci,N+1c_{i, N + 1},第 (N+1)(N + 1) 行包含 NN 个数,依次表示价格 cN+1,jc_{N + 1, j}

接下来的一行为 Q(1Q1500)Q(1 \leq Q \leq 1500)

之后的 QQ 行,每行有两个整数 iij(1i,jNj(1 \leq i, j \leq N,表示每天被翻转的路标的坐标。

输出格式

Q+1Q + 1 行:第一行是初始的总价格,之后 QQ 行依次是每次被翻转后的总价格。

说明/提示

样例 1 解释

在第一次翻转之前,喂养在位置 (1,1)(1, 1)(1,2)(1, 2) 的牛需要的价格都为 11,喂养在 (2,1)(2, 1) 的牛需要的价格为 100100,喂养在 (2,2)(2, 2) 的牛需要的价格为 500500。总价格为 602602。第一次翻转后,在 (1,1)(1, 1) 处的路标由 R 变为 D,此时在位置 (1,1)(1, 1) 的牛喂养的价格变为 100100(其它牛的价格没有变化),所以总价为 701701。第二次和第三次翻转都在来回翻转同一个路标。第四次翻转后,在位置 (1,1)(1, 1) 和位置 (2,1)(2, 1) 的牛喂养的价格变为 500500,总价变为 15011501

数据范围

  • 测试点 242 - 4 中:1N,Q501 \leq N, Q \leq 50

  • 测试点 575 - 7 中:1N,Q2501 \leq N, Q \leq 250

  • 测试点 2102 - 10 中:每个路标初始朝向以及被翻转的路标为随机生成。

  • 测试点 111511 - 15 中:无特殊条件。

2
RR 1
DD 10
100 500
4
1 1
1 1
1 1
2 1
602
701
602
701
1501

提示

Explanation for Sample 1

Before the first flip, the cows at (1,1)(1,1) and (1,2)(1,2) cost 11 to feed, the cow at (2,1)(2,1) costs 100100 to feed, and the cow at (2,2)(2,2) costs 500500 to feed, for a total cost of 602602. After the first flip, the direction of the signpost at (1,1)(1,1) changes from R to D, and the cow at (1,1)(1,1) now costs 100100 to feed (while the others remain unchanged), so the total cost is now 701701. The second and third flips switch the same sign back and forth. After the fourth flip, the cows at (1,1)(1,1) and (2,1)(2,1) now cost 500500 to feed, for a total cost of 15011501.

Scoring

  • Inputs 242-4: 1N,Q501 \le N,Q \le 50
  • Inputs 575-7: 1N,Q2501 \le N,Q \le 250
  • Inputs 2102-10: The initial direction in each cell, as well as the queries, are uniformly randomly generated.
  • Inputs 111511-15: No additional constraints.