题意:

对一个矩阵有2种操作:

1.把某个元素设为x。

2.查询以(x1,y1)为左上角 以(x2,y2)为右上角的矩阵中的数字的和。

思路:

二维树状数组入门题,同时对横坐标和纵坐标做前缀和就行了。

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = ;
int a[N][N];
int c[N][N];
int n;
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int ch)
{
for (int i = x;i <= n;i += lowbit(i))
{
for (int j = y;j <= n;j += lowbit(j))
{
c[i][j] += ch;
}
}
}
int getsum(int x,int y)
{
int ans = ;
for (int i = x;i > ;i -= lowbit(i))
{
for (int j = y;j > ;j -= lowbit(j))
{
ans += c[i][j];
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
memset(c,,sizeof(c));
memset(a,,sizeof(a));
scanf("%d",&n);
char s[];
while (scanf("%s",s) != EOF)
{
if (s[] == 'N') break;
if (s[] == 'E')
{
int x,y,num;
scanf("%d%d%d",&x,&y,&num);
x++,y++;
int ch = num - a[x][y];
a[x][y] = num;
add(x,y,ch);
}
if (s[] == 'U')
{
int x,y,p,q;
scanf("%d%d%d%d",&x,&y,&p,&q);
x++,y++,p++,q++;
int ans = ;
ans += getsum(p,q);
ans -= getsum(x-,q);
ans -= getsum(p,y-);
ans += getsum(x-,y-);
printf("%d\n",ans);
}
}
}
return ;
}

spoj 1029 Matrix Summation的更多相关文章

  1. SPOJ 1029 Matrix Summation【 二维树状数组 】

    题意:二维树状数组,更改值的时候有一点不一样, 是将a[x][y]设置为一个值,所以add的时候要将它和以前的值作差一下 #include<iostream> #include<cs ...

  2. SPOJ 74. Divisor Summation 分解数字的因子

    本题有两个难点: 1 大量的数据输入.没处理好就超时 - 这里使用buffer解决 2 因子分解的算法 a)暴力法超时 b)使用sieve(筛子),只是当中的算法逻辑也挺不easy搞对的. 数值N因子 ...

  3. SPOJ DIVSUM - Divisor Summation

    DIVSUM - Divisor Summation #number-theory Given a natural number n (1 <= n <= 500000), please ...

  4. SPOJ - MATSUM Matrix Summation---二维树状数组

    题目链接: https://vjudge.net/problem/SPOJ-MATSUM 题目大意: 二维数组,两种操作 SET 将某点设置成x SUM 求某个区域之和 解题思路: 这里用二维树状数组 ...

  5. greenplum 数组操作

    参考:http://gpdb.docs.pivotal.io/4390/admin_guide/query/topics/functions-operators.html Table 4. Advan ...

  6. SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)

    题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...

  7. SPOJ 375. Query on a tree (树链剖分)

    Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Ori ...

  8. SPOJ #440. The Turtle´s Shortest Path

    Coding a Dijkstra is not hard. %70 of my time spent on tackling TLE, as my last post. Dijkstra works ...

  9. spoj 1676 AC自动机+矩阵快速

    Text Generator Time Limit: 1386MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submi ...

随机推荐

  1. Android的Launcher启动流程 “Launcher部分启动流程”

    研究代码从:AndroidManifest.xml.自定义的Application.java开始. Android系统启动时,系统需要一个Home应用程序来负责将这些应用程序展示出来:也就是该应用的目 ...

  2. asp 获取url 返回值 和 对json 返回值的处理

    Function GetHttpPage(HttpUrl,endoce) If endoce = "" Then endoce = "GB2312" If Is ...

  3. java集合的复习

    1:自定义的linkedList链表 https://blog.csdn.net/qq_33471403/article/details/80109620 2:用linked    https://b ...

  4. 最大子矩阵和问题dp

    给定一个矩阵 matrix,其中矩阵中的元素可以包含正数.负数.和0,返回子矩阵的最大累加和.例如,矩阵 matrix 为: 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 - ...

  5. javaScript---RegExp

    字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...

  6. ACM常用STL

    转载于https://blog.csdn.net/riba2534/article/details/61929000 1.  stack stack<int>st;//栈st,用于存放in ...

  7. JS-原型的某些概念

    prototype:构造函数拥有一个对象,称为构造函数的原型属性,可以通过 构造函数prototype进行访问. __proto__: 构造函数所创造出的实例对象,可通过该属性访问原型对象. cons ...

  8. 使用Java Low Level REST Client操作elasticsearch

    Java REST客户端有两种风格: Java低级别REST客户端(Java Low Level REST Client,以后都简称低级客户端算了,难得码字):Elasticsearch的官方low- ...

  9. jsp页面第一句话报这个错Syntax error, insert "}" to complete

    前段时间做了个项目jsp页面出现了Syntax error, insert "}" to complete错误,是说少了个“}”花括号,错误是在第一行,删了第一行还是会有,错误也不 ...

  10. node 学习(一)

    什么是 Node.js Node.js就是运行在服务端的JavaScript. Node.js是一个基于Chrome JavaScript运行时建立的一个平台. Node.js是一个事件驱动 I/O ...