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 移动 ...
随机推荐
- 今天学习image在html中的应用
今天学习image在html中的应用 上次在学习超级链接的使用中有一小问题,是在添加网址中href="http://www.baidu.com" 中不能忘记http://,否则链接 ...
- CPrintDialog
CPrintDialog 封装windows为打印提供服务的通用窗体. BOOL GetDefaults(); //获取默认设备,不显示对话框 // Helpers for parsing infor ...
- oc 通过webView调用js方法
- (void)viewDidLoad { [super viewDidLoad]; //加载本地web页面 web = [[UIWebView alloc]init]; web.background ...
- Server Error The server encountered an error and could not complete your request. 新建站点模版失败
500 Server Error Error: Server Error The server encountered an error and could not complete your req ...
- CSS text-indent
text-indent 属性规定文本块中首行文本的缩进. 一个作用就是首行文本缩进,一般的文本都是首行缩进两个字符,这里就可以使用text-indent { text-indent: 2em; } 另 ...
- tomcat服务器启动错误
有的时候,启动tomcat,显示的他已经启动了,但是就是没有加载任何工程,最后页面报404错误. 这时候,可以试着把服务器Clean一下
- HDU 4430 Yukari's Birthday(二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题目大意:给定n个蜡烛,围绕蛋糕的中心插同心圆,从里往外分别是第1圈.第2圈....第r圈,第 ...
- STL标准模板库 向量容器(vector)
向量容器使用动态数组存储.管理对象.因为数组是一个随机访问数据结构,所以可以随机访问向量中的元素.在数组中间或是开始处插入一个元素是费时的,特别是在数组非常大的时候更是如此.然而在数组末端插入元素却很 ...
- JSON parser error with double quotes
Use backslash charater \ to escape double quotes in JSON file as below example. { "myInfo" ...
- (转)使用inotify、inotify_add_watch、inotify_rm_watch、read编写监控程序
转自:http://blog.csdn.net/myarrow/article/details/7096460 inotify是什么 inotify是文件系统变化通知机制,在监听到文件系统变化后,会向 ...