码迷,mamicode.com
首页 > 编程语言 > 详细

POJ-1195 Mobile Phones(二维树状数组)

时间:2016-08-02 01:13:01      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 18187   Accepted: 8401

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 
技术分享

The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 4
1 1 2 3
2 0 0 2 2 
1 1 1 2
1 1 2 -1
2 1 1 2 3 
3

Sample Output

3
4

Source

 

明天不编程,今天多做几道补上~

题目大意:给一个矩阵,初始全赋0,

                1,a,b,c表示f[a][b]+=c;

                2,a,b,c,d表示输出Σf[i][j] (i=a…c , j=b…d)

本蒟蒻刚学树状数组就给我来个二维树状数组的题真的好吗……= =

注意:他给的矩阵操作是从下标0开始的,所以每次要将x++,y++;

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 typedef long long LL;
12 const int MAX=1050;
13 int n;
14 int f[MAX][MAX];
15 void add(int x,int y,int z){
16     int i,j;
17     for (i=x;i<MAX;i+=(i&-i))
18      for (j=y;j<MAX;j+=(j&-j))
19       f[i][j]+=z;
20 }
21 int search(int x,int y){
22     int ans(0);
23     int i,j;
24     for (i=x;i>0;i-=(i&-i))
25      for (j=y;j>0;j-=(j&-j))
26       ans+=f[i][j];
27     return ans;
28 }
29 int main(){
30     freopen ("mobile.in","r",stdin);
31     freopen ("mobile.out","w",stdout);
32     int i,j;
33     int a,b,c,d,e;
34     memset(f,0,sizeof(f));
35     scanf("%d%d",&i,&n);
36     while (1)
37     {scanf("%d",&a);
38      if (a==3)
39       break;
40      if (a==1)
41      {scanf("%d%d%d",&b,&c,&d);
42       b++, c++;
43       add(b,c,d);
44      }
45      if (a==2)
46      {scanf("%d%d%d%d",&b,&c,&d,&e);
47       b++, c++, d++, e++;
48       printf("%d\n",search(d,e)-search(b-1,e)-search(d,c-1)+search(b-1,c-1));
49      }
50     }
51     return 0;
52 }

 

POJ-1195 Mobile Phones(二维树状数组)

标签:

原文地址:http://www.cnblogs.com/Michaelzzn/p/5727713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!