4 条题解

  • 1
    @ 2024-2-11 21:33:21
    #include <iostream> 
    using namespace std;
    #define ll long long
    ll n, m, k, x1, y1, x2, y2, s,a[1001][1001], sum[1001][1001];
    int main()
    {
        ios::sync_with_stdio(0);
        cin >> n >> m >> k;
        for (ll i = 1; i <= n; i++)
        {
            for (ll j = 1; j <= m; j++)
            {
                cin >> a[i][j];
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
            }
        }
        for (ll i = 1; i <= k; i++)
        {
            cin >> x1 >> y1 >> x2 >> y2;
            s = sum[x2][y2] - (sum[x1 - 1][y2] + sum[x2][y1 - 1] - sum[x1 - 1][y1 - 1]);
            cout << s << endl;
        }
        return 0;
    }
    

    因为y1所以只能用iostream了

    • 0
      @ 2024-3-17 20:19:12
      #include <bits/stdc++.h>
      using namespace std;
      const int MAX=1010;
      int n,m,k,cnt[MAX][MAX],prefix[MAX][MAX];
      int main(){
      	scanf("%d%d%d",&n,&m,&k);
      	for (int i=1;i<=n;i++){
      		for (int j=1;j<=m;j++){
      			scanf("%d",&cnt[i][j]);
      			prefix[i][j]=prefix[i-1][j]+prefix[i][j-1]-prefix[i-1][j-1]+cnt[i][j];
      		} 
      	}
      	int l1,l2,r1,r2;
      	for (int i=1;i<=k;i++){
      		scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
      		printf("%d\n",prefix[l2][r2]-prefix[l1-1][r2]-prefix[l2][r1-1]+prefix[l1-1][r1-1]);
      	}
      	return 0;
      }
      
      • 0
        @ 2023-5-2 22:19:47
        #include <bits/stdc++.h> 
        using namespace std;
        int main()
        {
        	int n, m, k, x1, y1, x2, y2, s;
            cin >> n >> m >> k;
            int a[n + 1][m + 1], sum[n + 1][m + 1];
            memset(sum, 0, sizeof(sum));
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= m; j++)
                {
                    cin >> a[i][j];
                    sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
                }
            }
            for (int i = 1; i <= k; i++)
            {
                cin >> x1 >> y1 >> x2 >> y2;
                s = sum[x2][y2] - (sum[x1 - 1][y2] + sum[x2][y1 - 1] - sum[x1 - 1][y1 - 1]);
                cout << s << endl;
            }
            return 0;
        }
        
        • 0
          @ 2022-8-5 19:45:57

          此题是较简单的二维前缀和模板题 前缀和、差分复习直播 链接:https://www.bilibili.com/video/BV1MN4y1j7dR?spm_id_from=333.999.0.0&vd_source=3136e9894179057b25e6f1820e1738da

          代码如下:

          #include <bits/stdc++.h>
          using namespace std;
          int n, m, k, sum[1005][1005], a[1005][1005];
          int main()
          {
              cin >> n >> m >> k;
              for (int i = 1; i <= n; i++)
              {
          	for (int j = 1; j <= m; j++)
                  {
          	    cin >> a[i][j];
          	    sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
          	}
              }
              for (int i = 1; i <= k; i++)
              {
                  int x1, y1, x2, y2;
          	cin >> x1 >> y1 >> x2 >> y2;
          	cout << sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1] << endl;
              }
              return 0;
          }
          
          • 1

          信息

          ID
          1057
          时间
          1000ms
          内存
          128MiB
          难度
          4
          标签
          递交数
          128
          已通过
          64
          上传者