POJ 1195 Mobile Phones (two-dimensional tree-like array)

Source: Internet
Author: User

Title Link: POJ 1195 Mobile Phones

"The main topic"




, the start operation is 0 initialized s * S size map with a value of 0

Action 1, enter X Y A, change the value of coordinates (x, y) in the map to a

Operation 2, input L B R T query interval (x, y) l<=x<=r, b<=y<=t, output the rectangle interval and;

Action 3 Closing the program


A typical two-dimensional tree-like array

Two-dimensional tree arrays and one-dimensional tree arrays are the same principle.


We first look at the one-dimensional tree array C "MAXN",

Its storage structure

C1 = A1
C2 = A1 + A2
C3 = A3
C4 = A1 + A2 + A3 + A4
C5 = A5
C6 = A5 + A6


C7 = A7
C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8
......
C16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 + A11 + A12 + A13 + A14 + A15 + A16
......

The second dimension of the two-dimensional tree array is the same as the normal one-dimensional tree array.

Set the two-dimensional tree array to c[][]


Example: Take a look at the composition of c[][].

Set the original two-dimensional array as:
A[][]={{a11,a12,a13,a14,a15,a16,a17,a18,a19},
{A21,a22,a23,a24,a25,a26,a27,a28,a29},
{A31,a32,a33,a34,a35,a36,a37,a38,a39},
{a41,a42,a43,a44,a45,a46,a47,a48,a49}};
So it corresponds to a two-dimensional tree-like array c[][]?


B[1]={a11,a11+a12,a13,a11+a12+a13+a14,a15,a15+a16,...} This is the first row of a one-dimensional tree array
B[2]={a21,a21+a22,a23,a21+a22+a23+a24,a25,a25+a26,...} This is a one-dimensional tree array of the second row
B[3]={a31,a31+a32,a33,a31+a32+a33+a34,a35,a35+a36,...} This is a one-dimensional, tree-like array of the third row
B[4]={a41,a41+a42,a43,a41+a42+a43+a44,a45,a45+a46,...} This is a one-dimensional tree array of line fourth


So:
C[1][1]=a11,c[1][2]=a11+a12,c[1][3]=a13,c[1][4]=a11+a12+a13+a14,c[1][5]=a15,c[1][6]=a15+a16,...
This is a one-dimensional tree array of the first row of a[][]

c[2][1]=a11+a21,c[2][2]=a11+a12+a21+a22,c[2][3]=a13+a23,c[2][4]=a11+a12+a13+a14+a21+a22+a23+a24,c[2][5]=a15+ A25,c[2][6]=a15+a16+a25+a26,...
This is the tree array after the first row and the second row of the a[][] array are added

C[3][1]=a31,c[3][2]=a31+a32,c[3][3]=a33,c[3][4]=a31+a32+a33+a34,c[3][5]=a35,c[3][6]=a35+a36,...
This is a one-dimensional tree array of the third row of a[][]

C[4][1]=a11+a21+a31+a41,c[4][2]=a11+a12+a21+a22+a31+a32+a41+a42,c[4][3]=a13+a23+a33+a43,...
This is the a[][] array of the first row + the second row + the third row + the fourth row after the tree-like array


attention to the bold part , you can see that the relationship between the storage value between the second dimension and the first dimension between the storage of the relationship is the same,

The number of items stored is lowbit (i).

So modify and query the function, nested two for loop can be completed

void Modify (int a,int b,int val) {for (Int. i=a;i<=n;i+=lowbit (i)) {for (int j=b;j<=n;j+=lowbit (j)) S[i][j]+=val;}} int sum (int x,int y) {int ans=0;for (int i=x;i>0;i-=lowbit (i)) {for (int j=y;j>0;j-=lowbit (j)) Ans+=s[i][j];} return ans;}


The above data source: A tree-like array speaks very good article


"Source Code"

 #include <iostream> #include <string> #include <cstring> #include < cstdio>using namespace Std;const int maxn = 1111;int s[maxn][maxn];int n;int lowbit (int x) {//x binary represents the last 1 position, and of course returns the The value is the binary number consisting of the last 1 and its subsequent 0 return x& (-X);} void Modify (int a,int b,int val) {for (Int. i=a;i<=n;i+=lowbit (i)) {for (int j=b;j<=n;j+=lowbit (j)) S[i][j]+=val;}} int sum (int x,int y) {int ans=0;for (int i=x;i>0;i-=lowbit (i)) {for (int j=y;j>0;j-=lowbit (j)) Ans+=s[i][j];} return ans;} int getsum (int x1,int y1,int x2,int y2) {return sum (x2,y2)-sum (x1-1,y2)-sum (x2,y1-1) +sum (x1-1,y1-1);//First the maximum interval, minus the excess above, The left side is extra, plus the upper left corner minus two times. }int Main () {int op;while (scanf ("%d%d", &op,&n)!=eof) {memset (s,0,sizeof (s)); int Order;int A,b,c,d;while ( scanf ("%d", &order)!=eof && order! = 3) {if (order==1) {scanf ("%d%d%d", &a,&b,&c); Modify (a+1,b+ 1,C);} else {scanf ("%d%d%d%d", &a,&b,&c,&d); int ans = getsum (a+1,b+1,c+1,d+1);p rintf ("%d\n", ans);}} return 0;} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1195 Mobile Phones (two-dimensional tree-like array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.