• POJ 2155 Matrix 二维树状数组


    题目大意:有一个全零的矩阵,有两个操作。

    1.改动(x1,y1)到(x2,y2)的数,使它们取异或。

    2.查询(x,y)的状态。


    思路:二维树状数组,区间改动,单点查询。


    CODE:


    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define MAX 1010
    using namespace std;
    
    int cases;
    int cnt,asks;
    bool arr_tree[MAX][MAX];
    char s[10];
    
    inline void Fix(int x,int y);
    inline bool GetSum(int x,int y);
    
    int main()
    {
    	for(cin >> cases;cases; --cases) {
    		memset(arr_tree,false,sizeof(arr_tree));
    		scanf("%d%d",&cnt,&asks);
    		for(int i = 1;i <= asks; ++i) {
    			scanf("%s",s);
    			if(s[0] == 'C') {
    				int x1,y1,x2,y2;
    				scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    				Fix(x2,y2),Fix(x1 - 1,y1 - 1);
    				Fix(x2,y1 - 1),Fix(x1 - 1,y2);
    			}
    			else {
    				int x,y;
    				scanf("%d%d",&x,&y);
    				printf("%d
    ",GetSum(x,y));
    			}
    		}
    		puts("");
    	}
    	return 0;
    }
    
    inline void Fix(int x,int y)
    {
    	for(int i = x;i;i -= i&-i)
    		for(int j = y;j;j -= j&-j)
    			arr_tree[i][j] ^= 1;
    }	
    
    inline bool GetSum(int x,int y)
    {
    	bool re = 0;
    	for(int i = x;i <= cnt;i += i&-i)
    		for(int j = y;j <= cnt;j += j&-j)
    			re ^= arr_tree[i][j];
    	return re;
    }


  • 相关阅读:
    vs编译出现 fatal error LNK1281:无法生成 SAFESEH 映像
    $apply()和$digest()——angular
    JS获取URL中参数值
    NeDB——node嵌入式数据库
    VS Code常用插件
    js断点调试
    VS Code 使用Git进行版本控制
    VS Code快捷键
    用户tokenId
    node-webkit-updater——NW.js自动更新
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6940655.html
Copyright © 2020-2023  润新知