Hust oj 1693 Difficult work(水题)
2016-08-09 16:42
155 查看
Difficult work | ||||||
| ||||||
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); } } } |
相关文章推荐
- Hust oj 1673 2013新的旅程(水题)
- Hust oj 1754 Minimum Scalar Product(水题)
- Hust oj 2000 极差(水题)
- Hust oj 1720 Fibonacci Numbers(水题)
- Hust oj 1384 Leyni, LOLI and Toasts(水题)
- Hust oj 1431 摞盘子(水题)
- Hust oj 1046 Balls Collision(水题)
- Hust oj 1369 Buy Computers(水题)
- Hust oj 1497 Death Knight Hero(字符串水题)
- Hust oj 1394 XianGe的游戏I(水题)
- Hust oj 2121 你猜猜(水题)
- Hust oj 2125 钱多多(水题)
- Hust oj 1197 Cuts the cake(水题)
- Hust oj 1648 Tailmon与序列(水题)
- Hust oj 2235 毒(水题)
- Hust oj 1608 XianGe的旅途(水题)
- Hust oj 1629 统计图(水题)
- Hust oj 1945 纸牌小游戏(模拟水题)
- Hust oj 2116 Maximum continuous product(水题)
- hdu 4858 水题