Topic Links:
http://poj.org/problem?id=1195
Main topic:
A n*n matrix is given, the initialization is 0, and the coordinates start at (0,0). There are three operations:
Command 1:1 x y w; increase the point value at coordinates (x, y) by W
Command 2:2 x1 y1 x2 y2; Ask the lower-left corner coordinates (X1,Y1), the upper-right coordinate (x2,y2), and the number of matrices
command 3:3; No action required, exit.
Ideas:
Two-bit tree array single point update, interval evaluation of the simple problem. You can do it directly. At the end of the Matrix, the theorem of the repulsion is considered.
That is, ans = Query (x1-1,y1-1)-query (x1-1,y2)-query (x2,y1-1) +query (x2,y2).
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define LL __ int64using namespace Std;const int maxn = 1100;int Tree[maxn][maxn],n = 1025;int lowbit (int x) {return x & (-X);} void Update (int x,int y,int W) {for (int i = x; i <= n; i + = Lowbit (i)) for (int j = y; J <= N; j + = Lowbit ( j)) Tree[i][j] + = w;} ll Query (int x,int y) {ll sum = 0; for (int i = x; i > 0; I-= Lowbit (i)) for (int j = y; j > 0; J-= Lowbit (j)) sum + = Tree[i][j]; return sum;} int main () {int op,n; while (~SCANF ("%d%d", &op, &n)) {memset (tree,0,sizeof (Tree)); while (~SCANF ("%d", &op) && op! = 3) {if (op = = 1) {int x,y,w; scanf ("%d%d%d", &x,&y,&w); Update (X+1,Y+1,W); } else if (op = = 2) {int x1,y1,x2,y2; scanf ("%d%d%d%d", &x1, &y1,&x2,&y2); x1++,y1++,x2++,y2++; printf ("%i64d\n", Query (x1-1,y1-1)-query (x1-1,y2)-query (x2,y1-1) +query (x2,y2)); }}} return 0;}
POJ1195 Mobile Phones "tree-shaped Array" "two-dimensional"