Tree-like array

Source: Internet
Author: User
Tags x2 y2

Tree-like array summary

First we say the correctness of this algorithm, starting from one dimension, we assume that there are two points, I,i1,i has been added lowbit (i), and I1 has been subtracted from lowbit (I1), which is two strict monotone sequence, if the two sequences have intersections, there is only one intersection K, then from the i~ I1 This section and can be used c[k] to record (and only one c[k] satisfied).

A two-dimensional tree array is similar, first clear two-dimensional c[i][j] is recorded with (I,J) for the lower right, (I-lowbit (i) +1, J-lowbit (j) +1) the upper left of the rectangle contains the elements of the and (as for why cannot be understood as a row of one-dimensional tree array, This is because the time complexity will change from (Logn) ^2 to Nlogn), assuming there is a point (I1, J1), I and I1 can determine the horizontal axis of the intersection, and J and J1 can determine the intersection, the intersection is determined.

Example :

problem 1. Matsum
Input file:matsum.in
Output File:matsum.out
Time Limit:1 Second
gives a matrix of n M, with the initial total element of 0, with two operations:
? Modify X Y D: Adds the value of position (x, y) to D.
? Query x1 y1 x2 y2: Ask (x1,y1)-(X2,y2) The and of this rectangle. 1
Input
Line 1th, 3 integers n; M Q, which indicates the number of rows, columns, and queries of the matrix, respectively.
The next Q line, each line is one of the above two actions.
Output
For each query, the output asks for the result.
Sample
Matsum.in Matsum.out
2 2 4
Modify 1 1 4
Query 1 1 2 2
Modify 2 2-4
Query 1 1 2 2
40
Note
? For 30% of data, 1 N; M Q 500;
? For another 30% of the data, N = 1; 1 M; Q 105;
? For another 40% of the data, 1 N; M 103; 1 Q 105;
? For all data, JDJ 100; 1 x N; 1 y M; 1 x1 x2 N; 1 y1 y2 M;

#include <cstdio>intN, M, q;structCase1 {intbit[100010]; voidModifyintPintd) { for(inti = P; I <= m; i + = i &-i) bit[i]+=D; }    intQueryintr) {intRT =0;  for(inti = r; I I-= i &-i) RT+=Bit[i]; returnRT; }    intQueryintLintr) {returnQuery (r)-Query (L-1); }    voidsolve () { while(q--) {            Charss[ -]; scanf ("%s", SS); if(ss[0] =='m' ) {                intx, y, D; scanf ("%d%d%d", &x, &y, &d);            Modify (y, D); } Else {                intx1, y1, x2, y2; scanf ("%d%d%d%d", &x1, &y1, &AMP;X2, &y2); printf ("%d\n", Query (y1,y2)); }}}}case1;structCase2 {intbit[1100][1100]; voidModifyintXintYintd) { for(inti = x; I <= N; i + = i &-i) for(intj = y; J <= M; J + = J &-j) Bit[i][j]+=D; }    intQueryintXinty) {intRT =0;  for(inti = x; I I-= i &-i) for(intj = y; J J-= J &-j) RT+=Bit[i][j]; returnRT; }    intQueryintX1,intY1,intX2,inty2) {        returnQuery (X2,Y2)-Query (x1-1, y2)-Query (x2,y1-1) + Query (x1-1, y1-1); }    voidsolve () { while(q--) {            Charss[ -]; scanf ("%s", SS); if(ss[0] =='m' ) {                intx, y, D; scanf ("%d%d%d", &x, &y, &d);            Modify (x, Y, D); } Else {                intx1, y1, x2, y2; scanf ("%d%d%d%d", &x1, &y1, &AMP;X2, &y2); printf ("%d\n", Query (x1,y1,x2,y2)); }}}}case2;intMain () {Freopen ("matsum.in","R", stdin); Freopen ("Matsum.out","W", stdout); scanf ("%d%d%d", &n, &m, &q); if(n = =1) Case1.solve (); Elsecase2.solve ();}

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.