• HihoCoder1336 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 y2: 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<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int n,c[1100][1100];
    const int Mod=1e9+7;
    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))
          c[i][j]=(c[i][j]+val)%Mod;    
    }
    int getsum(int x, int y) {
        int res = 0;
    
        for (int i = x; i; i -= lowbit(i)) {
            for (int j = y; j; j -= lowbit(j)) {
                res += c[i][j];
            }
        }
    
        return res;
    }
    int main()
    {
        int m,i,a,b,x,y,z;
        char c[10];
        scanf("%d%d",&n,&m);n++;
        for(i=1;i<=m;i++){
            scanf("%s",c);
            if(c[0]=='A'){
                scanf("%d%d%d",&x,&y,&z);
                add(x+1,y+1,z);
            }
            else {
                scanf("%d%d%d%d",&x,&y,&a,&b);
                printf("%d
    ",((getsum(a+1,b+1)+getsum(x,y)-getsum(a+1,y)-getsum(x,b+1))%Mod+Mod)%Mod);
            }
        }
        return 0;
    }
  • 相关阅读:
    让程序用自定义的菜单自定义菜单AVKON_VIEW,CBA,MENU_BAR,MENU_PANE
    symbian 菜单不显示的原因
    子类中调用父类的带参数的构造函数|子类构造函数调用父类构造函数 的说明
    symbian 设置 透明背景
    IOS App资源路径
    Nonblock I/O 及其使用
    CEikStatusPane MakeVisible kernexec 3错误
    把mapinfo图层的经纬度信息导出来的办法
    解决安装macports,不能更新的问题
    jpg结构解析
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7975419.html
Copyright © 2020-2023  润新知