题面

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. PHP die与exit的区别

    最近听见有人说die和exit区别,bula~bula.决心一探究竟. 翻了翻PHP 5.6的源码(源码的位置为zend目录下zend_language_scanner.l大约是1014~1020行) ...

  2. CENTOS6.6上搭建单实例ORACLE12C

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 自己在centos6.6上搭建的单实例oracle12c 由 ...

  3. HashMap中的散列函数、冲突解决机制和rehash

    一.概述 散列算法有两个主要的实现方式:开散列和闭散列,HashMap采用开散列实现. HashMap中,键值对(key-value)在内部是以Entry(HashMap中的静态内部类)实例的方式存储 ...

  4. Halcon一日一练:图像拼接技术

    图像拼接技术就是针对同一场景的一系列图片,根据图片的特征,比如位置,重叠部分等,拼接成一张大幅的宽视角的图像. 图像拼接要求拼接后图像最大程度的与原图一致,失真尽可能的小,并且要尽量做到天衣无缝即没有 ...

  5. [翻译]编写高性能 .NET 代码 第一章:性能测试与工具 -- 平均值 vs 百分比

    <<返回目录 平均值 vs 百分比 在考虑要性能测试的目标值时,我们需要考虑用什么统计口径.大多数人都会首选平均值,但在大多数情况下,这个正确的,但你也应该适当的考虑百分数.但你有可用性的 ...

  6. Xen的虚拟化详解

    最近在看Xen在2003年发表在sosp上的论文<Xen and the Art of Virtualization>,中途遇到一些不理解的技术点,在网络上查找相关资料,发现大多数人都只是 ...

  7. spring core 与 context理解

    Spring core是核心层,拥有这BeanFactory这个强大的工厂,是所有bean的管理器: 而spring context是上下文运行环境,基于spring core之上的一个架构, 它之上 ...

  8. ListIterator的使用

    package cn.lonecloud.Iterator; import java.util.ArrayList; import java.util.ListIterator; public cla ...

  9. linux 获取CPU个数

    #include<stdio.h> #include<unistd.h> int main() { int cpu_num; cpu_num = sysconf(_SC_NPR ...

  10. 使用Git的hook实现代码的自动部署

    这个功能非常的好用,可以省去诸多麻烦!我自己也是摸索了好久,才完全掌握的.希望能对大家有所帮助! 1,首先在我的阿里云服务器上已经创建好了一个代码远程的管理仓库,/srv/cmp.git 2, 在服务 ...