hiho一下 第172周
题目1 : Matrix Sum
描述
You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 operations:
1. Add x y value: Add value to the element Axy. (Subscripts starts from 0
2. Sum x1 y1 x2 y1: Return the sum of every element Axy for x1 ≤ x ≤ x2, y1 ≤ y ≤ y2.
输入
The first line contains 2 integers N and M, the size of the matrix and the number of operations.
Each of the following M line contains an operation.
1 ≤ N ≤ 1000, 1 ≤ M ≤ 100000
For each Add operation: 0 ≤ x < N, 0 ≤ y < N, -1000000 ≤ value ≤ 1000000
For each Sum operation: 0 ≤ x1 ≤ x2 < N, 0 ≤ y1 ≤ y2 < N
输出
For each Sum operation output a non-negative number denoting the sum modulo 109+7.
- 样例输入
-
5 8
Add 0 0 1
Sum 0 0 1 1
Add 1 1 1
Sum 0 0 1 1
Add 2 2 1
Add 3 3 1
Add 4 4 -1
Sum 0 0 4 4 - 样例输出
-
1
2
3
肯定是二维线段树,虽然思路明白,但是不会写,先用树状数组吧。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int N, M, x1, x2, y1, y2, v;
long long BIT2[][];
char str[];
int lowbit(int x) {
return x & (-x);
}
void add(int x, int y, int val) {
for (int i = x; i <= N; i += lowbit(i)) {
for (int j = y; j <= N; j += lowbit(j)) {
BIT2[i][j] += val;
BIT2[i][j] %= ;
}
}
}
long long sum(int x, int y) {
long long ret = ;
for (int i = x; i > ; i -= lowbit(i)) {
for (int j = y; j > ; j -= lowbit(j)) {
ret += BIT2[i][j];
ret %= ;
}
}
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%d%d", &N, &M);
memset(BIT2, , sizeof(BIT2));
for (int i = ; i < M; i++) {
scanf("%s", str);
if (strcmp(str, "Add") == ) {
scanf("%d%d%d", &x1, &y1, &v);
x1++, y1++;
add(x1, y1, v);
} else {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x2++, y2++;
long long ans = sum(x2, y2);
ans = ans - sum(x1, y2) - sum(x2, y1) + sum(x1, y1);
while (ans < ) {
ans += ;
}
printf("%lld\n", ans);
}
}
return ;
}
hiho一下 第172周的更多相关文章
- 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point
// 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...
- hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)
来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快. hi ...
- hiho 172周 - 二维树状数组模板题
题目链接 描述 You are given an N × N matrix. At the beginning every element is 0. Write a program supporti ...
- 【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)
本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的. 原题为hihocoder第77周的题目. 描述 Koch Snowflake i ...
- hiho一下 第207周
题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...
- hiho一下第128周 后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- 【hiho一下】第一周 最长回文子串
题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...
- Solution: 最近公共祖先·一 [hiho一下 第十三周]
题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...
- hiho一下十六周 RMQ-ST算法
RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当 ...
随机推荐
- swift里 as、as!、as?区别 T.Type与动态类型
as 1.编译器进行类型转换合法性检查:静态 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: shoppingLi ...
- PAT_A1125#Chain the Ropes
Source: PAT A1125 Chain the Ropes (25 分) Description: Given some segments of rope, you are supposed ...
- PAT_A1111#Online Map
Source: PAT A1111 Online Map (30 分) Description: Input our current position and a destination, an on ...
- eoLinker GoKu Gateway 开源版 V2.1发布,加入UI管理系统等
GoKu API Gateway 是eoLinker旗下的开源版接口网关,支持OpenAPI与微服务管理,支持私有云部署,实现API转发.请求参数转换.数据校验等功能,提供图形化界面管理,能够快速管理 ...
- SQL中的条件判断语句(case when zhen if,ifnull)用法
简介: case具有两种格式.简单case函数和case搜索函数.这两种方式,可以实现相同的功能.简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式.还有 ...
- BZOJ 1179 抢掠计划atm (缩点+有向无环图DP)
手动博客搬家: 本文发表于20170716 10:58:18, 原地址https://blog.csdn.net/suncongbo/article/details/81061601 https:// ...
- cxdbtreelist的按记录查找节点
lst_projet.DataController.DataSet.Locate('pm_id',vPm_ID,[]); bl:= lst_projet.DataController.DataSet. ...
- 0926mysql中MRR的用法
转自 http://blog.itpub.net/22664653/viewspace-1673682 [MySQL]MySQL5.6新特性之Multi-Range Read 2015-05-27 ...
- java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z解决办法
2019-05-20 23:02:20.168 |-INFO [http-nio-8001-exec-2] com.xxx.ecc.cloudbiz.service.payment.impl.Wei ...
- [React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3
getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement ...