zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    MKMapVIew学习系列2 在地图上绘制出你运行的轨迹
    WPF SDK研究 Intro(6) WordGame1
    WPF SDK研究 Intro(3) QuickStart3
    WPF SDK研究 Layout(1) Grid
    WPF SDK研究 目录 前言
    WPF SDK研究 Intro(7) WordGame2
    WPF SDK研究 Layout(2) GridComplex
    对vs2005创建的WPF模板分析
    WPF SDK研究 Intro(4) QuickStart4
    《Programming WPF》翻译 第6章 资源
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7975419.html
Copyright © 2011-2022 走看看