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 j : y1 to y2
mp[i][j] += value;
2、1 (x1, y1) (x2 y2)
ans1 = 纵坐标在 y1,y2间的总数
ans2 = 横坐标不在x1,x2间的总数
puts(ans1-ans2);
more format:
for i : 1 to n
for j : y1 to y2
ans1 += mp[i][j]
由于n最大是4e6, 所以用树状数组改段求段取代线段树
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if (x>9) pt(x / 10);
putchar(x % 10 + '0');
}
using namespace std;
typedef long long ll;
const int N = 4e6 + 100;
template<class T>
struct Tree{
T c[2][N];
int maxn;
void init(int x){
maxn = x+10; memset(c, 0, sizeof c);
}
inline int lowbit(int x){ return x&-x; }
T sum(T *b, int x){
T ans = 0;
if (x == 0)ans = b[0];
while (x)ans += b[x], x -= lowbit(x);
return ans;
}
void change(T *b, int x, T value){
if (x == 0)b[x] += value, x++;
while (x <= maxn)b[x] += value, x += lowbit(x);
}
T get_pre(int r){
return sum(c[0], r) * r + sum(c[1], r);
}
void add(int l, int r, T value){
change(c[0], l, value);
change(c[0], r + 1, -value);
change(c[1], l, value * (-l + 1));
change(c[1], r + 1, value * r);
}
T get(int l, int r){
return get_pre(r) - get_pre(l - 1);
}
};
Tree<ll> x, y;
int main(){
int n, m, w;
rd(n); rd(m); rd(w);
x.init(n); y.init(m);
ll all = 0;
while (w--){
int op, x1, x2, y1, y2; ll value;
rd(op); rd(x1); rd(y1); rd(x2); rd(y2);
if (op == 0)
{
rd(value);
all += value * (x2 - x1 + 1) * (y2 - y1 + 1);
x.add(x1, x2, value * (y2 - y1 + 1));
y.add(y1, y2, value * (x2 - x1 + 1));
}
else
{
pt(y.get(1, y2) - y.get(1, y1 - 1) - (all - x.get(1, x2) + x.get(1, x1 - 1))); puts("");
}
}
return 0;
}
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
本题的主要算法就是区间更新和区间求和: 可以用线段树和树状数组来做: 感觉线段树写的太麻烦了,看到官方题解上说可以用树状数组做,觉得很神奇,以前用过的树状数组都是单点维护,区间求和的: 其实树状数组还 ...
- CF390-E. Inna and Large Sweet Matrix(区间更新+区间查询)
题意很好理解,不说了 题解就是每次把值压缩成一维,比如x上,这样就可以求出任意宽度的整个竖条的和. 如这张图,求的是s5-(s1+s3+s7+s9) 因为可以求出一整竖条和一整横条,我们可以求出是s2 ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...
- Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...
- poj 2155 Matrix (树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16797 Accepted: 6312 Descripti ...
随机推荐
- 静态变量(static)的特点
静态变量(static):有局部变量,也有全局变量. 静态局部变量:在函数体内用static说明的变量称为静态局部变量,属于静态类别. 特点: (1)它占据一个永久性的存储单元.随着文件的存在而存在. ...
- Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version
解决方法:
- 如何使用Ext.create() 调用一个窗体
Ext.define("Scripts.Code.QM.OutgoingQuality.OQC.ReinspRequest.view.DefectContentsDetailInfoWind ...
- Linux(三) 一些命令
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- javascript中各种继承方式的优缺点
javascript中实现继承的方式有很多种,一般都是通过原型链和构造函数来实现.下面对各种实现方式进行分析,总结各自的优缺点. 一 原型继承 let Super = functioin(name = ...
- Fresco对Listview等快速滑动时停止加载
Fresco中在listview之类的快速滑动时停止加载,滑动停止后恢复加载: 1.设置图片请求是否开启 // 暂停图片请求 public static void imagePause() { Fre ...
- pandas常用操作
删除某列: concatdfs.drop('Unnamed: 0',axis=1) 打印所有列名: .columns
- xampp 安装 mysql-python
在已经安装brew前提下:brew install mysql-connector-c pip install MySQL-python
- 算法之美--1.蒙特卡洛方法计算pi
基本思想: 利用圆与其外接正方形面积之比为pi/4的关系,通过产生大量均匀分布的二维点,计算落在单位圆和单位正方形的数量之比再乘以4便得到pi的近似值.样本点越多,计算出的数据将会越接近真识的pi(前 ...
- 【开发总结】—— BABYLON 项目开发必备系列
前言:在公司主要使用Babylon.js作为前端引擎,我自己在开发中总结到基本上每一个新项目都会需要这些基本设置. 一.ios兼容:解决镜面反射 如果不加offline,材质中有镜面反射,苹果手机 ...