zoukankan      html  css  js  c++  java
  • poj1656 Counting Black

    可用暴力法~

    下面用二维树状数组:

    /*
     * 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;
    }

  • 相关阅读:
    Javascript中的Math.max()和Math.min()
    附件预览项目采坑记
    移动设备后台的理解
    网络是怎么连接的?(进阶一)
    Git建立本地分支和远程分支的映射关系
    springboot使用redis实现发布与订阅
    centos下安装配置mongodb
    vue循环时设置多选框禁用状态,v-for
    谷歌浏览器postman插件安装,亲测可用
    element-ui 无法对绑定表单的对象中的对象属性进行验证
  • 原文地址:https://www.cnblogs.com/longdouhzt/p/2099627.html
Copyright © 2011-2022 走看看