您的位置:首页 > 其它

Hust oj 1693 Difficult work(水题)

2016-08-09 16:42 155 查看
Difficult work
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 147(58 users)Total Accepted: 80(50 users)Rating: 





Special Judge: No
Description
     小D故意难为GiGi兔,给她一个很棘手的work。

先给出一个n*n的方形,每个格子内放入一个数字a(0<=a<=100)。然后算出以(x1, y1), (x2, y2)为对角线的矩形内所有数字的和。

Input
输入包含多组测试数据。

第一行包含两个正整数n和m(1=<n<=500,1=<m<=20000)。

接下来m行,每行包含四个整数x1, y1, x2, y2。(1<= x1<=x2, y1<= y2 <=n)。

Output
每行包括一个数字和。(保证在32位整数内)

Sample Input
3 3

1 2 3

4 5 6

7 8 9

1 1 2 2

1 1 3 3

2 1 3 3

Sample Output
12

45

39

直接暴力会超时,需要事先处理出每行的和,然后相减,看到有人用树状数组做,跑了400多MS,也是费力不讨好

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

int n,m;
const int maxn = 505;
int a[maxn][maxn];
int x;
int x1,y1,x2,y2;

int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&x);
a[i][j] = a[i][j-1] + x;
}
}
for(int i=0;i<m;i++)
{
int sum = 0;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i=x1;i<=x2;i++)
{
sum += (a[i][y2] - a[i][y1-1]);
}
printf("%d\n",sum);
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hustoj1693