现在的位置: 首页 > 综合 > 正文

poj-1656 Counting Black

2013年02月08日 ⁄ 综合 ⁄ 共 1356字 ⁄ 字号 评论关闭

可用暴力法~

下面用二维树状数组:

/*
 * 1656.cpp
 *
 *  Created on: 2011-7-6
 *      Author:
 *
 */
#include <iostream>
#include <string>
using namespace std;

const int MAXN = 100 + 5;
const int W = 0, B = 1;
int map[MAXN][MAXN] = {};
int c[MAXN][MAXN] = {};
int t, x, y, l;
string cmd;

int lowbit(int _x){
    return _x & (-_x);
}
void update(int mx, int my, int ml){
    if(cmd[0] == 'B'){
        for(int i=mx; i<=mx+ml-1; i++){
            for(int j=my; j<=my+ml-1; j++){
                if(map[i][j] != B){
                    map[i][j] = B;

                    for(int _x=i; _x<MAXN; _x+=lowbit(_x)){
                        for(int _y=j; _y<MAXN; _y+=lowbit(_y)){
                            c[_x][_y]++;
                        }
                    }
                }
            }
        }
    }

    else if(cmd[0] == 'W'){
        for(int i=mx; i<=mx+ml-1; i++){
            for(int j=my; j<=my+ml-1; j++){
                if(map[i][j] != W){
                    map[i][j] = W;

                    for(int _x=i; _x<MAXN; _x += lowbit(_x)){
                        for(int _y=j; _y<MAXN; _y += lowbit(_y)){
                            c[_x][_y]--;
                        }
                    }
                }
            }
        }
    }
}

int sum(int _x, int _y){
    int s = 0;
    for(int i=_x; i>0; i-=lowbit(i)){
        for(int j=_y; j>0; j-=lowbit(j)){
            s += c[i][j];
        }
    }

    return s;
}

int main(){
    cin >> t;
    while(t--){
        cin >> cmd >> x >> y >> l;
        if(cmd[0] == 'T'){
            cout<<sum(x+l-1,y+l-1)-sum(x+l-1,y-1)-sum(x-1,y+l-1)+sum(x-1,y-1)<<endl;
        }
        else
            update(x, y, l);
    }

    return 0;
}

抱歉!评论已关闭.