题意很好理解,不说了

题解就是每次把值压缩成一维,比如x上,这样就可以求出任意宽度的整个竖条的和。

如这张图,求的是s5-(s1+s3+s7+s9)

因为可以求出一整竖条和一整横条,我们可以求出是s2+s5+s8 也可以求出s4+s5+s6 当然也很容易求出总面积S

那么S-(s2+s5+s8)-(s4+s5+s6) = s1+s3+s7+s9-s5

对,答案已经出来了,很简单。

以后看到这种求解公式十分奇怪的题,就要算一算是不是能构造出很简单的公式。

注意这题用cin cout 会超时!因为codeforce是单样例,所以都忘了这点,t了好多次……T^T

区间更新+区间查询,线段树+lazy操作 (514ms

#include <stdio.h>

typedef long long ll;

const int N = ;
ll tx[N<<], ty[N<<], fx[N<<], fy[N<<];
int yl, yr, yv;
#define lson (o<<1)
#define rson ((o<<1)+1) void pushdown(ll tr[], ll fg[], int o, int l, int r)
{
if (fg[o]) {
fg[lson] += fg[o];
fg[rson] += fg[o];
int m = (l+r) >> ;
tr[lson] += fg[o] * (m-l+);
tr[rson] += fg[o] * (r-m);
fg[o] = ;
}
} void add(ll tr[], ll fg[], int o, int l, int r)
{
if (l == r) {
tr[o] += yv;
return ;
}
if (yl <= l && yr >= r) {
tr[o] += (ll)yv * (r-l+);
fg[o] += yv;
return ;
}
pushdown(tr, fg, o, l, r);
int m = (l+r) >> ;
if (yl <= m) add(tr, fg, lson, l, m);
if (yr > m) add(tr, fg, rson, m+, r);
tr[o] = tr[lson] + tr[rson];
} ll query(ll tr[], ll fg[], int o, int l, int r)
{
if (l >= yl && r <= yr) {
return tr[o];
}
pushdown(tr, fg, o, l, r);
int m = (l+r) >> ;
ll ans = ;
if (m >= yl) ans += query(tr, fg, o<<, l, m);
if (m < yr) ans += query(tr, fg, (o<<)+, m+, r);
return ans;
} int main()
{ int n, m, w;
scanf("%d%d%d", &n, &m, &w);
int op;
int x1, x2, y1, y2;
ll v;
ll tot, ans;
while (w--) {
scanf("%d%d%d%d%d", &op, &x1, &y1, &x2, &y2);
if (op) {
yl = , yr = n;
tot = query(tx, fx, , , n);
ans = ;
yl = x1, yr = x2;
ans += query(tx, fx, , , n);
yl = y1, yr = y2;
ans += query(ty, fy, , , m);
printf("%lld\n", ans-tot);
} else {
scanf("%lld", &v);
yl = x1, yr = x2, yv = v*(y2-y1+);
add(tx, fx, , , n);
yl = y1, yr = y2, yv = v*(x2-x1+);
add(ty, fy, , , m);
}
}
return ;
}

顺便学习了一下树状数组的区间操作(202ms 时间空间都优于线段树

#include <stdio.h>

typedef long long ll;

const int MAXN = ;

int lowbit(int x) { return x&-x; }

struct tree_array {
struct tree_array_single {
int N;
ll arr[MAXN];
void add(int x, ll v) { while(x <= N) arr[x] += v, x += lowbit(x); }
ll sum(int x) { ll sum = ; while(x) sum+=arr[x], x-=lowbit(x); return sum; }
} T1, T2;
void add(int x, ll v) { T1.add(x, v); T2.add(x, x*v); }
void update(int L, int R, ll v) { add(L, v); add(R+, -v); }
ll sum(int x) { return (x+)*T1.sum(x)-T2.sum(x); }
ll query(int L,int R) { return sum(R)-sum(L-); }
} tx, ty; int main()
{
//freopen("in.txt", "r", stdin);
int n, m, w;
scanf("%d%d%d", &n, &m, &w);
int op;
int x1, x2, y1, y2;
ll v;
tx.T1.N = tx.T2.N = n;
ty.T1.N = ty.T2.N = m;
ll tot = ;
while (w--) {
scanf("%d%d%d%d%d", &op, &x1, &y1, &x2, &y2);
if (op) {
ll ans = tx.query(x1, x2) + ty.query(y1, y2);
printf("%lld\n", ans-tot);
} else {
scanf("%lld", &v);
tot += (y2-y1+)*(x2-x1+)*v;
tx.update(x1, x2, v*(y2-y1+));
ty.update(y1, y2, v*(x2-x1+));
}
}
return ;
}

CF390-E. Inna and Large Sweet Matrix(区间更新+区间查询)的更多相关文章

  1. codeforces 390E Inna and Large Sweet Matrix

    本题的主要算法就是区间更新和区间求和: 可以用线段树和树状数组来做: 感觉线段树写的太麻烦了,看到官方题解上说可以用树状数组做,觉得很神奇,以前用过的树状数组都是单点维护,区间求和的: 其实树状数组还 ...

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

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

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

  4. hdu1698线段树的区间更新区间查询

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  6. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  7. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  8. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

随机推荐

  1. 漫话C++0x(五)—- thread, mutex, condition_variable

    熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不 ...

  2. POJ 3393 Lucky and Good Months by Gregorian Calendar

    http://poj.org/problem?id=3393 题意 : 对于这篇长长的英语阅读,表示无语无语再无语,花了好长时间,终于读完了.题目中规定每周的周六日为假日,其他为工作日,若是一个月的第 ...

  3. c++ 基础学习: 左值 概念cocos2d-x3.0的实际应用

    左值:概念baidu 1.2.6.2 与Cocos2d-x内存管理的结合 在2.x的使用场景中,CCArray和CCDictionary通常被分配在堆上,我们不得不需要考虑在适当的地方释放其内存.新的 ...

  4. HeadFirst设计模式之状态模式

    一. 1. 2.The State Pattern allows an object to alter its behavior when its internal state changes. Th ...

  5. HeadFirst设计模式之工厂模式

    一. 1."工厂模式"不是种真正的设计模式,而是一种编程术语 2.The Factory Method Pattern defi nes an interface for crea ...

  6. Android ActionBar隐藏修改图标和标题

    有时候在一些子页面或者内容页面,不需要显示ActionBar的标题栏图标.可用如下方式进行设置. 首先获取到ActionBar对象 ActionBar actionBar=getActionBar() ...

  7. 解决git Push时请求username和password,而不是ssh-key验证

    转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...

  8. 【HDOJ】4029 Distinct Sub-matrix

    思路是枚举矩阵列数,然后将字符矩阵转换成字符串,通过字符数组求不同子串数目.最后,减去不成立的情况.使用特殊字符分割可能的组合. /* 4029 */ #include <iostream> ...

  9. UML中常见关系详解(泛化、实现、依赖、关联、组合、聚合)

    UML中类与类,已经类与接口,接口与接口的关系有:泛化(generalization),关联(association),依赖(dependency),实现(realization)这几种.   泛化( ...

  10. redis作为mysql的缓存服务器(读写分离,通过mysql触发器实现数据同步)

    一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...