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 移动 ...
随机推荐
- nodejs中npm工具自身升级
npm官方地址:https://www.npmjs.com/ npm在github上的地址:https://github.com/npm/npm 目前(截止2016-03-15)npm最新版本为3.8 ...
- Microsoft Visual Studio 2013 Update 1 离线安装程序
☆ 微软官网地址:☆http://www.microsoft.com/zh-cn/download/details.aspx?id=41650☆ 离线安装程序 直接下载链接:☆http://downl ...
- Java简单算法--求100以内素数
package cn.magicdu.algorithm; /** * 打印素数 * * @author xiaoduc * */ public class Prim { public static ...
- win32项目设置窗口全屏
创建窗口的时候设置样式:CreateWindow(,,WS_MAXIMIZE,……): 显示窗口的时候设置:ShowWindow(hWnd,SM_MAXIMIZE);
- web开发常用图片格式
web开发常用图片格式有:gif jpg/jpeg png gif:图片压缩率高,可以显示动画,但是只能显示256色,可能造成颜色丢失. jpg:图片压缩率高(有损压缩),可以用小文件来显示 ...
- html-----020----事件
html事件 <body> <a href="http://www.cctv.com" accesskey="k" target=" ...
- OC6_字符串练习
// // QFString.h // OC6_字符串练习 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhang ...
- Java对象的序列化与反序列化:默认格式及JSON格式(使用jackson)
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3558663.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- OpenJudge/Poj 2013 Symmetric Order
1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...
- 避免ajax请求过多,导致内存溢出,请求之后回收资源
php试题网 http://phpshiti.com/ http://www.jb51.net/article/30458.htm success: function (data, textStatu ...