Conclusions about tree arrays from multi-School Joint nudt Question 2 cube

Source: Internet
Author: User

At a glance, this question is a typical tree array solution for updating the interval lookup points. However, if a three-dimensional tree array is used, there will be a problem: how to split it?

Let's start with a simple question. For one-dimensional situations, we can update the intervals (a, B), so we can add v At A, B + 1 minus V.

For two-dimensional situations, it is troublesome to update the region (x1, Y1) to (X2, Y2), then add V at (x1, Y1, subtract V at (X2 + 1, Y1) and (x1, y2 + 1), and add V at (X2 + 1, y2 + 1.

So what about 3D? After drawing, I got the conclusion that (x1, Y1, Z1) plus V, and (x1, Y1, Z1) minus V, these points include (X2 + 1, Y1, Z1), (x1, y2 + 1, Z1), (x1, Y1, Z2 + 1), and (X2 + 1, y2 + 1, Z2 + 1), and the rest is v.

The three tree arrays with different dimensions are combined. The source vertex P adds V and P minus V at the adjacent odd position, and the even position is added v, in this way, you can quickly write down all the cutting conditions.

 

My code:

 

# Include <iostream> <br/> # include <cstdio> <br/> # include <cstring> <br/> # include <cstdlib> </P> <p> using namespace STD; </P> <p> const int max = 110; <br/> int N; <br/> int tree [Max] [Max] [Max]; </P> <p> void add (int x, int y, int Z, int v) <br/> {<br/> int tmp1 = y; <br/> int tmp2 = z; <br/> while (x <= N) <br/> {<br/> Y = tmp1; <br/> while (Y <= N) <br/> {<br/> Z = tmp2; <br/> while (z <= N) <br/> {<br/> tree [x] [y] [Z] + = V; <br/> Z + = Z &-Z; <BR/>}< br/> Y + = Y &-y; <br/>}< br/> X + = x &-X; <br/>}</P> <p> int read (int x, int y, int Z) <br/>{< br/> int tmp1 = y; <br/> int tmp2 = z; <br/> int sum = 0; <br/> while (X) <br/>{< br/> Y = tmp1; <br/> while (y) <br/>{< br/> Z = tmp2; <br/> while (z) <br/> {<br/> sum + = tree [x] [y] [Z]; <br/> Z-= Z &-Z; <br/>}< br/> Y-= Y &-y; <br/>}< br/> X-= x &-X; <br/>}</P> <p> return sum; <br/>}</P> <p> int main () <br/>{< br/> int m; <br/> int x1, x2, Y1, Y2, Z1, Z2, OP; </P> <p> while (scanf ("% d", & N, & M )! = EOF) <br/>{< br/> for (INT I = 1; I <= N; I ++) <br/> for (Int J = 1; j <= N; j ++) <br/> for (int K = 1; k <= N; k ++) <br/> tree [I] [J] [k] = 0; <br/> for (INT I = 0; I <m; I ++) <br/>{< br/> scanf ("% d", & OP); <br/> If (OP) <br/>{< br/> scanf ("% d", & X1, & Y1, & Z1 ); <br/> scanf ("% d", & X2, & Y2, & Z2); <br/> Add (x1, Y1, Z1, 1 ); <br/> Add (X2 + 1, Y1, Z1,-1); <br/> Add (x1, y2 + 1, Z1,-1 ); <br/> Add (x1, Y1, Z2 + 1,-1); <br/> Add (X2 + 1, y2 + 1, Z1, 1 ); <br/> Add (X2 + 1, Y1, Z2 +); <br/> Add (x1, y2 + 1, Z2 + ); <br/> Add (X2 + 1, y2 + 1, Z2 + 1,-1 ); <br/>}< br/> else <br/> {<br/> scanf ("% d", & X1, & Y1, & Z1 ); <br/> printf ("% d/N", read (x1, Y1, Z1) % 2 ); <br/>}</P> <p> return 0; <br/>}< br/>

 

Conclusion: after each question is done, you need to reflect on it. If you don't have to do one question, you 'd better spend time optimizing the code. After all, this is better for you. The question is not expensive, but expensive.

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.