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 ...
随机推荐
- php通过$_SERVER['HTTP_USER_AGENT']获取浏览器相关参数
最近不忙,同事在忙一个app项目.当听到领导安排让他做一个判断苹果还是安卓手机,如果是安卓手机下载安卓app.如果是苹果手机下载苹果app;然后我就上网搜了一下学习学习: php通过$_SERVER[ ...
- 关于Integer的parseInt(String s, int radix)方法的使用
我们平时用到Integer.parseInt("123");其实默认是调用了int i =Integer.parseInt("123",10); 其中10代表的 ...
- LeetCode OJ-- Interleaving String **@
https://oj.leetcode.com/problems/interleaving-string/ 刚开始用递归做,但是超时了 class Solution { public: bool fl ...
- 参数化2--CSV Data Set Config 参数化配置
众所周知,在进行接口测试的过程中,需要创建不同的场景(不同条件的输入,来验证不同的入参的返回结果).因而,在日常的自动化接口监控或商品监控等线上监控过程中,需要配置大量的入参来监控接口的返回是否正确. ...
- Java NIO.2 使用Files类遍历文件夹
在以前的Java版本中,如果要遍历某个文件夹下所有的子文件.子文件夹,需要我们自己写递归,很麻烦. 在Java7以后,我们可以NIO.2中的Files工具类来遍历某个文件夹(会自动递归). 大致用法: ...
- Spring MVC通过Pageable对象和PageableDefault注解获取分页信息(MongoDB通过Pageable来操作分页)
说明:Pageable同时也能用于操作MongoDB的分页. PageableSpring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息( ...
- ASP.NET 5已终结,迎来ASP.NET Core 1.0和.NET Core 1.0 转
作者:yourber 命名是非常困难的事情,微软这次为了和ASP.NET4.6做区分,采用了全新的命名方式ASP.NET Core 1.0,它是一个全新的框架. ASP.NET 在过去的 15 年里是 ...
- Concurrency(Locking, Blocking and Row Versioning)
https://www.simple-talk.com/sql/t-sql-programming/row-versioning-concurrency-in-sql-server/?utm_sour ...
- iOS使用MD5 - 字符串加密至MD5&获取文件MD5
iOS 字符串加密至MD5 + (NSString *) md5:(NSString *)str { unsigned ]; CC_MD5( cStr, strlen(cStr), result ); ...
- 【IntelliJ IDEA】代码中出现Usage of API documented as @since 1.8+ more..
在idea中写代码过程中.有这种报错出现: Usage of API documented as @since 1.8+ more.. 修改JDK版本的几个地方 最后,在pom.xml文件中添加: & ...