POJ 1195 Mobile phones

题目链接:http://poj.org/problem?id=1195

二维树状数组的应用

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <cstdlib>

 4 

 5 const int MAXN = 1200;

 6 int S;

 7 int C[MAXN][MAXN];

 8 

 9 int lowbit( int x )

10 {

11     return x & -x;

12 }

13 

14 void Add( int x, int y, int add )

15 {

16     int tpy;

17     while ( x <= S )

18     {

19         tpy = y;

20         while ( tpy <= S )

21         {

22             C[x][tpy] += add;

23             tpy += lowbit(tpy);

24         }

25         x += lowbit(x);

26     }

27     return;

28 }

29 

30 int Sum( int x, int y )

31 {

32     int ret = 0;

33     while ( x > 0 )

34     {

35         int tpy = y;

36         while ( tpy > 0 )

37         {

38             ret += C[x][tpy];

39             tpy -= lowbit(tpy);

40         }

41         x -= lowbit(x);

42     }

43     return ret;

44 }

45 

46 int main()

47 {

48     int op, add;

49     int x1, y1, x2, y2;

50     while( ~scanf( "%d%d", &op, &S ) )

51     {

52         memset( C, 0, sizeof(C) );

53         while ( scanf( "%d", &op ), op != 3 )

54         {

55             if ( op == 1 )

56             {

57                 scanf( "%d%d%d", &x1, &y1, &add );

58                 ++x1, ++y1;

59                 Add( x1, y1, add );

60             }

61             else

62             {

63                 scanf( "%d%d%d%d", &x1, &y1, &x2, &y2 );

64                 ++x1, ++y1;

65                 ++x2, ++y2;

66                 printf("%d\n", Sum(x2, y2) - Sum(x1 - 1, y2) - Sum(x2, y1 - 1) + Sum(x1 - 1, y1 - 1) );

67             }

68         }

69     }

70     return 0;

71 }

 

你可能感兴趣的:(mobile)