Mobile phones poj1195(二维树状数组)

Mobile phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 24208 Accepted: 11127

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

题目大意:给你一个s*s的矩阵要你完成4个操作,0:初始化,1 X Y A: 将矩阵[x][y]修改成A,2 L B R T:将矩阵[L][B]-[R][T]的和输出来,4退出。
思路:
这题对时间复杂度要求比较严格,O(n^2)的暴力,直接T掉,这题需要采用二维树状数组去维护。代码比较简单,理解确是比较难的我的理解是每行每列都是一个一维的树状数组。求和公式getsum(x2,y2)+getsum(x1-1,y1-1)-getsum(x2,y1-1)-getsum(x1-1,y2);这个在本子上稍微画画应该还是能明白的。还剩两个题,刷完就准备复习期末考试了,加油!
代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

const int maxn=1e3+10;
int sum[maxn*2][maxn*2];
int s;
void init(){
	scanf("%d",&s);
	memset(sum,0,sizeof(sum));
}

void add(int x,int y,int v){
	for(;x<=s+1;x+=(x&-x)){
		for(int j=y;j<=s+1;j+=(j&-j)){
			sum[x][j]+=v;
		}
	}
}
int getsum(int x,int y){
	int s=0;
	for(;x;x-=(x&-x)){
		for(int j=y;j;j-=(j&-j)){
			s+=sum[x][j];
		}
	}
	return s;
}
int query(int x1,int y1,int x2,int y2){
	return getsum(x2,y2)+getsum(x1-1,y1-1)-getsum(x2,y1-1)-getsum(x1-1,y2);
}
int main(){
	int ins;
	while(scanf("%d",&ins)&&ins!=3){
		int x,y,a,b,v;
		switch(ins){
			case 0:init();break;
			case 1:scanf("%d%d%d",&x,&y,&v);add(x+1,y+1,v);break;
			case 2:scanf("%d%d%d%d",&x,&y,&a,&b);printf("%d\n",query(x+1,y+1,a+1,b+1));break;
		}
	}
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 15:37
1、这群人晚上&nbsp;11&nbsp;点发朋友圈:"凌晨&nbsp;11&nbsp;点,三环的灯还亮着。"&nbsp;实际下班时间:19:30。2、什么是嘉豪呀?我最近在字节实习,没什么时间上网3、同龄人:学校社团、酒吧蹦迪;我:acm、字节/腾讯实习4、别人朋友圈发:“今天不想上课”;我朋友圈发:“今天的班就上到这里啦”,定位:字节跳动5、别人的朋友圈都是到处旅游的定位,我的朋友圈天天都是“字节定位”,还一定要是在【公司的健身房】里拍张照片,实际只练了10分钟,其中凹造型5分钟6、mentor布置任务的时候,别人都是:”好的收到“,我:”是不是要xxxx,xxxx这么做也可以吧,这个技术方案会不会更好些“7、别人书包里装的:王道408、轻薄本、四六级真题。我书包里面装的:显存24GB4090独显gpu(24小时开机运行,屏幕上贴着“字节/腾讯等贴纸”)、速效救心丸(代码报错用)、电棍(熬夜写代码困了用),就很……你们懂吧8、入职大厂第一件事:发朋友圈、发小红书,晒工牌,985计算机硕|字节实习生|可以接咨询|有偿改简历,9、别人的社交软件简介:25岁|男|希望遇见有趣的灵魂;嘉豪的社交软件简介:25岁|程序员|字节跳动工程师|一张佩戴工牌的自拍照大厂嘉豪标配:1.&nbsp;挂胸前的工牌(地铁里只挂不收,怕你看不见&nbsp;logo)2.&nbsp;降噪耳机(不放音乐也戴着,避免别人跟自己说话)3.&nbsp;印&nbsp;logo&nbsp;的电脑包(字节红&nbsp;/&nbsp;腾讯蓝&nbsp;/&nbsp;阿里橙&nbsp;/&nbsp;美团黄)4.&nbsp;手表(最好显示心率,午饭后必发"步数已破&nbsp;6,000")
牛客30247842...:因为不好进啊要是大厂随便进哪来这么多人装逼
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务