Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 18489   Accepted: 8558

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 S表示把S*S区域内的点的值清零;X,Y,A表示将点(X,Y)的值加A;L,B,R,T表示求(L,B)~(R,T)之间的所有的点的值之和
代码:
 //树状数组模板
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int flag,X,Y,A,L,B,R,T,S;
int squ[][];
int lowbit(int x)
{
return x&(-x);
}
void add(int idx,int idy,int val)
{
for(int i=idx;i<=;i+=lowbit(i))
{
for(int j=idy;j<=;j+=lowbit(j))
{
squ[i][j]+=val;
}
}
}
int sum(int idx,int idy)
{
int s=;
for(int i=idx;i>;i-=lowbit(i))
{
for(int j=idy;j>;j-=lowbit(j))
{
s+=squ[i][j];
}
}
return s;
}
int ans(int x1,int y1,int x2,int y2)
{
return (sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-)+sum(x1-,y1-));
}
int main()
{
while(scanf("%d",&flag)&&flag!=)
{
if(flag==)
{
scanf("%d",&S);
memset(squ,,sizeof(squ));
}
else if(flag==)
{
scanf("%d%d%d",&X,&Y,&A);
if(A>=)
add(X+,Y+,A);
else
{
int num=ans(X+,Y+,X+,Y+);
add(X+,Y+,num+A>?A:-num);
}
}
else if(flag==)
{
scanf("%d%d%d%d",&L,&B,&R,&T);
printf("%d\n",ans(L+,B+,R+,T+));
}
}
return ;
}

POJ 1195 二维树状数组的更多相关文章

  1. Mobile phones POJ - 1195 二维树状数组求和

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  2. poj 2029 二维树状数组

    思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...

  3. poj 3378 二维树状数组

    思路:直接用long long 保存会WA.用下高精度加法就行了. #include<map> #include<set> #include<cmath> #inc ...

  4. poj 2155 (二维树状数组 区间修改 求某点值)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33682   Accepted: 12194 Descript ...

  5. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  6. (简单) POJ 1195 Mobile phones,二维树状数组。

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  7. POJ 1195 Mobile phones【二维树状数组】

    <题目链接> 题目大意: 一个由数字构成的大矩阵,开始是全0,能进行两种操作1) 对矩阵里的某个数加上一个整数(可正可负)2) 查询某个子矩阵里所有数字的和要求对每次查询,输出结果 解题分 ...

  8. POJ 1195 Mobile phones (二维树状数组)

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  9. POJ 1195:Mobile phones 二维树状数组

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16893   Accepted: 7789 De ...

随机推荐

  1. HDU 5791 Two DP

    Two   Problem Description   Alice gets two sequences A and B. A easy problem comes. How many pair of ...

  2. loj 1037(状压dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25914 思路:dp[state]表示当前状态下要消耗的最小的sho ...

  3. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  4. aChartEngine图表显示(一页显示多张图表)

    在看本篇的时候,请确认已经看过了 某android平板项目开发笔记----aChartEngine图表显示(1) 不然,有些地方这里就不再说明… 关于XYMutilpleSeriesDataset 一 ...

  5. 解决ScrollView嵌到listView冲突问题

    方法一: 把下面的方法放在绑定适配器操作的下面就行. /** * 重新计算ListView的高度,解决ScrollView和ListView两个View都有滚动的效果,在嵌套使用时起冲突的问题 * @ ...

  6. java 线程演示

    package unit8; public class Mainthread { public static void main(String[] args) { Thread t = new Thr ...

  7. CSS3_loading效果

    写个div给他个基本样式: <body> <div class="load-container load" id="loader" > ...

  8. CF#335 Sorting Railway Cars

    Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. BZOJ4356 : Ceoi2014 Wall

    求出左上角到每个需要保护的点左上角的最短路树,那么最优解一定圈住了它们. 然后将每个点拆成四个点,四个点之间如果没跨越最短路树的树边,那就连0权边. 每个需要保护的点四周4个点都不可通行,求出最短路即 ...

  10. div基础

    1. 写在后面的样式优于前面,会把前面的覆盖掉! 2.三角形的造法:width:0; height:0;然后设置border-left   border-right  border-top  bord ...