HDOJ 4456 Crowd discretization + two-dimensional tree Array

Source: Internet
Author: User

HDOJ 4456 Crowd discretization + two-dimensional tree Array

 

Rotate the coordinates 45 degrees to get a square. You can use a two-dimensional tree array to solve the problem...

To save memory, save all vertices updated in the tree array in advance and discretization

 

 

Crowd Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1199 Accepted Submission (s): 282



Problem Description City F in the southern China is preparing lanterns festival celebration along the streets to celebrate the festival.
Since frequent accidents had happened last year when the citizens went out to admire the colorful lanterns, City F is planning to develop a system to calculate the degree of congestion of the intersection of two streets.
The map of City F is organized in an N × N grid (N north-south streets and N west-east street ). for each intersection of streets, we define a density value for the crowd on the intersection.
Initially, the density value of every intersection is zero. as time goes by, the density values may change frequently. A set of cameras with new graphical recognition technology can calculate the density value of the intersection easily in a short time.
But the administrator of the police office is planning to develop a system to calculate the degree of congestion. for some consideration, they come up with a conception called k-dimension congestion degree. the k-dimension congestion degree of intersection (x0, y0) is represented as c (x0, y0, k), and it can be calculated by the formula below:

Here, d (x, y) stands for the density value on intersection (x, y) and (x, y) must be in the N x N grid. the formula means that all the intersections in the range of manhattan distance k from (x0, y0) effect the k-dimension congestion degree of (x0, y0) equally, so we just simply sum them up to get the k-dimension congestion degree of (x0, y0 ).
The figure below shows a 7 × 7 grid, and it shows that if you want to get the 2-dimension congestion degree of intersection ), you shoshould sum up the density values of all marked intersections.


Input These are multiple test cases.
Each test case begins with a line with two integers N, M, meaning that the city is an N × N grid and there will be M queries or events as time goes. (1 ≤ N ≤ 10 000, 1 ≤ M ≤ 80 000) Then M lines follow. each line indicates a query or an event which is given in form of (p, x, y, z), here p = 1 or 2, 1 ≤ x ≤ N, 1 ≤ y ≤ N.
The meaning of different p is shown below.
1. p = 1 the value of d (x, y) is increased by z, here-100 ≤ z ≤ 100.
2. p = 2 query the value of c (x, y, z), here 0 ≤ z ≤ 2N-1.
Input is terminated by N = 0.

Output For each query, output the value for c (x, y, z) in a line.

Sample Input
8 51 8 8 11 1 1 -22 5 5 61 5 5 32 2 3 93 21 3 2 -92 3 2 00

Sample Output
11-9

Source 2012 Asia Hangzhou Regional Contest

 

 

 

/*************************************** * ******** Author: CKbossCreated Time:, Monday, January 1, August 31, 2015 File Name: HDOJ4456.cpp *************************************** * *********/# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          Using namespace std; const int maxn = 88888; const int maxm = 4004000; int tree [maxm]; int hs [maxm], hn; int n, m; int w; int p [maxn], x [maxn], y [maxn], d [maxn]; inline int lowbit (int x) {return x & (-x );} void ready (int x, int y) {for (int I = x; I <= w; I + = lowbit (I) {for (int j = y; j <= w; j + = lowbit (j) {hs [hn ++] = I * w + j ;}} void Add (int x, int y, int v) {for (int I = x; I <= w; I + = lowbit (I) {for (int j = y; j <= w; j + = lowbit (j) {int id = lower_bound ( Hs, hs + hn, I * w + j)-hs; tree [id] + = v ;}} int Sum (int x, int y) {x = max (x, 0); x = min (x, w); y = max (y, 0); y = min (y, w ); int ret = 0; for (int I = x; I> 0; I-= lowbit (I) {for (int j = y; j> 0; j-= lowbit (j) {int id = lower_bound (hs, hs + hn, I * w + j)-hs; if (hs [id] = I * w + j) ret + = tree [id];} return ret;} int main () {// freopen(in.txt, r, stdin); // freopen(out.txt, w, stdout); while (scanf (% d, & n )! = EOF & n) {scanf (% d, & m); w = 2 * n; hn = 0; memset (tree, 0, sizeof (tree )); for (int I = 0; I
           
            

 

 

 

Related Article

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.