题意:n个数 m次操作
操作分别为
C l r d: 把区间[l, r] 加 d
Q l r : 查询区间[l, r]的和
H l r t: 查询时间t的时候[l, r]的和
B t: 回到时间t

思路:主席树区间修改,区间求和

 const int maxn =  + ;
const int maxnode = * maxn; int n, m; struct Node
{
int l, r;
LL sum, add;
} tr[maxnode];
int tail;
LL a[maxn];
LL qL, qR, value;
LL sum; #define lson tr[o].l,L,M
#define rson tr[o].r,M+1,R
#define self o,L,R
#define lc tr[o].l
#define rc tr[o].r void build(int o, int L, int R)
{
if (L == R)
{
tr[o].sum = a[L];
return;
}
tr[o].l = ++tail;
tr[o].r = ++tail;
int M = L + (R - L) / ;
build(lson);
build(rson);
tr[o].sum = tr[lc].sum + tr[rc].sum;
} void maintain(int o, int L, int R)
{
if (L < R)
{
tr[o].sum = tr[lc].sum + tr[rc].sum;
}
else tr[o].sum = a[L];
tr[o].sum += tr[o].add * (R - L + );
} void update(int& o, int L, int R)
{
tr[++tail] = tr[o];
o = tail;
if (qL <= L && R <= qR)
{
tr[o].add += value;
}
else
{
int M = L + (R - L) / ;
if (qL <= M) update(lson);
if (qR > M) update(rson);
}
maintain(self);
} void query(int o, int L, int R, int add)
{
if (qL <= L && R <= qR)
{
sum += tr[o].sum + add * (R - L + );
return;
}
int M = L + (R - L) / ;
if (qL <= M) query(lson, add + tr[o].add);
if (qR > M) query(rson, add + tr[o].add);
} int root[maxn];//表示第i个线段树的root是什么,显然root[0]=0 void init()
{
memset(tr, , sizeof(tr));
tail = ;
for (int i = ; i <= n; i++)
{
scanf("%lld", &a[i]);
}
root[] = ;
build(, , n);
} void solve()
{
char op[];
int t;
int i = ;
while (m--)
{
op[] = ;
scanf("%s", op);
if (op[] == 'Q')
{
scanf("%lld%lld", &qL, &qR);
sum = ;
query(root[i], , n, );
printf("%lld\n", sum); }
else if (op[] == 'C')
{
i++;
root[i] = root[i-];
scanf("%lld%lld%lld", &qL, &qR, &value);
update(root[i], , n);
}
else if (op[] == 'H')
{
scanf("%lld%lld%d", &qL, &qR, &t);
sum = ;
query(root[t], , n, );
printf("%lld\n", sum);
}
else
{
scanf("%d", &t);
i = t;
}
}
} int main()
{
while (scanf("%d%d", &n, &m) == )
{
init();
solve();
}
return ;
}

hdu 4348 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. hdu 4348 To the moon 主席树区间更新

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

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

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

  9. HDU 4348 To the moon(主席树 区间更新)题解

    题意: 给一个数组A[1] ~ A[n],有4种操作: Q l r询问l r区间和 C l r v给l r区间每个数加v H l r t询问第t步操作的时候l r区间和 B t返回到第t步操作 思路: ...

随机推荐

  1. python collections defaultdict

    class_counts  = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...

  2. Sealed密封类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; #region 概述 //在 ...

  3. Hibernate 中出现 XXXX is not mapped 问题

    1.查询的不是数据库名是实体名…Entity 2.也许是你where条件后的字段名称写错了

  4. 20160928_CentOS6.4x64_Oracle11gR2x64

    1.“Oracle Database 11g Release 2” 下载地址: http://www.oracle.com/technetwork/database/enterprise-editio ...

  5. 谷歌 火狐 CSS兼容

    @media screen and (-webkit-min-device-pixel-ratio:0){}谷歌@-moz-document url-prefix(){}火狐

  6. 防止SVN冲突,Elipse资源同步介绍

    灰色向右箭头: 本地修改了 灰色向右箭头且中间有白色减号: 本地删除了,服务器未删除 灰色向右且中间有个加号的箭头:本地比SVN上多出的文件 蓝色向左箭头:svn上修改过 蓝色向左且中间有个加号的箭头 ...

  7. Sprint2-3.0

    后续安排 第16周 周二晚7点之前将本代码上传到GITHUB. GITHUB地址:https://github.com/QueenIcey/teamwork/tree/master/eslife1 周 ...

  8. Shell脚本的追踪与debug

    [xd502djj@linux ~]#sh [-nvx] scripts.sh 参数: -n:不执行脚本,仅仅查询语法错误 -v:执行脚本前,先将脚本scripts的内容输出到屏幕上 -x:将使用到的 ...

  9. 将FlashPlayerDebugger的trace()功能输出到日志

    1.XP:C:\Documents and Settings\{username} win7:C:\Users\{username} 在以上文件夹下生成mm.cfg文件,写入如下内容: ErrorRe ...

  10. cppcheck 下载与安装(Liunx)

    下载网址:https://sourceforge.net/projects/cppcheck/files/cppcheck/ 选择安装包:cppcheck-1.75.tar.gz 解压安装包:tar ...