Vjudge题面

Time limit 2000 ms

Memory limit 65536 kB

OS Windows

Source 2012 Multi-University Training Contest 5

SPOJ原版题面

Background

To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.

The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we'll give you a chance, to implement the logic behind the scene.

Description

You‘ve been given N integers A[1], A[2],..., A[N]. On these integers, you need to implement the following operations:

  • C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the timestamp by 1, this is the only operation that will cause the timestamp increase.
  • Q l r: Querying the current sum of {Ai | l <= i <= r}.
  • H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
  • B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.

\(N, M ≤ 10^5, |A[i]| ≤ 10^9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10^4\) the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.

Input

n m
A1 A2 ... An...
(here following the m operations. )

Output

... (for each query, simply print the result. )

Example

Input 1:

10 5

1 2 3 4 5 6 7 8 9 10

Q 4 4

Q 1 10

Q 2 4

C 3 6 3

Q 2 4

Output 1:

4

55

9

15

Input 2:

2 4

0 0

C 1 1 1

C 2 2 -1

Q 1 2

H 1 2 1

Output 2:

0

1

吐槽

样例有点弱……传参时\(mid+1\)和\(r\)位置写反了,还过了样例,debug好久……

另外,HDU(连带着vjudge)上的样例是假的,各个case的输出之间不用换行的。换行会PE。

解题思路

题意就是写记录历史版本的线段树,也就是可持久化线段树,那就直接把区间修改的线段树板子拿过来改吧。但写着写着发现lazy标记下放会导致MLE,于是查阅资料,找到解决方案——标记永久化。lazy标记在区间加时不下放,而在区间查询时一路向下累加lazy值,当该区间被全部包含在查询区间中时,把当前区间的sum加上lazy的贡献相加,返回上去。

洛谷题解区看见另一种据说更优的方案——

[SP11470]TTM-To the moon——主席树区间修改/差分主席树里面的法二,用了两棵主席树,没太看懂,先留坑不填了。

源代码

#include <stdio.h>

int T;
int n, m;
long long a[100010]; struct Node
{
int lson, rson;
long long sum, lazy;
} t[4000010];
int cnt = 1, root[100010], timer;
void build(int &x, int l, int r)
{
x = cnt++;
t[x].lazy = 0;
if (l == r)
{
t[x].sum = a[l];
return;
}
int mid = l + r >> 1;
build(t[x].lson, l, mid);
build(t[x].rson, mid + 1, r);
t[x].sum = t[t[x].lson].sum + t[t[x].rson].sum;
} void add(int &x, int last, int l, int r, int al, int ar, int d)
{
if (al > r || ar < l)
return;
x = cnt++;
t[x] = t[last];
if (al <= l && r <= ar)
{
t[x].lazy += d;
t[x].sum += (r - l + 1) * d;
return;
}
int mid = l + r >> 1;
add(t[x].lson, t[last].lson, l, mid, al, ar, d);
add(t[x].rson, t[last].rson, mid + 1,r, al, ar, d);
t[x].sum = t[t[x].lson].sum + t[t[x].rson].sum + t[x].lazy * (r - l + 1);
} long long que(int x, int l, int r, int ql, int qr, long long la)
{
if (ql > r || qr < l)
return 0;
if (ql <= l && r <= qr)
return t[x].sum + la * (r - l + 1);
la += t[x].lazy;
int mid = l + r >> 1;
return que(t[x].lson, l, mid, ql, qr, la) + que(t[x].rson, mid + 1, r, ql, qr, la);
} int main()
{
freopen("test.in", "r", stdin);
//scanf("%d", &T);
//T=1;
//while (T--)
while (~scanf("%d%d", &n, &m))
{
cnt = 1;
timer = 0;
for (int i = 1; i <= n; i++)
scanf("%lld", a + i);
build(root[0], 1, n);
char opt[2];
int l, r, t;
long long d;
while (m--)
{
scanf("%s", opt);
if (opt[0] == 'C')
{
scanf("%d%d%lld", &l, &r, &d);
add(root[timer + 1], root[timer], 1, n, l, r, d);
timer++;
}
else if (opt[0] == 'Q')
{
scanf("%d%d", &l, &r);
printf("%lld\n", que(root[timer], 1, n, l, r, 0));
}
else if (opt[0] == 'H')
{
scanf("%d%d%d", &l, &r, &t);
printf("%lld\n", que(root[t], 1, n, l, r, 0));
}
else
{
scanf("%d", &t);
timer = t;
}
}
//puts("");//hdu样例假了,这句不能要
}
return 0;
}

HDU 4348 SPOJ 11470 To the moon的更多相关文章

  1. HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  2. hdu 4348 To the moon (主席树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...

  3. hdu 4348 To the moon (主席树 区间更新)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4348 题意: 4种操作: C l r c   区间[l,r]加c,时间+1 Q l r    询问当前时 ...

  4. HDU 4348 To the moon 主席树 在线更新

    http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...

  5. HDU 4348 To the moon 可持久化线段树

    To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...

  6. HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和

    To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  7. To the moon HDU - 4348 (主席树,区间修改)

    Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...

  8. hdu 4348 To the moon 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Prob ...

  9. HDU 4348 To the moon(可持久化线段树)

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

随机推荐

  1. HDU4372(第一类斯特林数)

    题意:N座高楼,高度均不同且为1~N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数. 0 < N, F, B <= 2000 首先我们知道一个结论:n的环排列的 ...

  2. 关于Polyaxon的使用

    1.首先upload代码 polyaxon upload 注意,upload只能在新建notebook前进行,且每次只能upload一次,所以在结束每次项目测试(开发)前,都应该在Polyaxon上修 ...

  3. IP地址相关运算(如VLSM,超网汇总)

    1.根据IP地址+子网掩码算出IP地址所在的网段(网络号) 例子: IP地址192.168.10.33,子网掩码为:255.255.255.240 (/28) ,写出所在的网络号 1.得出子网的块大小 ...

  4. 【Qt开发】V4L2 API详解 Camera详细设置

    Camera的可设置项极多,V4L2 支持了不少.但Sam之前对这些设置的用法和涵义都是在看videodev2.h中边看边理解,感觉非常生涩.直到写这篇blog时,才发现v4l2有专门的SPEC来说明 ...

  5. .net core 学习小结之 JWT 认证授权

    新增配置文件 { "Logging": { "IncludeScopes": false, "Debug": { "LogLeve ...

  6. 2019JAVA第一次课程总结

    课程总结:到现在为止之,学习专业课程已有两周了,从刚开始的啥也不懂,现在慢慢入门了.最开始我们为JAVA开发了运行环境,然后使用类编写了最简单的输出,然后开始学习了数据类型,这可以在编程中帮我们解决一 ...

  7. python可视化:matplotlib系列

    matplotlib 的官方文档: https://matplotlib.org/users/index.html 1 子图布局管理 布局参数 紧密布局的方法 坐标轴的公用和隐藏 2 直方图bar和b ...

  8. numpy库中数组的数据类型

    numpy库中数组的数据类型 dtype是一个特殊的对象,它含有ndarray将一块内存解释为特殊数据类型所需要的信息 指定数据类型创建数组 >>> import numpy as ...

  9. ssm中web配置各框架的配置文件路径方式

    一.在web文件中配置 使用逗号隔开 二.在applicationContext.xml文件中配置或引用 以下是引用方式 注: <import />标签要放在所有bean配置的最前面.  

  10. 流畅的Python (Fluent Python) —— 前言

    本书重点: 这本书并不是一本完备的 Python 使用手册,而是会强调 Python 作为编程语言独有的特性,这些特性或者是只有 Python 才具备的,或者是在其他大众语言里很少见的. Python ...