2020 年 “聯想杯”第三屆上海理工大學程序設計競賽 H. Hay Mower 矩陣操作

1. 題目描述

1.1. Limit

Time Limit: 2.0 sec

Memory Limit: 256 MB

1.2. Problem Description

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×mn \times m matrix. The growth rate of weed in row ii and column jj is denoted as ai,ja_{i,j}, indicating that the weed will grow ai,ja_{i,j} units every beginning of the moment. At the end of moment 00, there is no weed on the farm.

Setsuna will use mower kk times, where the ii-th use occurs at the end of moment tit_i. 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 998244353998244353.


1.3. Input

The first line contains three integers n,m,k(1n,m500,1k3×105)n,m,k(1\le n,m \le 500,1 \le k \le 3 \times 10^5).

The next nn lines contains mm integers each, where the jj-th integer of the ii-th line is ai,ja_{i,j} (0ai,j1018)(0 \le a_{i,j} \le 10^{18}).

The ii-th of the next kk lines contains one character and twointegers.

  • rr xix_i tit_i - Setsuna clears the weeds in row xix_i at the end of moment tit_i.
  • cc yiy_i tit_i - Setsuna clears the weeds in column yiyi at the end of moment tit_i.

For each test case output a single line containing “YES” if it is possible to solve the jigsaw puzzle, or “NO” otherwise. You can print each letter in any case (upper or lower).

It is guaranteed that 1xin,1yim,1ti10181 \le x_i \le n,1 \le y_i \le m,1 \le t_i \le 10^{18} hold for 1ik1 \le i \le k and tit_i is strictly increasing.


1.4. Output

Output one integer indicating the answer modulo 998244353998244353.


1.5. Sample Input 1

2 2 3
1 2
3 4
r 1 5
c 2 6
r 1 7

1.6. Sample Output 1

45

1.7. Sample Input 2

3 4 1
1 2 3 4
5 6 7 8
9 10 11 12
r 1 1000000000000000000

1.8. Sample Output 2

172998509

1.10. Source

2020 年 “聯想杯”全國高校程序設計在線邀請賽暨第三屆上海理工大學程序設計競賽 H. Hay Mower


2. 解讀

直接用二維數組存矩陣,然後在每個 tit_i 對整個矩陣進行更新的話會超時。

通過對題目再進行分析,我們可以發現每次對矩陣進行操作時隻影響到某一行或某一列,所以我們用 timeMatrixtimeMatrix 存儲上一次更新時間,在每個 tit_i 對其操作的行或列進行運算,將該行/列在 titi1t_i - t_{i-1}時間內新增加的數值累加就得到了答案,然後將 timeMatrixtimeMatrix 存儲的上一次更新時間賦值爲 tit_i 即可。

還有一個需要注意的地方就是每次相乘運算都要先對兩個數取模,不然特別容易溢出。

3. 代碼

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;

const int NUM = 500 + 1;
const long long mod = 998244353;

long long timeMatrix[NUM][NUM] = { { 0 } };
long long growMatrix[NUM][NUM] = { { 0 } };

map<long long, pair<char, long long>> mp;

int main()
{
    long long n, m, k, ans = 0;
    scanf("%lld %lld %lld", &n, &m, &k);
    long long ai, aj, mark, t, lastMoment, moment = 0;
    char ch;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%lld", &growMatrix[i][j]);
        }
    }
    // 讀換行符
    getchar();
    while (k--) {
        ch = getchar();
        scanf("%lld %lld\n", &ai, &aj);
        mp[aj] = make_pair(ch, ai);
    }
    // 計算
    for (auto it = mp.begin(); it != mp.end(); it++) {
        t = it->first;
        ch = it->second.first;
        mark = it->second.second;

        // 行
        if (ch == 'r') {
            for (int i = 1; i <= m; i++) {
                ans = (ans + ((t - timeMatrix[mark][i]) % mod) * (growMatrix[mark][i] % mod)) % mod;
                timeMatrix[mark][i] = t;
            }
        } else {
            // 列
            for (int j = 1; j <= n; j++) {
                ans = (ans + ((t - timeMatrix[j][mark]) % mod) * (growMatrix[j][mark] % mod)) % mod;
                timeMatrix[j][mark] = t;
            }
        }
    }
    printf("%lld\n", ans);

    return 0;
}

聯繫郵箱[email protected]

CSDNhttps://me.csdn.net/qq_41729780

知乎https://zhuanlan.zhihu.com/c_1225417532351741952

公衆號複雜網絡與機器學習

歡迎關注/轉載,有問題歡迎通過郵箱交流。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章