Vijos P1512--superbrother hit Mole (tree-like Array) (review)

Source: Internet
Author: User

Background

Superbrother in the computer room idle nothing to do (compare his noip, it is ironic ah ...), so bored to start playing "hit the Mole" ...

Describe

In this "hit the Mole" game, the mole will occasionally drill out of the hole, but will not get into the hole (mole really bold ...) )。 The openings are in a square of size n (n<=1024). This square is in a planar Cartesian coordinate system, the lower left corner is (0,0), and the upper-right corner is (n-1,n-1). The location of the hole is the whole point, that is, the horizontal ordinate is an integer. And Superbrother will occasionally want to know the total number of moles in a certain range. This is your mission.

Format input Format

There are multiple lines for each input file.

The first line, a number n, represents the mole's range.

Each subsequent line has a number m at the beginning, indicating a different operation: M=1, followed by 3 number x,y,k (0<=x,y<n), indicating a new K mole at the point (x, y), m=2, then followed by 4 numbers x1,y1,x2,y2 (0<=x1 <=x2<n,0<=y1<=y2<n), indicating the number of moles in the inquiry rectangle (x1,y1)-(x2,y2); m=3, it means the teacher is coming, not playing. Make sure the number is in the last line of the input.

The number of inquiries will not exceed 10000, and the number of moles will not exceed maxlongint.

Output format

For each m=2, the output line number is one number, which is the number of moles in the area being queried.

Example 1 Sample Input 1
41 2 2 52 0 0 2 33
Sample Output 1
5
Limit

Each test point 1s

A two-dimensional tree array problem, a two-dimensional array that saves the beginning to the i,j of the mole. The starting point here is because it can be 0,0, so add 1 to the horizontal ordinate after the input. At the time of the query, it is also the 2-layer for loop that finishes the entire range. since there is no maximum value for the tree-like array, we will summarize the method of finding the maximum value of the tree array tomorrow.

Wish oneself Noip Smooth ~

1#include <bits/stdc++.h>2 using namespacestd;3 Const intmaxn=1030;4 intA[MAXN][MAXN];5 intn,m;6 intLowbit (intx)7 {8     returnx& (-x);9 }Ten voidAddintXintYintW) One { A      for(inti=x;i<=n;i+=lowbit (i)) -      for(intj=y;j<=n;j+=Lowbit (j)) -a[i][j]+=W; the     return ; - } - intSumintXinty) - { +     intans=0; -      for(inti=x;i>=1; i-=lowbit (i)) +      for(intj=y;j>=1; j-=Lowbit (j)) Aans+=A[i][j]; at     returnans; - } - intMain () - { -scanf"%d",&n); -      while(1) in     { -         intK; toscanf"%d",&k); +         if(k==3) Break; -         if(k==2) the         { *             intx1,x2,x3,x4; $scanf"%d%d%d%d",&x1,&x2,&x3,&x4);Panax Notoginsengx1++;x2++;x3++;x4++;//remove points on an axis -printf"%d\n", SUM (x3,x4)-sum (x1-1, x4)-sum (x3,x2-1) +sum (x1-1, x2-1));//Rong denounced the             Continue;  +         } A         if(k==1) the         { +             intx1,x2,w; -scanf"%d%d%d",&x1,&x2,&W); $x1++;x2++; $ Add (x1,x2,w); -         } -          the     } -     return 0;Wuyi}

Vijos P1512--superbrother hit Mole (tree-like Array) (review)

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.