Problem H. Hay Mower
Input file:
Standard Input Time limit: 2 seconds
Output file: Standard Output Memory limit: 256 megabytes
Are you tired of city life? Have you ever had illusions of pastoral peace? The clean atmosphere, the
closeness to nature and the gentle pace of living, all made Setsuna yearn for the pastoral life more.
In order to experience the simple pastoral life, Setsuna has moved to Star Valley and started her farming
journey.
Soon, she discovers the problem: overgrown weeds are harming her farm. In Chinese, we call it ”Sheng
Cao”. She realized that weeding should be put at the top priority.
The farm can be described as an n × m matrix. The growth rate of weed in row i and column j is
denoted as ai,j , indicating that the weed will grow ai,j units every beginning of the moment. At the
end of moment 0, there is no weed on the farm.
Setsuna will use mower k times, where the i-th use occurs at the end of moment ti
. Each use of the
mower completely removes weeds in a row or column.
Setsuna wonders how many units of weed she will remove.
The answer might be very large, so please output the desired answer modulo 998244353.
Input
The first line contains three integers n, m, k(1 ≤ n, m ≤ 500, 1 ≤ k ≤ 3 × 105
).
The next n lines contains m integers each, where the j-th integer of the i-th line is ai,j (0 ≤ ai,j ≤ 1018).
The i-th of the next k lines contains one character and two integers.
• r xi ti - Setsuna clears the weeds in row xi at the end of moment ti
.
• c yi ti - Setsuna clears the weeds in column yi at the end of moment ti
.
It is guaranteed that 1 ≤ xi ≤ n, 1 ≤ yi ≤ m, 1 ≤ ti ≤ 1018 hold for 1 ≤ i ≤ k and ti
is strictly increasing.
Output
Output one integer indicating the answer modulo 998244353.
Samples
Standard Input Standard Output
2 2 3
1 2
3 4
r 1 5
c 2 6
r 1 7
45
3 4 1
1 2 3 4
5 6 7 8
9 10 11 12
r 1 1000000000000000000
172998509
Note
Sample 1:
At the end of moment 0, the farm looks like
[0 0
0 0]
At the end of moment 5, Setsuna has cleared row 1 and 15 units of weed has been cut, so the farm looks
like
[0 0
15 20]
At the end of moment 6, Setsuna has cleared column 2 and 26 units of weed has been cut, so the farm
looks like
[1 0
18 0]
At the end of moment 7, Setsuna has cleared row 1 and 4 units of weed has been cut, so the farm looks
like
[0 0
21 4]
So the answer is 15 + 26 + 4 = 45 units.
思路:
这个题的大致题意就是,题目给了一大块草坪,把这一大块草坪分成n*m的矩阵,然后给出矩阵中每一小块的草的增长速度。给出Q次询问,每次询问都会割掉一行或者一列的草,问我们执行完这Q次询问后一共割掉了多少草
这个题一开始我直接用暴力的方法跑了下来,结果总是wa3,而且感觉这样做的复杂度实在是太大,后来我在不停地计算这个样例的时候发现,这个草的生长是一直都在进行的,不管这一行或者这一列的草割了还是没割,我们看第一次和第三次操作会发现,我们先给答案加上 t = 5 时割了的草之后,再计算 t = 7 时割草的数量时,答案需要加上的是 t = 7 - 5 ,所以我们只需要把每次询问的这一行或者这一列记录一下,寻找这Q次询问中这一行或者这一列的时间最大值就可以了,最后只需要计算用时间最大值乘以草的增长速度即可
int n,m,Q ;
ll R[501],C[501],r,nt,a[501][501];
char s[2];
int main(){
scanf("%d%d%d",&n,&m,&Q);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%lld",&a[i][j]);
a[i][j] %= mod;
}
}
while(Q--){
scanf("%s",s);
scanf("%lld%lld",&r,&nt);
if(s[0] == 'r') R[r] = nt;
else C[r] = nt;
}
ll ans = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
ans = (ans + max(R[i],C[j]) % mod * a[i][j] % mod) % mod;
}
}
printf("%lld\n",ans);
return 0;
}