poj 1195 Mobile phones(二维树状数组)

二维树状数组用于求子矩阵的和。

View Code
 1 /*

 2 Author:Zhaofa Fang

 3 Lang:C++

 4 */

 5 #include <cstdio>

 6 #include <cstdlib>

 7 #include <iostream>

 8 #include <cmath>

 9 #include <cstring>

10 #include <algorithm>

11 #include <string>

12 #include <utility>

13 #include <vector>

14 #include <queue>

15 #include <stack>

16 #include <map>

17 #include <set>

18 using namespace std;

19 typedef long long ll;

20 #define pii pair<int,int>

21 #define pb push_back

22 #define mp make_pair

23 #define fi first

24 #define se second

25 #define lowbit(x) (x&(-x))

26 #define INF (1<<30)

27 

28 const int maxn = 1025;

29 int c[maxn][maxn];

30 int n;

31 void add(int x,int y,int v)

32 {

33     for(int i=x;i<=n;i+=lowbit(i))

34         for(int j=y;j<=n;j+=lowbit(j))

35             c[i][j] += v;

36 }

37 int sum(int x,int y)

38 {

39     int res = 0;

40     for(int i=x;i>0;i-=lowbit(i))

41         for(int j=y;j>0;j-=lowbit(j))

42             res += c[i][j];

43     return res;

44 }

45 int main()

46 {

47     #ifndef ONLINE_JUDGE

48     freopen("in","r",stdin);

49     #endif

50     int op;

51     while(~scanf("%d",&op))

52     {

53         if(op == 0)

54         {

55             scanf("%d",&n);

56             memset(c,0,sizeof(c));

57         }

58         else if(op == 1)

59         {

60             int x,y,a;

61             scanf("%d%d%d",&x,&y,&a);

62             add(x+1,y+1,a);

63         }

64         else if(op == 2)

65         {

66             int l,b,r,t;

67             scanf("%d%d%d%d",&l,&b,&r,&t);

68             printf("%d\n",sum(r+1,t+1)+sum(l,b)-sum(l,t+1)-sum(r+1,b));

69         }

70         else break;

71     }

72     return 0;

73 }

你可能感兴趣的:(mobile)