首页 > 程序开发 > 软件开发 > 其他 >

poj1195——Mobile phones(二维树状数组)

2016-09-14

poj1195——Mobile phones(二维树状数组)

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. fZ喎"https://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vciBhIHRhYmxlIG9mIHNpemUgNCAqIDQsIHdlIGhhdmUgMCAmbHQ7PSBYICZsdDs9IDMgYW5kIDAgJmx0Oz0gWSAmbHQ7PSAzLjwvcD4NCjxwPlRhYmxlIHNpemU6IDEgKiAxICZsdDs9IFMgKiBTICZsdDs9IDEwMjQgKiAxMDI0PC9wPg0KPHA+Q2VsbCB2YWx1ZSBWIGF0IGFueSB0aW1lOiAwICZsdDs9IFYgJmx0Oz0gMzI3Njc8L3A+DQo8cD5VcGRhdGUgYW1vdW50OiAtMzI3NjggJmx0Oz0gQSAmbHQ7PSAzMjc2NzwvcD4NCjxwPk5vIG9mIGluc3RydWN0aW9ucyBpbiBpbnB1dDogMyAmbHQ7PSBVICZsdDs9IDYwMDAyPC9wPg0KPHA+TWF4aW11bSBudW1iZXIgb2YgcGhvbmVzIGluIHRoZSB3aG9sZSB0YWJsZTogTT0gMl4zMDwvcD4NCjxwPk91dHB1dDwvcD4NCjxwPllvdXIgcHJvZ3JhbSBzaG91bGQgbm90IGFuc3dlciBhbnl0aGluZyB0byBsaW5lcyB3aXRoIGFuIGluc3RydWN0aW9uIG90aGVyIHRoYW4gMi4gSWYgdGhlIGluc3RydWN0aW9uIGlzIDIsIHRoZW4geW91ciBwcm9ncmFtIGlzIGV4cGVjdGVkIHRvIGFuc3dlciB0aGUgcXVlcnkgYnkgd3JpdGluZyB0aGUgYW5zd2VyIGFzIGEgc2luZ2xlIGxpbmUgY29udGFpbmluZyBhIHNpbmdsZSBpbnRlZ2VyIHRvIHN0YW5kYXJkIG91dHB1dC48L3A+DQo8cD5TYW1wbGUgSW5wdXQ8L3A+DQo8cD4wIDQ8L3A+DQo8cD4xIDEgMiAzPC9wPg0KPHA+MiAwIDAgMiAyPC9wPg0KPHA+MSAxIDEgMjwvcD4NCjxwPjEgMSAyIC0xPC9wPg0KPHA+MiAxIDEgMiAzPC9wPg0KPHA+MzwvcD4NCjxwPlNhbXBsZSBPdXRwdXQ8L3A+DQo8cD4zPC9wPg0KPHA+NDwvcD4NCjxwPtXitcDM4rrDz/HSssTc08PP37bOyvejrLK7uf3K99e0yv3X6bj8yN3S18DtveKjrLb+zqy1xLLZ1/e6zcbVzai1xLy4uvXSu8Or0rvR+TwvcD4NCjxwcmUgY2xhc3M9"brush:java;"> #include #include #include #include #include #include #include #include #include #define INF 0x3f3f3f3f #define MAXN 2000 #define Mod 10001 using namespace std; int c[MAXN][MAXN],n; int lowbit(int k) { return k&-k; } int read(int x,int y) { int sum=0; for(int i=x;i>0;i-=lowbit(i)) for(int j=y;j>0;j-=lowbit(j)) sum+=c[i][j]; return sum; } void add(int x,int y,int a) { for(int i=x;i<=n;i+=lowbit(i)) for(int j=y;j<=n;j+=lowbit(j)) c[i][j]+=a; } int main() { int op; while(~scanf("%d%d",&op,&n)) { memset(c,0,sizeof(c)); while(1) { scanf("%d",&op); if(op==1) { int x,y,a; scanf("%d%d%d",&x,&y,&a); add(x+1,y+1,a); } if(op==2) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); x1++,y1++,x2++,y2++; printf("%d\n",read(x2,y2)-read(x2,y1-1)-read(x1-1,y2)+read(x1-1,y1-1)); } if(op==3) break; } } return 0; }

相关文章
最新文章
热点推荐