题意:

对一个矩阵有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. S0.2 灰度图

    目录 灰度图定义 灰度图优点 RGB转灰度算法(OpenCV3) 量化 算法公式 OpenCV自带函数实现 综合比较 灰度图定义 对于单色(灰度)图像而言,每个像素的亮度用一个数值来表示,通常数值范围 ...

  2. 09-Http & Servlet

    Http协议&Servlet Http协议 什么是协议 > 双方在交互.通讯的时候, 遵守的一种规范.规则. http协议 > 针对网络上的客户端 与 服务器端在执行http请求的 ...

  3. jquery for循环判断是否重复

    //使用for循环 判断是否有重名 var len=$("li").length;//获取页面中所有li的数量 for(var i=0; i<len; i++){ oldna ...

  4. Module(CP343-1)Advanced system error SDB generation Error in rule file or rule file not found

    报这个故障的原因是因为安装目录下缺少三个 err文件.只要从好的目录下拷贝过来即可. C:\Program Files (x86)\SIEMENS\Step7\s7wbx\rul

  5. MAC下 mySQL及workbench安装

    1.首先去mysql 网站下载安装包,https://dev.mysql.com/downloads/mysql/ 选择第一个.dmg格式的下载安装 2.安装完成后,在System Preferenc ...

  6. nginx/php的redis模块扩展

    redis模块介绍 redis2-nginx-module 可以实现 Nginx 以非阻塞方式直接防问远方的 Redis 服务,可以启用强大的 Redis 连接池功能,进而实现更多的连接与更快速的访问 ...

  7. oracle统计数据时,涉及两个表的数据

    SELECT t1.*,a.num FROM (SELECT SUM(t.total_profit) total_profit, SUM(t.main_business_income) main_bu ...

  8. JDK8之The type java.util.Map$Entry cannot be resolved

    eclipse+tomcat7+jdk1.6上面报错的方式我的解法方法是吧jre8换成6的就好了选中项目->右键->java build path ->找到jre system li ...

  9. C博客作业01--分支,顺序结构

    本章学习总结(2分) 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 本周初次接触C语言,一开始难度较大,很多代码都看不懂,书里面的章节要看很多遍.开始编写代码时也遇到很多困难 ...

  10. Java 非静态内部类中可以定义静态变量或方法吗?

    如图: 这个问题的答案是不可以 由于内部类的实例化是由外部类实例化之后加载的,如果外部类还没有实例化,这时候调用内部类的静态成员,此时内部类还没有被加载,却要开始创建静态成员,这是矛盾的,所以java ...