POJ 2155 D区段树
POJ 2155 D区段树
思考:D区段树是每个节点设置一个段树树。
刚開始由于题目是求A[I,J],然后在y查询那直接ans^=Map[i][j]的时候没看懂。后面自己把图画出来了才理解。
由于仅仅有0和1。所以能够用异或来搞,而不须要每次都须要改动。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define llson j<<1,l,mid
#define rrson j<<1|1,mid+1,r
#define INF 510010
#define maxn 4010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
bool Map[maxn][maxn];
int n,q,t,ans;
void update_y(int i,int j,int l,int r,int y1,int y2)
{
if(l==y1&&r==y2) {Map[i][j]^=1;return ;}
int mid=(l+r)>>1;
if(y2<=mid) update_y(i,llson,y1,y2);
else if(y1>mid) update_y(i,rrson,y1,y2);
else
{
update_y(i,llson,y1,mid);
update_y(i,rrson,mid+1,y2);
}
}
void update_x(int i,int l,int r,int x1,int x2,int y1,int y2)
{
if(l==x1&&r==x2)
{
update_y(i,1,1,n,y1,y2);
return ;
}
int mid=(l+r)>>1;
if(x2<=mid) update_x(lson,x1,x2,y1,y2);
else if(x1>mid) update_x(rson,x1,x2,y1,y2);
else
{
update_x(lson,x1,mid,y1,y2);
update_x(rson,mid+1,x2,y1,y2);
}
}
void query_y(int i,int j,int l,int r,int y)
{
ans^=Map[i][j];
if(l==r) return ;
int mid=(l+r)>>1;
if(y<=mid) query_y(i,llson,y);
else query_y(i,rrson,y);
}
void query_x(int i,int l,int r,int x,int y)
{
query_y(i,1,1,n,y);
if(l==r) return ;
int mid=(l+r)>>1;
if(x<=mid) query_x(lson,x,y);
else query_x(rson,x,y);
}
int main()
{
//freopen("test.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
mem(Map,0);
while(q--)
{
char s[2];
scanf("%s",s);
ans=0;
if(s[0]=='C')
{
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
update_x(1,1,n,x1,x2,y1,y2);
}
else
{
int x,y;
scanf("%d%d",&x,&y);
query_x(1,1,n,x,y);
printf("%d\n",ans);
}
}
if(t) puts("");
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ 2155 D区段树的更多相关文章
- POJ 2155 Matrix(树状数组+容斥原理)
[题目链接] http://poj.org/problem?id=2155 [题目大意] 要求维护两个操作,矩阵翻转和单点查询 [题解] 树状数组可以处理前缀和问题,前缀之间进行容斥即可得到答案. [ ...
- poj 2155 (二维树状数组 区间修改 求某点值)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33682 Accepted: 12194 Descript ...
- POJ poj 2155 Matrix
题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 1849 Two(遍历树)
POJ 1849 Two(遍历树) http://poj.org/problem?id=1849 题意: 有一颗n个结点的带权的无向树, 在s结点放两个机器人, 这两个机器人会把树的每条边都走一遍, ...
- Buy Tickets POJ - 2828 思维+线段树
Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...
- POJ 2155 Matrix (D区段树)
http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1 ...
- POJ 2155 Matrix【二维树状数组+YY(区间计数)】
题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
随机推荐
- JAVA进阶----主线程等待子线程各种方案比较(转)
创建线程以及管理线程池基本理解 参考原文链接:http://www.oschina.net/question/12_11255?sort=time 一.创建一个简单的java线程 在 Java 语言中 ...
- password加密问题
password加密问题 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人爱好:酷爱数据结构和算法,希望将来 ...
- hdu2125(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2125 题意:N×M的网格其中有一条边坏掉了,问从起点到终点的放法数. 分析:数学公式 如果没有坏边的话 ...
- Cocos2d-x教程(28)-ttf 字体库的使用
欢迎增加 Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/37650843 通常为 ...
- C#基础总结之Attribute
Attribute是什么 Attribute的中文姓名 为什么我要拿一段文字来说Attribute的中文姓名呢?答案是:因为这很重要.正所谓“名”不正,则言不顺:另外重构手法中有一种很重要的方法叫重命 ...
- Effective C++ 条款24
若全部參数皆需类型转换,请为此採用non-member函数 我们直奔主题 假设你定义一个有理数类例如以下 class Rational{ public: Rational(int numerator= ...
- java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor An ExecutorService that executes each submitted task using o ...
- 【OC加强】NSDate的使用方法——日期时间在实际开发中比較有用
(1)日期的最主要知识点就是日期转换成字符串格式化输出,相反就是依照某个格式把字符串转换成日期. (2)一般关于时区的设置非常少用到,仅仅要了解就可以. #import <Foundation/ ...
- windows下php开发环境的搭建
环境搭建软件组合为:Apache2.2.9+mysql5.2.32+php5.2.6 下载地址如下 http://download.csdn.net/detail/xttxqjfg/5670455 ...
- windows phone (18) Border元素
原文:windows phone (18) Border元素 Border类是对某一个对象的周围边框,背景,或者同时绘制两者,首先看一个简单的例子进行分析[作者:神舟龍] xaml文件: <!- ...