【二维树状数组】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 ...
随机推荐
- 如何用C#动态编译、执行代码[转]
原文链接 在开始之前,先熟悉几个类及部分属性.方法:CSharpCodeProvider.ICodeCompiler.CompilerParameters.CompilerResults.Assemb ...
- 工厂方法模式及php实现
工厂方法模式: 工厂方法模式(Factory Method Pattern)又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Polymorphic Facto ...
- swiper4初始化/swiper-init/data-swiper
用data属性初始化swiper <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- openmv第一次调试
2018-09-19 20:14:51 import sensor, image, time import car import json import time from pyb import U ...
- R Programming week1-Subsetting
Subsetting There are a number of operators that can be used to extract subsets of R objects. [ alway ...
- BotFramework学习-01
微软在Build2016大会上表示,未来将是一个充满聊天机器人的世界,为此他们推出了微软Bot Framework,能够允许任何人制作自己的聊天机器人,微软则提供“cognitive microser ...
- MySql 基础知识-常用命令及sql语句
一.常用mysql命令行命令 1,启动mysql服务 net start mysql. 停止mysql服务 net stop mysql 2,netstart -na|findstr 330 ...
- codeforces_D. Social Circles
http://codeforces.com/contest/1060/problem/D 题意: n个客人,每个客人希望自己左边空li个座位,右边空ri个座位,可以形成任意个圆,问最少多少个座位. 思 ...
- Java锁,真的有这么复杂吗?
为什么使用synchronizedvolatile,在多线程下可以保证变量的可见性,但是不能保证原子性,下面一段代码说明: 运行上面代码,会发现输出flag的值不是理想中10000,虽然volatil ...
- Python3.0 操作MySQL数据库执行SQL语句
py3不支持MySQLdb,需要导入pymysql模块 # coding: utf-8 # Team : Quality Management Center # Author:Carson # Dat ...