【二维树状数组】See you~
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~的更多相关文章
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- 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 ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- MooFest_二维树状数组
Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a s ...
随机推荐
- Windows查杀端口
Windows环境下当某个端口被占用时,通过netstat命令进行查询pid,然后通过taskkill命令杀进程. 一.查询占用端口号的进程信息 netstat -an|findstr 二.杀掉占用端 ...
- 简单备份11g db (文件系统)
1.more check.sqlsqlplus / as sysdba << EOF!banner start dbstartupselect name from v\$database; ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
- NavigationView的使用
代码已经分享至github:https://github.com/YanYoJun/NavigationDemo 转载请注明原文链接:http://www.cnblogs.com/yanyojun/p ...
- JVM最多能创建多少个线程: unable to create new native thread
转载自:http://www.rigongyizu.com/jvm-max-threads/ 有应用报出这样的异常“java.lang.OutOfMemoryError: unable to crea ...
- mybatis 返回值
转载: 在使用ibatis插入数据进数据库的时候,会用到一些sequence的数据,有些情况下,在插入完成之后还需要将sequence的值返回,然后才能进行下一步的操作. 使用ibatis的 ...
- Activiti数据库表结构(表详细版)
http://blog.csdn.net/hj7jay/article/details/51302829 1 Activiti数据库表结构 1.1 数据库表名说明 Activiti工作流总 ...
- react link引入外部css样式的坑
刚开始的代码是这样的,使用react router4.x写的demo路由跳转后,页面的没有渲染,是因为没有引入外部css文件(或者说引入外部文件路径错误) <!DOCTYPE html> ...
- R-FCN:Object Detection via Region-based Fully Convolutional Networks
fast.faster这些网络都可以被roi-pooling层分成两个子网络:1.a shared,'fully convolutional' subnetwork 2.an roi-wise sub ...