首页 > 代码库 > [Vijos1512] SuperBrother打鼹鼠 (二维树状数组)
[Vijos1512] SuperBrother打鼹鼠 (二维树状数组)
传送门
直接搞就行。
注意下表re从零开始,而树状数组搞不了0,所以统一增加一个偏移量1.
(话说数据随机是什么鬼?)
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <string> 5 # include <cmath> 6 # include <vector> 7 # include <map> 8 # include <queue> 9 # include <cstdlib>10 # define MAXN 110011 using namespace std;12 13 inline int get_num() {14 int k = 0, f = 1;15 char c = getchar();16 for(; !isdigit(c); c = getchar()) if(c == ‘-‘) f = -1;17 for(; isdigit(c); c = getchar()) k = k * 10 + c - ‘0‘;18 return k * f;19 }20 21 int n;22 int c[MAXN][MAXN];23 inline int lowbit(int x)24 {25 return x & -x;26 }27 28 inline void add(int x, int y, int k)29 {30 int i, j;31 for(i = x; i <= n; i += lowbit(i))32 for(j = y; j <= n; j += lowbit(j))33 c[i][j] += k;34 }35 36 inline int query(int x, int y)37 {38 int i, j, ans = 0;39 for(i = x; i; i -= lowbit(i))40 for(j = y; j; j -= lowbit(j))41 ans += c[i][j];42 return ans;43 }44 45 int main()46 {47 int i, x, y, k, x1, x2, y1, y2, m;48 n = get_num() + 1;49 while(1)50 {51 m = get_num();52 if(m == 3) break;53 if(m == 1)54 {55 x = get_num() + 1;56 y = get_num() + 1;57 k = get_num();58 add(x, y, k);59 }60 else61 {62 x1 = get_num() + 1;63 y1 = get_num() + 1;64 x2 = get_num() + 1;65 y2 = get_num() + 1;66 printf("%d\n", query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1));67 }68 }69 return 0;70 }
[Vijos1512] SuperBrother打鼹鼠 (二维树状数组)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。