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

题目链接

描述

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 x1xx2, y1yy2.

输入

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 ≤ x1x2 < N, 0 ≤ y1y2 < N

输出

For each Sum operation output a non-negative number denoting the sum modulo 109+7.

----------------------------------------------------------------------------------------------------------------------

破题,non-negative number denoting the sum modulo ,wa了好几次

#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long LL;

using namespace std;
const int N = 1024;
const LL MOD = 1e9+7;

int n,m;
LL sum[N][N];
int lowbit(int data){
    return data&(-data);
}

void add(int x,int y,int d){
    for(int i=x;i<=n;i+=lowbit(i))
    for(int j=y;j<=n;j+=lowbit(j)){
        sum[i][j] += d;
        sum[i][j] %= MOD;
    }
}
LL query(int x,int y){
    LL ret = 0;
    for(int i=x;i>0;i-=lowbit(i))
    for(int j=y;j>0;j-=lowbit(j)){
        ret += sum[i][j];
        ret %= MOD;
    }
    return ret;
}
int main(){
    cin>>n>>m; char str[16];
    memset(sum,0,sizeof(sum));
    int x1,y1,x2,y2,d;
    while(m--){
        scanf("%s",str);
        if(str[0]=='A'){
            scanf("%d%d%d",&x1,&y1,&d);
            add(x1+1,y1+1,d);
        }
        else{
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            LL total = query(x2+1,y2+1);
            LL small = query(x1+0,y1+0);
            LL s1 = query(x2+1,y1+0);
            LL s2 = query(x1+0,y2+1);
            printf("%lld\n",((total+small-s1-s2)%MOD+MOD)%MOD);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/redips-l/p/7668605.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值