HihoCoder - 1336 二维数状数组(单点更新 区间查询)
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 ≤ x2, y1 ≤ y ≤ y2.
InputThe 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
OutputFor each Sum operation output a non-negative number denoting the sum modulo 109+7.
Sample Input
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
Sample Output
1 2 3
题目大意:给你一个N*N二维矩阵,初始值时都为0,有两种操作
1:单点修改值
2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和.
1:单点修改值
2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和.
二维树状数组模板题,单点更新区间查询
#include<queue> #include<set> #include<cstdio> #include <iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; #define max_v 1005 #define mod 1000000007 typedef long long LL; int c[max_v][max_v]; int lowbit(int x) { return x&(-x); } LL getsum(int x,int y) { LL sum=0; for(int i=x;i!=0;i-=lowbit(i)) { for(int j=y;j!=0;j-=lowbit(j)) { sum=(sum+c[i][j]); } } return sum; } void update(int x,int y,int v) { for(int i=x;i<max_v;i+=lowbit(i)) { for(int j=y;j<max_v;j+=lowbit(j)) { c[i][j]=(c[i][j]+v); } } } int main() { char str[10]; int n,m; while(~scanf("%d %d",&n,&m)) { memset(c,0,sizeof(c)); int a,b,w; while(m--) { scanf("%s",str); if(str[0]=='A') { scanf("%d %d %d",&a,&b,&w); a++; b++; update(a,b,w); }else { int sx,sy,ex,ey; scanf("%d %d %d %d",&sx,&sy,&ex,&ey); sx++; sy++; ex++; ey++; printf("%lld\n",(getsum(ex,ey)-getsum(sx-1,ey)-getsum(ex,sy-1)+getsum(sx-1,sy-1)+mod)%mod); } } } return 0; } /* 题目大意:给你一个N*N二维矩阵,初始值时都为0,有两种操作 1:单点修改值 2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和. 二维树状数组模板题,单点更新区间查询 */
心之所向,素履以往
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南