生活随笔
收集整理的這篇文章主要介紹了
NYOJ-172 小珂的图表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
小珂的圖表
時間限制:1000 ms ?|? 內存限制:65535 KB 難度:2 描述
小珂最近有一個麻煩,就是要統計一下指定區域中有幾個方格被染黑了.表格的布局及表格各個位置的坐標如下所示.
?
?
有三種操作命令,
BLACK x,y,l 表示把以坐標(x,y)為左上角頂點,(x+l-1,y+l-1)為右下角頂點的矩形染黑。
WHITE x,y,l 表示吧指定區域染白。
TEST x,y,l 表示計算指定區域的黑塊的個數。
說明:如果 x,y,x+l-1 ,y+l-1超出圖表的范圍,就只計算圖表內部的。
輸入第一行有一個整數n(0<n<100),表示有n條命令,隨后的n行有n個指令。輸出遇到TEST命令,把結果輸出并換行。樣例輸入 5
BLACK 1 1 2
BLACK 2 2 2
TEST 1 1 3
WHITE 2 1 1
TEST 1 1 3
樣例輸出 7
6
來源POJ 1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4
5 using namespace std;
6
7 int map[105][105];
8 int col[105][105];
9
10 int lowbit(int i)
11 {
12 return i & (-i);
13 }
14
15 void add(int x, int y, int color)
16 {
17 if(color == col[x][y])
18 return ;
19 col[x][y] = color;
20 for(int i = x; i <= 100; i += lowbit(i))
21 for(int j = y; j <= 100; j += lowbit(j))
22 map[i][j] += color;
23 }
24
25 int getSum(int x, int y)
26 {
27 int tot = 0;
28 for(int i = x; i > 0; i -= lowbit(i))
29 for(int j = y; j > 0; j -= lowbit(j))
30 tot += map[i][j];
31 return tot;
32 }
33
34 int main()
35 {
36 int T, x, y, x1, y1, len;
37 char cmd[10];
38 memset(map, 0, sizeof(map));
39 memset(col, -1, sizeof(col)); // 初始標記每個格子都是白色
40
41 scanf("%d", &T);
42 while(T--)
43 {
44 scanf("%s%d%d%d", cmd, &x, &y, &len);
45
46 x1 = x+len-1 > 100 ? 100 : x+len-1;
47 y1 = y+len-1 > 100 ? 100 : y+len-1;
48
49 if(cmd[0] == 'B')
50 {
51 for(int i = x; i <= x1; ++i)
52 for(int j = y; j <= y1; ++j)
53 add(i, j, 1);
54 }
55 else if(cmd[0] == 'W')
56 {
57 for(int i = x; i <= x1; ++i)
58 for(int j = y; j <= y1; ++j)
59 add(i, j, -1);
60 }
61 else
62 {
63 printf("%d\n", getSum(x1,y1)+getSum(x-1,y-1)-getSum(x-1,y1)-getSum(x1,y-1));
64 }
65 }
66 return 0;
67 } ?
轉載于:https://www.cnblogs.com/dongsheng/archive/2013/05/31/3111481.html
總結
以上是生活随笔為你收集整理的NYOJ-172 小珂的图表的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。