HDU4348 To the moon (主席树)
标记永久化,除非想\(MLE\)
忽然感到主席树不过是函数式的树套树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
//#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 100007;
struct ChairmanTree{
int l, r;
long long sum, tag;
}t[N * 40];
int treeIndex, T[N];
inline void Pushup(int &rt, int &l, int &r){
t[rt].sum = t[t[rt].l].sum + t[t[rt].r].sum + 1ll * (r - l + 1) * t[rt].tag;
}
inline void Build(int &rt, int l, int r){
rt = ++treeIndex;
if(l == r){
io >> t[rt].sum;
return;
}
int mid = (l + r) >> 1;
Build(t[rt].l, l, mid), Build(t[rt].r, mid + 1, r);
Pushup(rt, l, r);
}
inline void Updata(int &rt, int pre, int l, int r, int L, int R, long long w) {
rt = ++treeIndex;
t[rt] = t[pre]; // it need to be initialed here
if(L <= l && r <= R){
t[rt].tag += w;
t[rt].sum += 1ll * (r - l + 1) * w;
return;
}
// t[rt] = t[pre]; // not here !
int mid = (l + r) >> 1;
if(L <= mid)
Updata(t[rt].l, t[pre].l, l, mid, L, R, w);
if(R > mid)
Updata(t[rt].r, t[pre].r, mid + 1, r, L, R, w);
Pushup(rt, l, r);
}
inline long long Query(int rt, int l, int r, int L, int R, long long tot){
if(L <= l && r <= R) return t[rt].sum + 1ll * (r - l + 1) * tot;
tot += t[rt].tag;
int mid = (l + r) >> 1;
long long sum = 0;
if(L <= mid)
sum += Query(t[rt].l, l, mid, L, R, tot);
if(R >= mid+1)
sum += Query(t[rt].r, mid+1, r, L, R, tot);
return sum;
}
int tim;
int main(){
FileOpen();
int n, m;
while(~scanf("%d%d", &n, &m)){
treeIndex = tim = 0;
Build(T[tim], 1, n);
while (m--) {
char opt;
for(opt = getchar(); opt != 'C' && opt != 'Q' && opt != 'H' && opt != 'B'; opt = getchar());
if(opt == 'C'){
int l, r, w;
io >> l >> r >> w;
++tim;
Updata(T[tim], T[tim - 1], 1, n, l, r, w);
}
else if(opt == 'Q'){
int l, r;
io >> l >> r;
printf("%lld\n", Query(T[tim], 1, n, l, r, 0));
}
else if(opt == 'H'){
int l, r, edition;
io >> l >> r >> edition;
printf("%lld\n", Query(T[edition], 1, n, l, r, 0));
}
else if(opt == 'B'){
int newTime;
io >> newTime;
tim = newTime;
}
}
}
return 0;
}

HDU4348 To the moon (主席树)的更多相关文章
- [HDU4348]To the moon(主席树+标记永久化)
学可持久化treap的时候才发现自己竟然没写过需要标记下传的主席树,然而现在发现大部分操作都可以标记永久化,下传会增大占用空间. 这题一种写法是和普通的线段树一样标记下传,注意所有修改操作(包括put ...
- hdu4348 To the moon (主席树 || 离线线段树)
Problem Description Background To The Moon is a independent game released in November 2011, it is a ...
- hdu 4348 To the moon (主席树区间更新)
传送门 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q l r:查询当前时间戳区间[l,r]中所有数的和 . (3)H ...
- hdu 4348 To the moon (主席树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...
- HDU 4348 To the moon 主席树 在线更新
http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...
- HDU 4348 To the moon 主席树
题意: 给出一个长度为\(n(n \leq 10^5)\)的序列,最开始时间\(t=0\),支持下面几个操作: \(C \, l \, r \, d\):将区间\([l,r]\)每个数都加上\(d\) ...
- hdu 4348 To the moon 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
- SP11470 TTM - To the moon[主席树标记永久化]
SP11470 TTM - To the moon C l r d:区间 \([L,R]\) 中的数都加 d ,同时当前的时间戳加 1. Q l r:查询当前时间戳区间 \([L,R]\) 中所有数的 ...
- hdu4348区间更新的主席树+标记永久化
http://acm.hdu.edu.cn/showproblem.php?pid=4348 sb的标记永久化即可,刚开始add和sum没复制过来wa了两发...,操作和原来的都一样,出来单点变成区间 ...
- HDU4348To the moon主席树,区间修改
题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q l r:查询当前时间戳区间[l,r]中所有数的和 . (3)H l r ...
随机推荐
- linux系统下文件误删除该如何恢复?
一.linux误删除数据的场景 在实际的工作中,朋友们可能会将linux服务器上的文件不小心误删除掉了.而且越是资历老的工程师越容易犯这样的错误,敲代码的速度也是够快,啪啪rm -rf一个回车,然后就 ...
- 《Unix 网络编程》05:TCP C/S 程序示例
TCP客户/服务器程序示例 系列文章导航:<Unix 网络编程>笔记 目标 ECHO-Application 结构如下: graph LR; A[标准输入/输出] --fgets--> ...
- 论文解读(GCC)《GCC: Graph Contrastive Coding for Graph Neural Network Pre-Training》
论文信息 论文标题:GCC: Graph Contrastive Coding for Graph Neural Network Pre-Training论文作者:Jiezhong Qiu, Qibi ...
- python基础学习6
Python的基础学习6 内容概要 while + else 死循环.while的嵌套 for循环基本使用 range关键字 for循环补充.爬虫 基本数据类型及内置方法 内容详情 while + e ...
- 浏览器代理user-agent
两种方法: 法1:浏览器地址栏输入:about://version,然后复制用户代理: 如果法1不行,法2肯定可以. 法2:打开任意浏览器,输入任意网址,下面以火狐和百度网址为例来进行说明: 打开火狐 ...
- 解决Docker运行命令时提示"Got permission denied while trying to connect to the Docker daemon socket"类情况
Docker安装命令: 解决Docker运行命令时提示"Got permission denied while trying to connect to the Docker daemon ...
- 【NOIP2017 提高组正式赛】列队 题解
题目大意 有一个 \(n\times m\) 的方阵,每次有 \((x,y)\) 离开,离开后有两个命令 向左看齐.这时第一列保持不动,所有学生向左填补空缺.这条指令之后,空位在第 \(x\) 行第 ...
- 关于vue cli 使用iview 自定义主题遇到的坑
定制主题,这里讲变量覆盖 当你老老实实的把上面文档中的代码一一复制粘贴到项目文件中时,发现了还没装less,所以你就 npm install less –savenpm install less-lo ...
- SAP BDC 调用中 金额格式转换
在BDC调用中,由于用户设置不同,导致金额.日期等字段的输入格式不正确.此处给出 自创 金额转换FM 并配有 调用方式. function zgm_conver_cuur. *"------ ...
- SAP 日期计算
1. CONVERSION_EXIT_IDATE_OUTPUT INPUT: 20200601 OUTPUT: 03FEB2008 2. CONVERT_DATE_TO_ ...