#33470: 20行解前綴和,直接加起來省去多餘的步驟


luke921021luke@gmail.com (wang天行 喵!)

學校 : 不指定學校
編號 : 192923
來源 : [223.137.230.94]
最後登入時間 :
2023-12-06 21:27:05
a694. 吞食天地二 | From: [114.36.248.50] | 發表日期 : 2023-01-08 21:42

別再一堆for或是while了

 


#include 

using namespace std;

int main(){
    int n, m, x1, y1, x2, y2, total[505][505];
    while (cin >> n >> m){
        for (int a = 1; a <= n; a++)
            for (int b = 1; b <= n; b++){
                cin >> total[a][b];
                total[a][b] += total[a - 1][b] + total[a][b - 1] - total[a - 1][b - 1];
            }
        while (m--){
            cin >> x1 >> y1 >> x2 >> y2;
            cout << total[x2][y2] - total[x1 - 1][y2] - total[x2][y1 - 1] + total[x1 - 1][y1 - 1] << endl;
        }
    }
    return 0;
}
 
ZeroJudge Forum