题目链接

二维树状数组

 #include<iostream>
 #include<math.h>
 #include<algorithm>
 #include<stdlib.h>
 using namespace std;
 #define ll long long
 #define re(i,n) for(int i=0;i<n;i++)
 ;
 int c[maxn][maxn];
 int n, q;
 int lowbit(int x){
     return x&(-x);
 }
 void update(int x, int y){
     ; i-=lowbit(i))
         ; j -= lowbit(j))
             c[i][j] ^= ;
 }
 int query(int x, int y){
     ;
     for (int i = x; i <= n; i+=lowbit(i))
     for (int j = y; j <= n; j += lowbit(j)){
         ans ^= c[i][j];
     }
     return ans;
 }
 int main(){
     //freopen("in.txt", "r", stdin);
     int T; cin >> T;
     while (T--){
         scanf("%d%d", &n, &q);
         memset(c, , sizeof(c));
         while (q--){
             ]; scanf("%s", op);
             ] == 'Q'){
                 int x, y; scanf("%d%d", &x, &y);
                 printf("%d\n", query(x, y));
             }
             else{
                 int fx, fy, tx, ty; scanf("%d%d%d%d", &fx, &fy, &tx, &ty);
                 if (fx > tx)swap(fx, tx); if (fy > ty)swap(fy, ty);
                 fx--, fy--;
                 update(fx, fy);
                 update(tx, ty);
                 update(fx, ty);
                 update(tx, fy);
             }
         }
         puts("");
     }
     ;
 }

二维线段树

 #include<iostream>
 #include<stdio.h>
 #include<string.h>
 using namespace std;
 #define ll long long
 #define re(i,n) for(int i=0;i<n;i++)
 #define ls(x) x<<1,f,mid
 #define rs(x) x<<1|1,mid+1,t
 ;
 int n, q;
 int fx, fy, tx, ty, x, y;
 int ans;
 ][maxn << ];
 void updatey(int i, int j, int f, int t){
     if (fy <= f&&ty >= t){
         a[i][j] ^= ;
         return;
     }
     ;
     if (fy <= mid)updatey(i,ls(j));
     if (ty > mid)updatey(i, rs(j));
 }
 void updatex(int i, int f, int t){
     if (fx<=f&&tx>=t){
         updatey(i, , , n);
         return;
     }
     ;
     if (fx <= mid)updatex(ls(i));
     if (tx > mid)updatex(rs(i));
 }
 void queryy(int i, int j, int f, int t){
     ans += a[i][j];
     if (f == t)return;
     ;
     if (y > mid)queryy(i,rs(j));
     else queryy(i, ls(j));
 }
 void queryx(int i, int f, int t){
     queryy(i, , , n);
     if (f == t)return;
     ;
     if (x > mid)queryx(rs(i));
     else queryx(ls(i));
 }
 int main(){
     //freopen("in.txt", "r", stdin);
     int T; cin >> T;
     while (T--){
         scanf("%d%d", &n, &q);
         memset(a, , sizeof(a));
         /*应该用memset,用for循环一定要注意初始化的不是n,而是全部.
         公元2015年9.20日在于此坑.
         re(i, n + 1)re(j, n + 1)a[i][j] = 0;
         */
         while (q--){
             ]; scanf("%s", op);
             ] == 'Q'){
                 scanf("%d%d",&x,&y);
                 ans = ;
                 queryx(, , n);
                 printf();
             }
             else{
                 scanf("%d%d%d%d", &fx, &fy, &tx, &ty);
                 if (fx > tx)swap(fx, tx); if (fy > ty)swap(fy, ty);
                 updatex(,,n);
                 //debug();
             }
         }
         if (T)puts("");
     }
     ;
 }

zkw二维线段树 开区间写法

 #include<iostream>
 #include<stdio.h>
 #include<algorithm>
 using namespace std;
 ;
 typedef long long ll;
 #define re(i,n) for(int i=0;i<n;i++)
 ][maxn << ];
 int n, q,sz;
 bool yquery(int x, int y){
     y += sz;
     ;
     /*
     因为1是哨兵节点,1必然始终都为0,不曾变化,所以算上1也无妨
     */
     while (y){
         ans ^= tr[x][y];
         y >>= ;
     }
     return ans;
 }
 bool query(int x,int y){
     x += sz;
     ;
     while (x){
         ans ^= yquery(x, y);
         x >>= ;
     }
     return ans;
 }
 void yupdate(int x, int fy, int ty){
     fy += sz - , ty += sz + ;
     ){
         )tr[x][fy ^ ] ^= ;
         )tr[x][ty ^ ] ^= ;
         fy >>= , ty >>= ;
     }
 }
 void update(int fx,int fy,int tx,int ty){
     fx += sz - , tx += sz + ;
     ){
         )yupdate(fx^, fy, ty);
         )yupdate(tx ^ , fy, ty);
         fx >>= , tx >>= ;
     }
 }
 int main(){
     //freopen("in.txt", "r", stdin);
     int T; cin >> T;
     while (T--){
         scanf("%d%d", &n, &q);
         sz = ; )sz <<= ;
         memset(tr, , sizeof(tr));
         while (q--){
             ]; scanf("%s", op);
             ] == 'Q'){
                 int x, y; scanf("%d%d", &x, &y);
                 bool ans = query(x, y);
                 printf("%d\n", ans);
             }
             else{
                 int fx, fy, tx, ty; scanf("%d%d%d%d", &fx, &fy, &tx, &ty);
                 if (fx > tx)swap(fx, tx); if (fy > ty)swap(fy, ty);
                 update(fx, fy, tx, ty);
             }
         }
         puts("");
     }
     ;
 }

zwk二维线段树闭区间写法

 #include<iostream>
 #include<stdio.h>
 #include<algorithm>
 using namespace std;
 ;
 typedef long long ll;
 #define re(i,n) for(int i=0;i<n;i++)
 ][maxn << ];
 int n, q,sz;
 bool yquery(int x, int y){
     y += sz-;
     ;
     /*
     因为1是哨兵节点,1必然始终都为0,不曾变化,所以算上1也无妨
     */
     while (y){
         ans ^= tr[x][y];
         y >>= ;
     }
     return ans;
 }
 bool query(int x,int y){
     x += sz-;
     ;
     while (x){
         ans ^= yquery(x, y);
         x >>= ;
     }
     return ans;
 }
 /*
 开区间=闭区间-两个端点.
 所以,先处理两个端点之后就变成了开区间.
 两个端点处理时要加上一个判断,l==r,如果相等,那就不能对同一个点处理两次,只处理一个点然后返回就行了;如果不等,那就先处理两端,再按照开区间的方式进行处理.
 */
 void yupdate(int x, int fy, int ty){
     fy += sz - , ty += sz -;
     ; return; }
     tr[x][fy] ^= , tr[x][ty] ^= ;
     ){
         )tr[x][fy ^ ] ^= ;
         )tr[x][ty ^ ] ^= ;
         fy >>= , ty >>= ;
     }
 }
 void update(int fx,int fy,int tx,int ty){
     fx += sz - , tx += sz - ;
     if (fx == tx){
         yupdate(fx, fy, ty); return;
     }
     yupdate(fx, fy, ty), yupdate(tx, fy, ty);
     ){
         )yupdate(fx^, fy, ty);
         )yupdate(tx ^ , fy, ty);
         fx >>= , tx >>= ;
     }
 }
 int main(){
     freopen("in.txt", "r", stdin);
     int T; cin >> T;
     while (T--){
         scanf("%d%d", &n, &q);
         sz = ; ;
         memset(tr, , sizeof(tr));
         while (q--){
             ]; scanf("%s", op);
             ] == 'Q'){
                 int x, y; scanf("%d%d", &x, &y);
                 bool ans = query(x, y);
                 printf("%d\n", ans);
             }
             else{
                 int fx, fy, tx, ty; scanf("%d%d%d%d", &fx, &fy, &tx, &ty);
                 if (fx > tx)swap(fx, tx); if (fy > ty)swap(fy, ty);
                 update(fx, fy, tx, ty);
             }
         }
         putchar();//换行是10,回车13,空格32
     }
     ;
 }

POJ2155 Matrix二维线段树经典题的更多相关文章

  1. POJ2155 Matrix 二维线段树

    关键词:线段树 二维线段树维护一个 维护一个X线段的线段树,每个X节点维护一个 维护一个Y线段的线段树. 注意,以下代码没有PushDownX.因为如果要这么做,PushDownX时,由于当前X节点的 ...

  2. hdu1823(二维线段树模板题)

    hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...

  3. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  4. POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询

    本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样, ...

  5. poj 2155 matrix 二维线段树 线段树套线段树

    题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...

  6. Mosaic HDU 4819 二维线段树入门题

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  7. poj 2155 matrix 二维线段树

    题目链接 区间翻转, 单点查询, 查询操作我真是不太明白...... #include <iostream> #include <vector> #include <cs ...

  8. POJ2155 Matrix 【二维线段树】

    题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...

  9. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

随机推荐

  1. android oncreate获取宽高度

    gridView = (GridView) getView().findViewById(R.id.gridView_musicbook); gridView.getViewTreeObserver( ...

  2. 彻底解决mysql中文乱码的办法,修改mysql解压缩版(免安装版或zip版)字符编码

    MySQL会出现中文乱码的原因不外乎下列几点:1.server本身设定问题,例如server字符编码还停留在latin12.table的语系设定问题(包含character与collation)3.客 ...

  3. Machine Learning Algorithms Study Notes(1)--Introduction

    Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 目 录 1    Introduction    1 1.1    ...

  4. AC日记——大小写字母互换 openjudge 1.7 14

    14:大小写字母互换 总时间限制:  1000ms 内存限制:  65536kB 描述 把一个字符串中所有出现的大写字母都替换成小写字母,同时把小写字母替换成大写字母. 输入 输入一行:待互换的字符串 ...

  5. Makefile规则③规则语法、依赖、通配符、目录搜寻、目标

    规则语法 通常规则的语法格式如下: TARGETS : PREREQUISITES COMMAND ... 或者: TARGETS : PREREQUISITES ; COMMAND COMMAND ...

  6. [No000033]码农网-如何锻炼出最牛程序员的编码套路

    最近,我大量阅读了Steve Yegge的文章.其中有一篇叫"Practicing Programming"(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反 ...

  7. nmap脚本扫描使用总结

    nmap的脚本默认目录为:/usr/share/nmap/scripts/ Nmap提供的命令行参数如下 -sC: 等价于--script=default,使用默认类别的脚本进行扫描 可更换其他类别 ...

  8. 南邮oj[1401] 乘车费用

    Description lqp家离学校十分十分远,同时他又没有钱乘taxi.于是他不得不每天早早起床,匆匆赶到公交车站乘车到学校.众所周知CZ是个公交车十分发达的地方,但是CZ的公交车十分的奇怪,lq ...

  9. ajax asud模板

    <table class="table"> <tr> <th>@Html.DisplayNameFor(model=>model.Id)& ...

  10. 10 Things Every Java Programmer Should Know about String

    String in Java is very special class and most frequently used class as well. There are lot many thin ...