【线段树】 POJ 2155 Matrix 二维线段树/二维树状数组

C操作 取反 一个左上角 x1,y1 右下角x2,y2 的矩阵 0变1 1变0 ;

Q操作 输出x,y位置的值  

mark二维树状数组方法

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 4001;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=123456789;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int n,m;
int sum[MAXN][MAXN],ans;
void updatey(int x,int y1,int y2,int l,int r,int rt)
{
    if(y1==l&&r==y2)
    {
        sum[x][rt]^=1;
        return ;
    }
    int m=(l+r)>>1;
    if(y2<=m) updatey(x,y1,y2,lson);
    else if(m<y1) updatey(x,y1,y2,rson);
    else
    {
        updatey(x,y1,m,lson);
        updatey(x,m+1,y2,rson);
    }
}
void updatex(int x1,int y1,int x2,int y2,int l,int r,int rt)
{
    if(x1==l&&r==x2)
    {
        updatey(rt,y1,y2,1,n,1);
        return ;
    }
    int m=(l+r)>>1;
    if(x2<=m) updatex(x1,y1,x2,y2,lson);
    else if(m<x1) updatex(x1,y1,x2,y2,rson);
    else
    {
        updatex(x1,y1,m,y2,lson);
        updatex(m+1,y1,x2,y2,rson);
    }
}
void queryy(int x,int y,int l,int r,int rt)
{
    ans^=sum[x][rt];
    if(l==r) return ;
    int m=(l+r)>>1;
    if(y<=m)  queryy(x,y,lson);
    else  queryy(x,y,rson);
}
void queryx(int x,int y,int l,int r,int rt)
{
    queryy(rt,y,1,n,1);
    if(l==r)
        return;
    int m=(l+r)>>1;
    if(x<=m) queryx(x,y,lson);
    else queryx(x,y,rson);
}
int main()
{
    int t;
   // IN;
    scanf("%d",&t);
    while(t--)
    {
        cler(sum,0);
        char s[3];
        scanf("%d%d",&n,&m);
        while(m--)
        {
            int a,b,c,d;
            scanf("%s",s);
            if(s[0]=='C')
            {
                scanf("%d%d%d%d",&a,&b,&c,&d);
                updatex(a,b,c,d,1,n,1);
            }
            else
            {
                ans=0;
                scanf("%d%d",&a,&b);
                queryx(a,b,1,n,1);
                printf("%d\n",ans);
            }
        }
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值