题面

vjudge

Sol

主席树模板

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);
const int __(5e6); IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int rt[_], tot, ls[__], rs[__];
ll add[__], sum[__];
int n, m; IL void Build(RG int &x, RG int l, RG int r){
x = ++tot;
if(l == r){
sum[x] = Input();
return;
}
RG int mid = (l + r) >> 1;
Build(ls[x], l, mid), Build(rs[x], mid + 1, r);
sum[x] = sum[ls[x]] + sum[rs[x]];
} IL void Modify(RG int &x, RG int l, RG int r, RG int L, RG int R, RG ll ad){
ls[++tot] = ls[x], rs[tot] = rs[x], sum[tot] = sum[x], add[tot] = add[x];
x = tot;
RG int len = min(r, R) - max(l, L) + 1;
sum[x] += 1LL * ad * len;
if(L <= l && R >= r){
add[x] += ad;
return;
}
RG int mid = (l + r) >> 1;
if(L <= mid) Modify(ls[x], l, mid, L, R, ad);
if(R > mid) Modify(rs[x], mid + 1, r, L, R, ad);
} IL ll Query(RG int x, RG int l, RG int r, RG int L, RG int R, RG ll ad){
if(L <= l && R >= r) return sum[x] + 1LL * ad * (r - l + 1);
ad += add[x];
RG int mid = (l + r) >> 1; RG ll ret = 0;
if(L <= mid) ret = Query(ls[x], l, mid, L, R, ad);
if(R > mid) ret += Query(rs[x], mid + 1, r, L, R, ad);
return ret;
} int main(RG int argc, RG char* argv[]){
n = Input(), m = Input();
Build(rt[0], 1, n);
for(RG int i = 1, now = 0, x, y, z; i <= m; ++i){
RG char op; scanf(" %c", &op);
if(op == 'C'){
++now, rt[now] = rt[now - 1];
x = Input(), y = Input(), z = Input();
Modify(rt[now], 1, n, x, y, z);
}
else if(op == 'Q'){
x = Input(), y = Input();
printf("%lld\n", Query(rt[now], 1, n, x, y, 0));
}
else if(op == 'H'){
x = Input(), y = Input(), z = Input();
printf("%lld\n", Query(rt[z], 1, n, x, y, 0));
}
else now = Input();
}
return 0;
}

SPOJ:To the moon的更多相关文章

  1. 2018.08.04 spoj TTM to the moon(主席树)

    spoj传送门 vjudge传送门 主席树板子题. 支持历史版本的区间和,区间和,区间修改和时光倒流. 其中新奇一点的也只有区间修改了,这个东西直接标记永久化就行了. 如果想下传标记的话也行,需要在p ...

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

  3. SPOJ:Free tour II (树分治+启发式合并)

    After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...

  4. SPOJ:NPC2016A(数学)

    http://www.spoj.com/problems/NPC2016A/en/ 题意:在一个n*n的平面里面,初始在(x,y)需要碰到每条边一次,然后返回(x,y),问最短路径是多长. 思路:像样 ...

  5. HDU 4348 SPOJ 11470 To the moon

    Vjudge题面 Time limit 2000 ms Memory limit 65536 kB OS Windows Source 2012 Multi-University Training C ...

  6. HDU4348:To the moon

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227. ...

  7. SPOJ:D-query(非常规主席树求区间不同数的个数)

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...

  8. SPOJ:House Fence(分治&DP)

    "Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...

  9. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

随机推荐

  1. Docker第一弹:下载运行hello-world程序

    1.需要安装好docker程序 没有安装的请看在centos 6.8下安装docker 2.从docker镜像仓库中拉去hello-world镜像 docker pull hello-world 3. ...

  2. JavaScript那些事

    1.定义静态常量:     const C=1; 该常量不能变化的. 2.在if判断中如果是两个变量比较js会将一个字符和一个数字比较的话,会将字符转换成数字然后在对这两个进行对比: var num= ...

  3. 初识Vue——计算属性和观察者

    一.计算属性 在模板内使用 1.基础例子 <template> <div class="main"> <div id="reverse_st ...

  4. Node.js的下载、安装、配置、Hello World、文档阅读

    Node.js的下载.安装.配置.Hello World.文档阅读

  5. 进入Docker容器

    在进入Docker容器之前,首先要运行对应的Docker容器,先使用命令docker ps查看正在运行的容器. docker inspect --format='{{.NetworkSettings. ...

  6. 使用org.apache.commons.logging打日志注意事项

    使用方法:例如,protected final Log logger = LogFactory.getLog(getClass());if (logger.isDebugEnabled()) { lo ...

  7. MySQL基础学习笔记

    一.数据库基本概念 1.1 数据库 - Database 从字面意思看,数据库就是一个存储数据的仓库.从计算机的角度来讲,数据库(Datebase)是按照数据结构来组织.存储和管理数据的仓库. 简单来 ...

  8. ActiveMq笔记3-AMQ高可用性理论

    单点的ActiveMQ作为企业应用无法满足高可用和集群的需求,所以ActiveMQ提供了master-slave.broker cluster等多种部署方式,但通过分析多种部署方式之后我认为需要将两种 ...

  9. Lua Table转C# Dictionary

    因为在游戏公司做web后台开发,经常会涉及到取游戏服务器的数据库里面读写各种操作. 昨天下午,服务器那边让我读一个配置显示到后台,让运营大佬们可以在web后台配置游戏参数. 本来以为很简单个事情,结果 ...

  10. jqeury显示前几个,隐藏后几个,点击后隐藏前几个显示后几个

    <script type="text/javascript"> $(".ul li").each(function(){ if($(this).in ...