本题的主要算法就是区间更新和区间求和;

可以用线段树和树状数组来做;

感觉线段树写的太麻烦了,看到官方题解上说可以用树状数组做,觉得很神奇,以前用过的树状数组都是单点维护,区间求和的;

其实树状数组还可以区间维护,单点求值;和区间维护,区间求和的;

详情请见博客

#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的更多相关文章

  1. CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)

    树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且代码量和常数较小 首先定义一个数组 int c[N]; 并清空 memset(c, 0, sizeof c) ...

  2. 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 ...

  3. CF390-E. Inna and Large Sweet Matrix(区间更新+区间查询)

    题意很好理解,不说了 题解就是每次把值压缩成一维,比如x上,这样就可以求出任意宽度的整个竖条的和. 如这张图,求的是s5-(s1+s3+s7+s9) 因为可以求出一整竖条和一整横条,我们可以求出是s2 ...

  4. codeforces C. Inna and Huge Candy Matrix

    http://codeforces.com/problemset/problem/400/C 题意:给你一个n*m的矩阵,然后在矩阵中有p个糖果,给你每个糖果的初始位置,然后经过x次顺时针反转,y次旋 ...

  5. codeforces C. Inna and Huge Candy Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/400/C 题目意思:给出一个n行m列的矩阵,问经过 x 次clockwise,y 次 horizontal ...

  6. codeforces 390D Inna and Sweet Matrix

    几个小结论: 1.路径长度=i+j-1; 2.最简单的走法是先横走再竖着走或者先竖着走再横着走 #include<cstdio> #include<cstring> using ...

  7. codeforces round #234B(DIV2) C Inna and Huge Candy Matrix

    #include <iostream> #include <vector> #include <algorithm> #include <utility> ...

  8. codeforces 400 C Inna and Huge Candy Matrix【模拟】

    题意:给出一个矩形的三种操作,顺时针旋转,逆时针旋转,对称,给出原始坐标,再给出操作数,问最后得到的坐标 画一下模拟一下操作就可以找到规律了 #include<iostream> #inc ...

  9. codeforces 374A Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行  m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...

随机推荐

  1. js实现过滤重复字符和重复数组-javascript技巧

    js实现过滤重复字符 <script type="text/javascript"> <!-- String.prototype.noRepeatStr=func ...

  2. 【转】MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    [转]MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据, ...

  3. java web项目中classes文件夹下的class和WEB-INF/lib中jar里的class文件加载顺序

    如果是发布到weblogic的话,可以在WebContent\WEB-INF\weblogic.xml里面配置.参考配置如下:<?xml version="1.0" enco ...

  4. ASPxGridView动态创建表格列编辑模板

    在项目中用到了DevExpress的ASPxGridview控件,每每去配置它的时候,总感觉很是啰嗦,于是想到了用代码自动配置. 于是有了这样的代码: foreach (ZiyuWeb.Entity. ...

  5. java scoket (UDP通信模型)简易聊天室

    import java.net.DatagramPacket; import java.net.DatagramSocket; /** * 接收线程 * * @author xiaoduc * */ ...

  6. ubuntu系统安装flashplayer

    打开浏览器,输入adobe flashplayer 进入官方网站,下载Linux 32-bit, 简体中文, Firefox,下载.tar.gz包. 然后点击立即下载.下载之后找到解压该文件夹,找到 ...

  7. C# 高精度减法 支持小数(待优化)

    是现实思路 1,先小数点补位,8913758923475893274958738945793845-4893127498372459823745324532453245.284929384729837 ...

  8. ifstream:incomplete type is not allowed

    IntelliSense: incomplete type is not allowed ifstream inputFile; Need to add this: #include <fstr ...

  9. forever start Error: Cannot find module './daemon.v0.10.26'

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3590158.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  10. Poj 1503 Integer Inquiry

    1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS   Memory Limit: 1000 ...