思路

  • 因为具有覆盖性,所以从后往前填最优
  • 如果一个位置没有被填过,则用nex记录这个矩形的最右端(因为之前的位置已经被填过了,之后填这后面的位置就行了)
  • 否则就转移到之前记录的nex的位置

代码

// Problem: 九峰与蛇形填数
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/9984/G
// Memory Limit: 524288 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=3005;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);

int n,m;
int a[N][N];
int nex[N][N];
int x[N],y[N],k[N];

void solve(){
    cin>>n>>m;
    rep(i,1,m) cin>>x[i]>>y[i]>>k[i];

    per(ii,m,1){
        rep(i,x[ii],x[ii]+k[ii]-1){
            rep(j,y[ii],y[ii]+k[ii]-1){
                if(!a[i][j]){
                    nex[i][j]=y[ii]+k[ii]-1;
                    if((i-x[ii])&1) a[i][j]=(i-x[ii]+1)*k[ii]-j+y[ii];
                    else a[i][j]=(i-x[ii])*k[ii]+j-y[ii]+1;
                }
                else j=nex[i][j];
            }
        }
    }

    rep(i,1,n) rep(j,1,n) cout<<a[i][j]<<" \n"[j==n];
}


int main(){
    ios::sync_with_stdio(0);cin.tie(0);
//    int t;cin>>t;while(t--)
    solve();
    return 0;
}