前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小码匠的信息学江湖:【模板】二维树状数组 2:区间修改,单点查询

小码匠的信息学江湖:【模板】二维树状数组 2:区间修改,单点查询

作者头像
小码匠
发布2023-09-19 08:20:12
2850
发布2023-09-19 08:20:12
举报
文章被收录于专栏:小码匠和老码农

本系列是模版系列,会整理

  • 题目链接地址
  • 参考资料
  • AC代码
  • 自己挖坑(部分题目有)

关于思路大家可参照题目信息的链接地址或者相关书籍,文章旨在分享代码。

题目信息

  • https://loj.ac/p/134

参考资料

  • 树状数组
    • https://oi-wiki.org/ds/fenwick/

题目描述

这是一道模板题。

给出一个

n\times m

的零矩阵 A,你需要完成如下操作:

  • 1 x y k:表示元素 A_{x,y} 自增 k;
  • 2 a b c d:表示询问左上角为 (a,b),右下角为 (c,d) 的子矩阵内所有数的和。
输入格式

输入的第一行有两个正整数 n, m;接下来若干行,每行一个操作,直到文件结束。

输出格式

对于每个 2 操作,输出一个整数,表示对于这个操作的回答。

样例

输入复制

代码语言:javascript
复制
2 2
1 1 1 3
1 2 2 4
2 1 1 2 2

输出复制

代码语言:javascript
复制
7
数据范围与提示
  • 对于 10% 的数据,n=1;
  • 对于另 10% 的数据,m=1;
  • 对于全部数据,
1\le n,m\le 2^{12},1\le x,a,c\le n,1\le y,b,d\le m,|k|\le 10^5

,保证操作数目不超过

3\times 10^5

,且询问的子矩阵存在。

AC代码

代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>

using namespace std;
#define endl '\n';
typedef long long LL;

struct node {
    LL w, d;
};

struct FenwickTree {
    vector<vector<LL>> c;
    int n, m;

    FenwickTree(int l, int m) :
            n(l), c(l + 1, vector<LL>(m + 1)), m(m) {}

    int lowbit(int a) {
        return (-a) & a;
    }

    LL cnt(int x, int y) {
        LL ans = 0;
        for (int i = x; i > 0; i -= lowbit(i)) {
            for (int j = y; j > 0; j -= lowbit(j)) {
                ans += c[i][j];
            }
        }
        return ans;
    }

    void add(int x, int y, LL w) {
        for (int i = x; i <= n; i += lowbit(i)) {
            for (int j = y; j <= m; j += lowbit(j)) {
                c[i][j] += w;
            }
        }
    }

    LL ask(int x1, int y1, int x2, int y2) {
        return cnt(x2, y2) - cnt(x2, y1 - 1) - cnt(x1 - 1, y2) + cnt(x1 - 1, y1 - 1);
    }
};

void best_coder() {
    int n, m;
    scanf("%d%d", &n, &m);
    FenwickTree ft(n, m);
    int a;
    while (~scanf("%d", &a)) {
        if (a == 1) {
            int x1, y1, x2, y2;
            LL w;
            scanf("%d%d%d%d%lld", &x1, &y1, &x2, &y2, &w);
            ft.add(x1, y1, w);
            ft.add(x1, y2 + 1, -w);
            ft.add(x2 + 1, y1, -w);
            ft.add(x2 + 1, y2 + 1, w);
        } else {
            int x, y;
            scanf("%d%d", &x, &y);
            printf("%lld\n", ft.cnt(x, y));
        }
    }
}

void happy_coder() {

}

int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // freopen("xxx.in", "r", stdin);
    // freopen("xxx.out", "w", stdout);

    // 小码匠
    best_coder();

    // 最优解
    // happy_coder();

    // fclose(stdin);
    // fclose(stdout);

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-09-10 06:00,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小码匠和老码农 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目信息
  • 参考资料
  • 题目描述
    • 输入格式
      • 输出格式
        • 样例
          • 数据范围与提示
          • AC代码
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档