[Vijos1512] SuperBrother打鼹鼠 (二维树状数组)
Posted 蒟蒻zht的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 1100 11 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 else 61 { 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打鼹鼠 (二维树状数组)的主要内容,如果未能解决你的问题,请参考以下文章
vijos p1512——SuperBrother打鼹鼠(树状数组)(复习)