https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F

【题意】

给定一个矩阵,每个格子的初始值为1。现在可以对矩阵有四种操作:

A x y n1 :给格点(x,y)的值加n1

D x y n1: 给格点(x,y)的值减n1,如果现在格点的值不够n1,把格点置0

M x1 y1 x2 y2:(x1,y1)移动给(x2,y2)n1个

S x1 y1 x2 y2 查询子矩阵的和

【思路】

当然是二维树状数组

但是一定要注意:lowbit(0)是死循环,一定不能是0。所以初始化格点为1的时候要从1开始,以及对于输入的坐标,我们要加1处理。

【Accepted】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath> using namespace std;
typedef long long ll;
int n;
const int maxn=1e3+;
int bit[maxn][maxn]; int lowbit(int x)
{
return (-x)&x;
} void add(int x,int y,int val)
{
while(x<maxn)
{
int temp=y;
while(temp<maxn)
{
bit[x][temp]+=val;
temp+=lowbit(temp);
}
x+=lowbit(x);
}
} int sum(int x,int y)
{
int ans=;
while(x>)
{
int temp=y;
while(temp>)
{
ans+=bit[x][temp];
temp-=lowbit(temp);
}
x-=lowbit(x);
}
return ans;
} int solve(int x1,int y1,int x2,int y2)
{
return sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-)+sum(x1-,y1-);
} char op[];
int main()
{
int T;
scanf("%d",&T);
int cas=;
while(T--)
{
printf("Case %d:\n",++cas);
memset(bit,,sizeof(bit));
for(int i=;i<maxn;i++)
{
for(int k=;k<maxn;k++)
{
add(i,k,);
}
}
scanf("%d",&n);
for(int i=;i<n;i++)
{
// getchar();
scanf("%s",op);
if(op[]=='S')
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++;y1++;x2++;y2++;
if(x1>x2)
{
swap(x1,x2);
}
if(y1>y2)
{
swap(y1,y2);
}
printf("%d\n",solve(x1,y1,x2,y2));
}
if(op[]=='A')
{
int x,y,n1;
scanf("%d%d%d",&x,&y,&n1);
x++;y++;
add(x,y,n1);
}
if(op[]=='D')
{
int x,y,n1;
scanf("%d%d%d",&x,&y,&n1);
x++;y++;
if(solve(x,y,x,y)<n1)
{
n1=solve(x,y,x,y);
}
add(x,y,-n1);
}
if(op[]=='M')
{
int x1,y1,x2,y2,n1;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1);
x1++;y1++;x2++;y2++;
if(solve(x1,y1,x1,y1)<n1)
{
n1=solve(x1,y1,x1,y1);
}
add(x1,y1,-n1);
add(x2,y2,n1);
}
}
}
return ;
}

二维树状数组

【知识点】

1. 树状数组是O(logn)的,是因为n的二进制里最多有logn个1

2. 注意:树状数组的下标必须从1开始,,因为lowbit(0)=0,如果从0开始的话就会陷入死循环!!树状数组适用于所有满足结合律的运算(加法,乘法,异或等)

3. 所有树状数组能完成的操作线段树都能够完成,但是线段树的代码复杂,时间复杂度也比较高,查询、修改需要递归完成,而,树状数组的操作不仅代码简洁,便于理解,而且一切都是递推完成的,所以能用树状数组解决的问题尽量不要用线段树来写。

4. 树状数组可以查找逆序对,对于LIS问题可以查找方案数。

【二维树状数组】See you~的更多相关文章

  1. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  2. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  3. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  4. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  5. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  6. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  7. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  8. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  9. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

  10. MooFest_二维树状数组

    Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a s ...

随机推荐

  1. Service官方教程(1)Started与Bound的区别、要实现的函数、声明service

    Services 简介和分类 A Service is an application component that can perform long-running operations in the ...

  2. 225 Implement Stack using Queues 队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...

  3. 客户端负载均衡 - Ribbon

    Ribbon是Netflix公司开源的一个负载均衡的项目(https://github.com/Netflix/ribbon),它是一个基于HTTP.TCP的客户端负载均衡器. 服务端负载均衡 负载均 ...

  4. Jauery 中Ajax的几种异步请求

       以下介绍Jquery中  Post   Get   Ajax几种异步请求的使用方法  <%@ Page Language="C#" AutoEventWireup=&q ...

  5. Redux中的异步操作

    异步操作的另一种方案就是让Action Creator返回一个Promise对象. 我们这边使用  redux-promise  中间件 import { createStore, applyMidd ...

  6. iOS地图----MapKit框架

    1.MapKit框架使用前提 ①导入框架 ②导入主头文件 #import <MapKit/MapKit.h> ③MapKit框架使用须知 MapKit框架中所有数据类型的前缀都是MK Ma ...

  7. Katalon Studio 安装 配置 简单使用

    本教程只针对Katalon Studio进行演示操作. 一.下载 Katalon 官网下载地址:https://www.katalon.com/download/ (需要注册账号) 二.解压.配置 直 ...

  8. SQL——连接查询、聚合函数、开窗函数、分组功能、联合查询、子查询

    连接查询 inner join,用的最多,表示多张表一一对应 聚合函数 操作行数据,进行合并 sum.avg.count.max.min 开窗函数 将合并的数据分布到原表的每一行,相当于多出来了一列, ...

  9. vsphere中的linux虚拟机安装vmware-tools

    先在vcenter中选中虚拟机点击安装这个工具,如图 然后这台linux虚拟机的控制台操作,挂载先建立挂载目录 cd /mnt #在挂载建一个用来挂载的文件. mkdir cdrom 使用mount命 ...

  10. du查看文件大小

    du+文件名就可以查看文件大小 du+ -h + 文件名也是查看文件大小,只是-h会将文件大小转换成M,G等格式