vijos-1512 Superbrother hit Mole (two-dimensional tree array)

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" ... Description

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. 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, then followed by 3 number x,y,k (0<=x,y output format

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

4
1 2 2 5
2 0 0 2 3
3
Sample Output 1
5
Ideas

About a two-dimensional tree array: dead-night Writing Algorithm (3): Tree-like array
This is a template for a two-dimensional tree array, here is my template: N: Size of the rectangle update (X,Y,V) to the coordinates (x, y) of the element plus the value of v int sum (int x1,int y1,int x2,int y2), query the matrix within the range of elements and queries The function return value (x, y) represents the sum of the number of matrices from (–>) (x, Y) to be aware that if 0 appears in the coordinates to be queried, give the coordinate +1 code

#include <bits/stdc++.h> using namespace std;
#define MEM (A, B) memset (A,b,sizeof (a)) const int n=1030+7;
int n;
int c[n][n]; inline int lowbit (int t) {return t& (-t),} void update (int x,int Y,int v) {for (int i=x; i<=n; i+=lowbit (
i)) for (int j=y; j<=n; j+=lowbit (j)) C[i][j]+=v;
    } int query (int x,int y) {int s=0;
    for (int i=x, i>0; i-=lowbit (i)) for (int j=y; j>0; j-=lowbit (j)) S+=c[i][j];
return s;
    } int sum (int x1,int y1,int x2,int y2) {x1--, y1--;
return query (x2,y2)-query (x2,y1)-query (x1,y2) +query (x1,y1);
    } int main () {int m,x1,y1,x2,y2,k,x,y;
    scanf ("%d", &n); while (scanf ("%d", &m) && (m!=3)) {if (m==1) {scanf ("%d%d%d", &x,&y,&k
            );
            x++,y++;
        Update (X,Y,K);
            } else if (m==2) {scanf ("%d%d%d%d", &x1,&y1,&x2,&y2);
         x1++,y1++,x2++,y2++;   printf ("%d\n", Sum (x1,y1,x2,y2));
}} return 0;
 }

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.