zoukankan      html  css  js  c++  java
  • POJ 2155 Matrix(二维BIT)

    Matrix

    【题目链接】Matrix

    【题目类型】二维BIT

    &题解:

    bit只能单点更新,恰好,这题可以想一下就可以用单点更新解决了.
    只不过最后我交上去居然T了,想了10多分钟,试了一下关同步,结果就A了,1700ms,之后又优化了一下bit数组,改成了bool型,用了位运算,结果时间是1600ms,就快了100ms,真的不知道榜上那些100ms的代码是怎么写的 = =

    &代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn=1005;
    int n,t;
    bool bit[maxn][maxn];
    
    bool Sum(int x,int y) {
    	bool ans=0;
    	for(int i=x; i>0; i-=i&-i) {
    		for(int j=y; j>0; j-=j&-j) {
    			ans^=bit[i][j];
    		}
    	}
    	return ans;
    }
    void Add(int x,int y) {
    	for(int i=x; i<=n; i+=i&-i) {
    		for(int j=y; j<=n; j+=j&-j) {
    			bit[i][j]^=1;
    		}
    	}
    }
    
    int main() {
    	freopen("e:1.in","r",stdin);
    	iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    	int T; cin>>T;
    	while(T--) {
    		memset(bit,0,sizeof(bit));
    		cin>>n>>t;
    		for(int i=0; i<t; i++) {
    			string op;
    			int x1,y1,x2,y2;
    			cin>>op>>x1>>y1;
    			if(op=="Q") {
    				cout<<Sum(x1,y1)<<endl;
    			}
    			else {
    				cin>>x2>>y2;
    				Add(x1,y1);
    				Add(x2+1,y1);
    				Add(x1,y2+1);
    				Add(x2+1,y2+1);
    			}
    		}
    		cout<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    paste 合并文件
    split 分割文件
    cut 从文本中提取一段文字并输出
    tailf 跟踪日志文件
    tail 显示文件内容尾部
    给Linux系统新增加一块硬盘
    Centos7+httpd+fastcgi安装提示错误
    Redhat 7使用CentOS 7的Yum网络源
    windows7下cmd窗口使用ssh命令
    PHP set_error_handler() 函数
  • 原文地址:https://www.cnblogs.com/s1124yy/p/6768225.html
Copyright © 2011-2022 走看看