题解:AT_abc464_e [ABC464E] Fill-Rect Query

· · 题解

这道题的核心思路是将矩形覆盖问题转化为“标记右下角”的二维后缀最大值问题。

题中给出,每次覆盖的左上角一定是(1,1),而每次操作后都会覆盖这次操作区域里的所有字母,所以只需要标记每次操作的右下角,按操作顺序做一遍二维后缀按操作顺序的最大值,再按每个位置的二维后缀最大值所对应的字母输出即可。

时间复杂度O(h×w)。

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1000010, Q = 200010;
int h, w, q;
char op[Q];

int main () {
    cin >> h >> w >> q;
    vector<int> d(h * w + 10, 0);//将二维压成一维 
    for (int i = 1; i <= q; i++) {
        int r, c; char x;
        cin >> r >> c >> x;
        op[i] = x;//记录该操作对应的字母 
        d[(r - 1) * w + c] = i;//在该操作的右下角的位置记录该操作的顺序i 
    }
    for (int i = h; i >= 1; i--) {
        for (int j = w; j >= 1; j--) {
            int pos = (i - 1) * w + j;
            int res = d[pos];

            if(i + 1 <= h) res = max(res, d[pos + w]);
            if(j + 1 <= w) res = max(res, d[pos + 1]); //二维后缀按操作顺序的最大值

            d[pos] = res;
        }
    }
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            int v = d[(i - 1) * w + j];//按每个位置的二维后缀最大值所对应的字母输出
            if(!v) printf("A");
            else printf("%c", op[v]);
        }
        puts("");
    }
    return 0;
}