题目描述

给定一段长度为 \(N\) 的序列 \(a\) 以及 \(M\) 次操作,操作有以下几种:

  • C l r d :将区间 \([l,r]\) 中的数都加上 \(d\)
  • Q l r :查询当前时间戳区间 \([l,r]\) 中所有数的和
  • H l r t :查询时间戳为 \(t\) 时,区间 \([l,r]\) 中所有数的和
  • B t :将当前时间戳重置为 \(t\)

对于每一次询问操作,输出一行对应的答案。

时间戳初始值为 \(0\)

数据范围:

\(1\leq N,M\leq 10^5\)

然后要开 long long


基本思路

主席树的一道比较模板的题。。。

原谅我不想讲细节(其实代码里面写的还可以)

关于标记永久化的一些实现细节,我是参考的《信息学奥赛一本通 \(\cdot\) 提高篇》


细节注意事项

  • long long一定要开
  • 细节不要写挂,数据结构题还是有难调的
  • 应该不会有人像我一样搞错输入顺序吧。。。

参考代码

/*--------------------------------
Author: The Ace Bee
Blog: www.cnblogs.com/zsbzsb
This code is made by The Ace Bee
--------------------------------*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} typedef long long LL;
const int _ = 100010; int n, m, a[_];
int tot, rt[_], lc[_ << 5], rc[_ << 5];
LL sum[_ << 5], add[_ << 5]; inline void build(int& p, int l = 1, int r = n) {
p = ++tot;
if (l == r) return read(sum[p]);
int mid = (l + r) >> 1;
build(lc[p], l, mid), build(rc[p], mid + 1, r);
sum[p] = sum[lc[p]] + sum[rc[p]];
} inline void update(int& p, int q, int ql, int qr, int v, int l = 1, int r = n) {
lc[p = ++tot] = lc[q], rc[p] = rc[q], add[p] = add[q], sum[p] = sum[q];
if (ql <= l && r <= qr) { add[p] += v; return; }
int mid = (l + r) >> 1;
if (ql <= mid) update(lc[p], lc[q], ql, qr, v, l, mid);
if (qr > mid) update(rc[p], rc[q], ql, qr, v, mid + 1, r);
sum[p] += 1ll * v * (min(qr, r) - max(ql, l) + 1);
} inline LL query(int p, int ql, int qr, int l = 1, int r = n) {
if (ql <= l && r <= qr) return sum[p] + 1ll * add[p] * (r - l + 1);
int mid = (l + r) >> 1; LL res = 0;
if (ql <= mid) res += query(lc[p], ql, qr, l, mid);
if (qr > mid) res += query(rc[p], ql, qr, mid + 1, r);
return res + 1ll * add[p] * (min(qr, r) - max(ql, l) + 1);
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m), build(rt[0]);
int tim = 0;
for (rg int i = 1; i <= m; ++i) {
char s[5]; scanf("%s", s);
if (s[0] == 'B') read(tim), tot = rt[tim + 1] - 1;
else if (s[0] == 'C') { int ql, qr, v;
read(ql), read(qr), read(v), ++tim;
update(rt[tim], rt[tim - 1], ql, qr, v);
} else if (s[0] == 'Q') { int ql, qr;
read(ql), read(qr);
printf("%lld\n", query(rt[tim], ql, qr));
} else if (s[0] == 'H') { int t, ql, qr;
read(ql), read(qr), read(t);
printf("%lld\n", query(rt[t], ql, qr));
}
}
return 0;
}

「SP11470」TTM - To the moon的更多相关文章

  1. SP11470 TTM - To the moon[主席树标记永久化]

    SP11470 TTM - To the moon C l r d:区间 \([L,R]\) 中的数都加 d ,同时当前的时间戳加 1. Q l r:查询当前时间戳区间 \([L,R]\) 中所有数的 ...

  2. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  3. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  4. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  5. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  6. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  7. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  8. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

  9. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

随机推荐

  1. Multism中的一些特殊元器件在哪里找

    1.TLP521-1(光耦)在哪里找 2.单刀双掷开关 3.数码管 indicator:指示器 SEVEN_SEG_DECIMAL_COM_A_BULE: 七段带小数点共阳极,蓝色显示 A:阳极 K: ...

  2. Go 后端主要做什么

    漫谈 Go 语言后端开发 :https://blog.csdn.net/u010986776/article/details/87276303 Golang 资深后端工程师要了解的知识:https:/ ...

  3. LeetCode 42接雨水 按行求解(差分+排序)

    按行求解的思路比较清晰明了,但是这个方法的复杂度高达O(heightSize*sum(height[i])),几乎高达O(N^2). 但是也并不是不可以解决,经观察我们可以发现,这个算法的缺点在于要遍 ...

  4. appium常用的类库、对应的方法和属性

    1.driver.swipe(self, start_x, start_y, end_x, end_y, duration=None) tart_x - 滑动开始x轴坐标 start_y - 滑动开始 ...

  5. Bug搬运工-CSCvf74866:Webauth scaling improvements - fix problem with GUI going unresponsive with HTTPS redirect

    Webauth scaling improvements - fix problem with GUI going unresponsive with HTTPS redirect CSCvf7486 ...

  6. javaweb项目运转流程

    做web项目,不仅要会做,还需要了解其工作流程,为什么这么做!这些知道了.其他的都是渣渣.上图!对于web 项目了解他的运行流程之后,基本其他的都不是问题.web项目还是很简单的 这是简化的开发时常用 ...

  7. 实现在vue中element-ui的el-dialog弹框拖拽

    参考:实现在vue中element-ui的el-dialog弹框拖拽 1.在 utils 中新建 directives.js 文件 import Vue from 'vue' // v-dialogD ...

  8. Laravel Vuejs 实战:开发知乎 (9)定义话题与问题关系

    1.话题[Topic] 执行命令: php artisan make:model Topic –cmr 修改****_**_**_create_topics_table.php数据库迁移文件如下: c ...

  9. Java面向对象封装优化1_this(Python中的self)

    1. 类 package cn.itcast.day06.demo03; /* 问题描述:定义Person的年龄时,无法阻止不合理的数值被设置进来. 解决方案:用private关键字将需要保护的成员变 ...

  10. HTML-移动端-rem px vw vh 的转换

    vw/vh rem px 三者的转换(快速入门移动端页面编写) 1:三种单位的转换 2:如何适配移动端的不同设备 前提知识: 手机端的长宽是实际设计过程中的两倍 比如手机端是 750 * 1200 那 ...