codeforces 390E Inna and Large Sweet Matrix
本题的主要算法就是区间更新和区间求和;
可以用线段树和树状数组来做;
感觉线段树写的太麻烦了,看到官方题解上说可以用树状数组做,觉得很神奇,以前用过的树状数组都是单点维护,区间求和的;
其实树状数组还可以区间维护,单点求值;和区间维护,区间求和的;
详情请见博客。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#define maxn 4000010
#define ll long long
using namespace std; ll a[][maxn];
ll b[][maxn]; void add_a(int flag,int x,ll value)
{
while(x>)
{
a[flag][x]+=value;
x-=x&(-x);
}
} void add_b(int flag,int n,int x,ll value)
{
for(int i=x;i<=n;i+=i&(-i))
b[flag][i]+=x*value;
} ll sum_a(int flag,int n,int x)
{
ll sum=;
while(x<=n)
{
sum+=a[flag][x];
x+=x&(-x);
}
return sum;
} ll sum_b(int flag,int x)
{
ll sum=;
while(x>)
{
sum+=b[flag][x];
x-=x&(-x);
}
return sum;
} ll getsum(int flag,int n,int x)
{
if(x)
return sum_a(flag,n,x)*x+sum_b(flag,x-);
else
{
return ;
}
} int main()
{
int n,m,w;
int comand;
int x1,x2,y1,y2;
int v;
scanf("%d%d%d",&n,&m,&w);
for(int i=; i<w; i++)
{
scanf("%d",&comand);
if(comand==)
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);
ll vy=v*(y2-y1+);
ll vx=v*(x2-x1+);
add_a(,x2,vy);
add_b(,n,x2,vy);
if(x1>)
{
add_a(,x1-,-vy);
add_b(,n,x1-,-vy);
}
add_a(,y2,vx);
add_b(,m,y2,vx);
if(y1>)
{
add_a(,y1-,-vx);
add_b(,m,y1-,-vx);
}
}
else
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
ll tmp1=getsum(,n,x2)-getsum(,n,x1-);
ll tmp2=getsum(,m,y1-);
ll tmp3=getsum(,m,m)-getsum(,m,y2);
cout<<tmp1-tmp2-tmp3<<endl;
}
}
return ;
}
codeforces 390E Inna and Large Sweet Matrix的更多相关文章
- CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)
树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且代码量和常数较小 首先定义一个数组 int c[N]; 并清空 memset(c, 0, sizeof c) ...
- Codeforces 390E Inna and Large Sweet Matrix 树状数组改段求段
题目链接:点击打开链接 题意:给定n*m的二维平面 w个操作 int mp[n][m] = { 0 }; 1.0 (x1,y1) (x2,y2) value for i : x1 to x2 for ...
- CF390-E. Inna and Large Sweet Matrix(区间更新+区间查询)
题意很好理解,不说了 题解就是每次把值压缩成一维,比如x上,这样就可以求出任意宽度的整个竖条的和. 如这张图,求的是s5-(s1+s3+s7+s9) 因为可以求出一整竖条和一整横条,我们可以求出是s2 ...
- codeforces C. Inna and Huge Candy Matrix
http://codeforces.com/problemset/problem/400/C 题意:给你一个n*m的矩阵,然后在矩阵中有p个糖果,给你每个糖果的初始位置,然后经过x次顺时针反转,y次旋 ...
- codeforces C. Inna and Huge Candy Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/400/C 题目意思:给出一个n行m列的矩阵,问经过 x 次clockwise,y 次 horizontal ...
- codeforces 390D Inna and Sweet Matrix
几个小结论: 1.路径长度=i+j-1; 2.最简单的走法是先横走再竖着走或者先竖着走再横着走 #include<cstdio> #include<cstring> using ...
- codeforces round #234B(DIV2) C Inna and Huge Candy Matrix
#include <iostream> #include <vector> #include <algorithm> #include <utility> ...
- codeforces 400 C Inna and Huge Candy Matrix【模拟】
题意:给出一个矩形的三种操作,顺时针旋转,逆时针旋转,对称,给出原始坐标,再给出操作数,问最后得到的坐标 画一下模拟一下操作就可以找到规律了 #include<iostream> #inc ...
- codeforces 374A Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行 m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...
随机推荐
- Eclipse中的TreeViewer类和ListViewer类
TreeViewer和TableViewer在使用上还是有很多相似之处.TreeViewer中冶有TableViewer中的过滤器和排序器.具体使用看TableViewer中的使用. 和Table有J ...
- 【转】monkeyrunner学习总结二:连接、安装、启动
一.获取packageName与startActivity 前置条件:已成功安装配置SL4A并连接PC调试(参见:monkeyrunner学习总结一:环境搭建:http://www.amsoft.cn ...
- PHP之数组遍历
数组在PHP中是一个非常强大的武器,用起来方便.容易,由于使用起来异常灵活,用它就可以实现数据结构中的链表.栈.队列.堆以及所谓的字典.集合等,也可以转换成XML格式. 1.使用for for语句遍历 ...
- 【MINA】OrderedThreadPoolExecutor和UnorderedThreadPoolExecutor的事件监听线程池的选择
mina中有两个线程池概念 1.处理监听建立连接的线程池 2.处理读写事件的线程池 本文中主要探讨读写事件的线程池的选择 这两种都经过实际项目的使用和检测,说说优缺点 早期的项目是用Unordere ...
- oracle里面的时间转字符串to_char(),字符串转时间to_date(),以及substr和instr的使用。
工作中编写过的一条语句 select * from Bt_Holiday where to_char(Setting_DATE,'YYYY')=Substr('2015-03-00',1,4) AND ...
- Oracle Varchar2长度 及 PHP 长度判断
oracle数据库相信大家都比较熟悉,数据库中有一种非常常用的数据类型:字符串型. 对应该类型,在oracle中有三种比较常用的类型:varchar2(byte).varchar2( ...
- 解决:The Operation couldn't be completed.(LaunchServicesError error 0.)
问题描述:当运行Xcode6时,编译代码成功,但是登陆模拟器失败,显示错误: The Operation couldn't be completed.(LaunchServicesError erro ...
- spring.net 和 mybatis.net
demo是整合现有的spring.net 和 mybatis.net 完成的控制台程序,需要注意的地方:关于Config文件夹中的config文件的属性设定:同时保证Providers.config默 ...
- Express安装与调试
Express 是基于Node.Js平台,快速.开放.极简的 web 开发框架. 1.安装 Express的安装通过cmd来进行,过程如下: 首先,先在本地建立一个项目文件夹,取名Nodejs. 然后 ...
- JSP九大内置对象(转载)
JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1. ...