题目1 : Matrix Sum

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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 ≤ x2y1 ≤ 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周的更多相关文章

  1. 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point

    // 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...

  2. hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)

    来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快.   hi ...

  3. hiho 172周 - 二维树状数组模板题

    题目链接 描述 You are given an N × N matrix. At the beginning every element is 0. Write a program supporti ...

  4. 【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)

    本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的. 原题为hihocoder第77周的题目. 描述 Koch Snowflake i ...

  5. hiho一下 第207周

    题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...

  6. hiho一下第128周 后缀自动机二·重复旋律5

    #1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...

  7. 【hiho一下】第一周 最长回文子串

    题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...

  8. Solution: 最近公共祖先·一 [hiho一下 第十三周]

    题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...

  9. hiho一下十六周 RMQ-ST算法

    RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当 ...

随机推荐

  1. js 获取属性名称,再根据这个属性名获取值

    if (result.success) { var obj = JSON.parse(result.data); var sltObj = document.getElementById(" ...

  2. 微信jssdk图片上传

    一.html页面如下: <div class="weui-cell"> <div class="weui-cell__hd"></ ...

  3. mysql与oracle 表字段定义比较

    链接: https://blog.csdn.net/yzsind/article/details/7948226

  4. 多叉树结构的数据,parent表示法转成children表示法

    最近碰到的问题,有个数组,数组元素是对象,该对象的结构就如树的parent表示法的节点一样.形象点讲就是该数组存放了树的所有“叶子节点”,并且叶子节点内存有父节点,一直到根节点为止,就如存了一条从叶子 ...

  5. 24.基于groovy脚本进行partial update

    主要知识点 在es中其实是有内置的脚本支持的,可以基于groovy脚本实现各种各样的复杂操作 基于groovy脚本,如何执行partial update es scripting module,我们会 ...

  6. 在vue项目中引用element-ui时 让el-input 获取焦点的方法

    在制作项目的时候遇到一个需求,点击一个按钮弹出一个input输入框,并让输入框获得焦点,项目中引用了ElementUI 在网上查找了很多方法,但是在实际使用中发现了一个问题无论是使用$ref获取inp ...

  7. Full-featured Vue 评分组件

    分享一下最近写的 vue 的评分组件 Features: 支持半星.可清除.文案展示.只读.自定义颜色.自定义字符及图片等.支持 hover 的时候改变 value.内置三种样式,以及非常好看 DEM ...

  8. Hibernate事务管理-HibernateTransactionManager-对hibernate session的管理

    由于对SSH还停留在比较初级的应用水平上,今天在遇到一个疑惑时折腾了很久,具体问题是这样的, 有这么一个测试方法, public static void test1() { ApplicationCo ...

  9. Codeforces 919D Substring (拓扑图DP)

    手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo/article/details/81061500 给定一个\(n\ ...

  10. [bzoj3389][Usaco2004Dec]Cleaning Shifts安排值班_最短路

    Cleaning Shifts bzoj-3389 Usaco-2004Dec 题目大意:每天有n个时间段,每个时间段都必须安排一个奶牛值班.有m个奶牛,每个奶牛只有一个空闲时间s[i]~e[i],求 ...