記得要while cin>>
cin>>p[i][j];
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+p[i][j];
cin>>x1>>y1>>x2>>y2;
cout<<dp[x2][y2]-dp[x1-1][y2]-dp[x2][y1-1]+dp[x1-1][y1-1]<<endl;
記得要while cin>>
cin>>p[i][j];
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+p[i][j]; 我可以問一下大神 這行怎麼來的嗎?
cin>>x1>>y1>>x2>>y2;
cout<<dp[x2][y2]-dp[x1-1][y2]-dp[x2][y1-1]+dp[x1-1][y1-1]<<endl;
利用(類)動態規劃, 先把n*n個位置 (1,1)-(x,y)的和 建表
dp(x1,y1,x2,y2) = dp(x2,y2) - dp(x1-1,y2) - dp(x2, y1-1) + dp(x1-1,y1-1), 以免python TLE(3s)