不懂就问,哪里错了?

P3397 地毯

LacusMortis @ 2024-10-26 00:09:17

#include<iostream>
using namespace std;
int main()
{
    int n,m,a[1000][1000],x1,x2,y1,y2;
    cin>>n>>m;
    for(int i=0;i<m;i++)
        {cin>>x1>>x2>>y1>>y2;
        for(int j=x1;j<=x2;j++)
            for(int k=y1;k<=y2;k++)
                a[j][k]++;}
    for(int i=0;i<n;i++)
        {for(int j=0;j<n;j++)
            cout<<a[i][j]<<' ';
        cout<<endl;}
    return 0;
}

by postpone @ 2024-10-26 01:30:23

@LacusMortis 复杂度 O(n^2m) 过不了,需要用前缀和优化。


by postpone @ 2024-10-26 01:34:41

    vector f(n + 2, vector<int>(n + 2));
    for (int i = 0; i < m; i++) {
        int x, y, u, v;
        cin >> x >> y >> u >> v;
        f[x][y]++;
        f[x][v + 1]--;
        f[u + 1][y]--;
        f[u + 1][v + 1]++;
    }

    vector pre(n + 1, vector<int>(n + 1));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + f[i][j];
        }
    }

by niuniudundun @ 2024-11-06 20:27:30

@LacusMortis


by niuniudundun @ 2024-11-06 20:36:17

@LacusMortis 求关


by Jiangminghui123 @ 2024-11-07 13:44:53

@LacusMortis a数组开1001大小


|