Matrix time limit: 4 Sec Memory Limit: MB
Description
Given a 1000*1000 two-dimensional matrix, each number in the initial matrix is 1, and then there are 4 operations for the matrix.
S x1 y1 X2 y2: Calculates (x1,y1), and (x2,y2) all elements within the matrix.
A x y V: Adds (x, y) to the V
D x y V: reduce (x, y) to V
M x1 y1 x2 y2 V: Transfers v in (X1,Y1) element to (X2,y2).
All operands are a positive number.
If an operation drops the element in the matrix below 1, it is treated as 1.
X, y starts numbering from 0.
Input
The first line is a number T, which represents the number of examples:
The first line of each group of examples is a number m, representing m operations, m<100000
The next M-line represents m operations
Output
Each set of examples outputs a
Case I:
I represent the example of the first I
For each operation s, the output line represents the result of the calculation.
All results will not exceed int range
Sample Input
14A 1 1 1M 1 1 2 2 1D 2 2 1S 1 1 2 2
Sample Output
Case 1:4
Solution thinking: Very obvious is a very typical two-dimensional tree array problem, the tree array part is completely template, just to solve the actual problem in detail, a bit of skill. When I first saw the problem, I immediately thought of the matrix of the once-cut building, which felt very similar. And then it was done, and then it started, to initialize the C array of the tree array, since the matrix has an initial value of 1. A two-layer loop is used. One call to update (). The result was timed out.
。
。 And then in other places to optimize, or time-out ...
Just thought of a way. Initialize all c arrays directly to 0. So you don't have to call Update () again (because C has to be in front of him, and now it's all 0, and of course, 0). Directly plus the number of nodes in that interval (since the original matrix should all be initialized to 1, but I started all initialized to 0.) Each node is reduced by 1, the number of nodes that is the area of the interval) can be.
There is also a requirement to ensure that the original matrix element cannot be less than 1 after operation. Detailed to calculate the current position of the element value, before the operation of the number of e-changes. You can then run the operation directly. There is also a way to find the element of the current position, using the area summation function to sum the current point directly. i.e. Map[a][b] = Getsum (A, B, a, b). See Code
Lou Master matrix See: http://blog.csdn.net/u013446688/article/details/38194977
AC Code:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace std;const int MAXN = 1002; #define LOWBIT (x) x & (-X)//lowbit function int C[MAXN][MAXN]; The c array of the tree array inline int update (int x, int y, int d) {//update function int i, j;for (i=x; i<=1000; i+=lowbit (i)) for (j=y; j& lt;=1000; J+=lowbit (j)) C[i][j] + = D; inline int sum (int x, int y) {//sum function int ans = 0;int I, j;for (i=x; i>0; i-=lowbit (i)) for (j=y; j>0; J-=lowbi T (j)) ans + = C[i][j];return ans; int getsum (int x1, int y1, int x2, int y2) {//zone sum return sum (x2, y2)-sum (x1-1, y2)-sum (x2, y1-1) + sum (x1-1, y1 -1);} int main () {//freopen ("In.txt", "R", stdin); int T, M, A, B, CC, D, e;string p;scanf ("%d", &t); for (int t=1; t<=t; t++) { scanf ("%d", &m);p rintf ("Case%d:\n", T), memset (c, 0, sizeof (c)), and while (m--) {cin>>p;if (p[0] = = ' A ') {scanf ("%d %d%d ", &a, &b, &e); a++; B++;update (A, B, e);} else if (p[0] = = ' D ') {scanf ("%D%d%d ", &a, &b, &e); a++; B++;int k = Getsum (A, B, a, b); The value of the element at the current position if (K-e < 0) e = k; Change E to ensure that the element is not less than 1update (A, B,-e) after the operation;} else if (p[0] = = ' M ') {scanf ("%d%d%d%d%d", &a, &b, &CC, &d, &e); a++; b++; cc++; d++;int k = Getsum (A, B, A, b); Ditto if (K-e < 0) E = K;update (A, B,-e); Update (CC, D, e);} ELSE{SCANF ("%d%d%d%d", &a, &b, &CC, &d); a++; b++; cc++; D++;if (CC < a) swap (A, CC); if (d < b) Swap ( b, d); int ans = Getsum (A, B, CC, D) + (cc-a+1) * (d-b+1); Find matrix area and printf ("%d\n", ans);}}} return 0;}
Hlju 1188 Matrix (two-dimensional tree-like array)