spoj 1029 Matrix Summation
题意:
对一个矩阵有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的更多相关文章
- SPOJ 1029 Matrix Summation【 二维树状数组 】
题意:二维树状数组,更改值的时候有一点不一样, 是将a[x][y]设置为一个值,所以add的时候要将它和以前的值作差一下 #include<iostream> #include<cs ...
- SPOJ 74. Divisor Summation 分解数字的因子
本题有两个难点: 1 大量的数据输入.没处理好就超时 - 这里使用buffer解决 2 因子分解的算法 a)暴力法超时 b)使用sieve(筛子),只是当中的算法逻辑也挺不easy搞对的. 数值N因子 ...
- SPOJ DIVSUM - Divisor Summation
DIVSUM - Divisor Summation #number-theory Given a natural number n (1 <= n <= 500000), please ...
- SPOJ - MATSUM Matrix Summation---二维树状数组
题目链接: https://vjudge.net/problem/SPOJ-MATSUM 题目大意: 二维数组,两种操作 SET 将某点设置成x SUM 求某个区域之和 解题思路: 这里用二维树状数组 ...
- greenplum 数组操作
参考:http://gpdb.docs.pivotal.io/4390/admin_guide/query/topics/functions-operators.html Table 4. Advan ...
- SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)
题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...
- SPOJ 375. Query on a tree (树链剖分)
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- 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 ...
- spoj 1676 AC自动机+矩阵快速
Text Generator Time Limit: 1386MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submi ...
随机推荐
- Windows下利用MKL加速caffe,与openblas比较
一.介绍:先简单Mark一下网上的介绍资料,弄清楚MKL是个啥,已经与openblas等的关系. 矩阵运算库blas, cblas, openblas, atlas, lapack, mkl之间有什么 ...
- TypeError: 'module' object is not callable
pkuseg.py 内容如下: import pkusegseg = pkuseg.pkuseg()text = seg.cut('我爱北京天安门')print(text) 原因是py文件名于包名一样 ...
- __x__(32)0908第五天__Photoshop的基本操作
1. 设置 Photoshop 的单位为 像素px 2. 标尺 显示与隐藏 Ctrl + r 3. 放大与缩小 Ctrl + 1 放大到100% Ctrl + 0 适应屏幕 Alt + ...
- webservice接口测试wsdl,参数是xml格式。python,入参转化成str,返回值转化成dict调用
1.用SoapUI测试webservice接口,传入参数是xml格式时.xml格式需要将xml的外围增加<![CDATA[xml]]> 2.但是用python去做webservice测试, ...
- Python全栈-magedu-2018-笔记7
第三章 - Python 内置数据结构 线性结构 线性结构 可迭代 for ... in len()可以获取长度 通过下标可以访问 可以切片 学过的线性结构 列表.元组.字符串.bytes.bytea ...
- Integer、new Integer() 和 int 比较的面试题
基本概念的区分: 1.Integer 是 int 的包装类,int 则是 java 的一种基本数据类型2.Integer 变量必须实例化后才能使用,而int变量不需要3.Integer 实际是对象的引 ...
- 长时间关机测试脚本.VBS
Sub Main Dim cnt Dim delay Dim time Dim atttime atttime = 20 delay = 3000 time = 50 cnt_time=3 crt.s ...
- direction: rtl;
这个属性,有点无语,费了点时间. <style type="text/css"> .hao {direction: rtl;}</style> <se ...
- sql进阶
--1.变量的声明和赋值 declare @Start DateTime, @End DateTime set @Start = '1999-9-9' set @End = '2000-1-1' ...
- centos7开启80和8080端口
开启8080端口 firewall-cmd --permanent --add-port=8080/tcp firewall-cmd --reload 重定向80端口到8080端口firewall-c ...