曼哈顿距离

版权声明:来自星空计算机团队——申屠志刚 https://blog.csdn.net/weixin_43272781/article/details/84898788

http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4438

题解:

std

C++版本一:


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <time.h>

using namespace std;

const int maxn = 1e3 + 100, maxm = 1e6 + 100, mod = 1000000007;
typedef long long ll;
typedef pair<int, int> P;
ll s[maxn][maxn];
int n, m;
ll x[maxn][maxn], y[maxn][maxn];
void solve(){
    memset(x, 0,sizeof x);
    memset(y, 0,sizeof y);
    scanf("%d%d", &n, &m);
	for(int i=0; i<m; ++i)
	{
		int x1, y1, x2, y2;
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		x[x1+1][y1] += 1;
		x[x2+1][y1] += -1;
		x[x2+1][y1] += -(x2 - x1);
		x[x2+2][y1] += (x2 - x1);
		x[x1+1][y2+1] -= 1;
		x[x2+1][y2+1] -= -1;
		x[x2+1][y2+1] -= -(x2 - x1);
		x[x2+2][y2+1] -= (x2 - x1);

		y[x1][y1+1] += 1;
		y[x1][y2+1] += -1;
		y[x1][y2+1] += -(y2 - y1);
		y[x1][y2+2] += (y2 - y1);
		y[x2+1][y1+1] -= 1;
		y[x2+1][y2+1] -= -1;
		y[x2+1][y2+1] -= -(y2 - y1);
		y[x2+1][y2+2] -= (y2 - y1);
	}
	for(int t=0; t<2; ++t)
		for(int j=1; j<=n; ++j)
			for(int i=1; i<=n; ++i)
				x[i][j] += x[i-1][j];
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=n; ++j)
			x[i][j] += x[i][j-1];
	for(int t=0; t<2; ++t)
		for(int i=1; i<=n; ++i)
			for(int j=1; j<=n; ++j)
				y[i][j] += y[i][j-1];
	for(int j=1; j<=n; ++j)
		for(int i=1; i<=n; ++i)
			y[i][j] += y[i-1][j];


	ll ans = 1;
	bool flag = false;
	for(int i=1; i<=n; ++i)
        for(int j=1; j<=n; ++j)
			if(x[i][j]+y[i][j]) ans = (ans * (x[i][j]+y[i][j]))%mod, flag = true;
    if(flag)
	cout << ans << endl;
    else puts("0");
}
char fre[10] = "data1.in";
char fot[20] = "check1.out";
int main()
{
    solve();
//	int T = 6;
//    for(int cas = 1; cas <= T; ++cas){
//        fre[4] = ('0' + cas);
//        fot[5] = ('0' + cas);
//        freopen(fre, "r", stdin);
////        freopen(fot, "w", stdout);
//        solve();
//    }
	return 0;
}

 C++版本二


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
char fre[10] = "data1.in";
char fot[20] = "data1.out";

int tot[N][N];
int sum[N][N];
void solve(){
    int n, m, u, l, d, r;
    scanf("%d%d", &n, &m);
    memset(tot, 0, sizeof tot);
    memset(sum, 0, sizeof sum);
    for(int i = 1; i <= m; ++i){
        scanf("%d%d%d%d", &u, &l, &d, &r);
        sum[u][l] += (u + l);
        sum[u][r + 1] -= (u + l);
        sum[d + 1][l] -= (u + l);
        sum[d + 1][r + 1] += (u + l);
        tot[u][l] ++;
        tot[u][r + 1] --;
        tot[d + 1][l] --;
        tot[d + 1][r + 1] ++;
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            sum[i][j] += sum[i][j - 1];
            tot[i][j] += tot[i][j - 1];
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            sum[i][j] += sum[i - 1][j];
            tot[i][j] += tot[i - 1][j];
        }
    }
    LL ans = 1, now = 0;
    bool flag = false;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            now = tot[i][j] * (i + j) - sum[i][j];
//            sum[i][j] = 0, tot[i][j] = 0;
            if(now) ans = ans * now % mod, flag = true;
        }
    }
    if(!flag)   ans = 0;
    printf("%I64d\n", ans);
}
int main()
{
    solve();
//    int T = 6;
//    for(int cas = 1; cas <= T; ++cas){
//        fre[4] = ('0' + cas);
//        fot[4] = ('0' + cas);
//        freopen(fre, "r", stdin);
//        freopen(fot, "w", stdout);
//        solve();
//    }
    return 0;
}
/*
419683495
775697741
996935516
391348459
0
797953655
*/

猜你喜欢

转载自blog.csdn.net/weixin_43272781/article/details/84898788