zoukankan      html  css  js  c++  java
  • poj1195

    二维树状数组

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    using namespace std;

    #define maxn 1030

    int c[maxn][maxn];
    int Row, Col, n;

    inline
    int Lowbit(const int &x)
    {
    return x & (-x);
    }

    int Sum(int i, int j)
    {
    int tempj, sum = 0;
    while (i > 0)
    {
    tempj
    = j;
    while (tempj > 0)
    {
    sum
    += c[i][tempj];
    tempj
    -= Lowbit(tempj);
    }
    i
    -= Lowbit(i);
    }
    return sum;
    }

    void Update(int i, int j, int num)
    {
    int tempj;
    while (i <= Row)
    {
    tempj
    = j;
    while (tempj <= Col)
    {
    c[i][tempj]
    += num;
    tempj
    += Lowbit(tempj);
    }
    i
    += Lowbit(i);
    }
    }

    int cal(int l, int b, int r, int t)
    {
    return Sum(r, t) - Sum(l, t) - Sum(r, b) + Sum(l, b);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int order, x, y, a, l, r, b, t;
    while (scanf("%d", &order), order != 3)
    {
    if (order == 0)
    {
    scanf(
    "%d", &n);
    Col
    = Row = n + 2;
    memset(c,
    0, sizeof(c));
    }
    else if (order == 1)
    {
    scanf(
    "%d%d%d", &x, &y, &a);
    Update(x
    + 2, y + 2, a);
    }
    else if (order == 2)
    {
    scanf(
    "%d%d%d%d", &l, &b, &r, &t);
    printf(
    "%d\n", cal(l + 1, b + 1, r + 2, t + 2));
    }
    }
    return 0;
    }
  • 相关阅读:
    Python 的with关键字
    java解析xml
    Java IO & Serialization
    Java动态编译
    爬虫下载City Scape数据
    Pytorch多GPU训练
    可视化利器Visdom
    GLOG使用Demo
    hyperopt自动调参
    [Redis源码阅读]redis持久化
  • 原文地址:https://www.cnblogs.com/rainydays/p/2160283.html
Copyright © 2011-2022 走看看