队友套的可持续化线段树,徘徊在RE和MLE之间多发过的。。。

复用结点新的线段树平均要log2N个结点。

其实离线就好,按照时间顺序组织操作然后dfs。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const int maxn = ;
int n, m;
struct Node {
long long d, td;
int timelabel;
Node() {};
Node(long long _d, long long _td, int t) {
d = _d; td = _td; timelabel = t;
}
};
vector<Node> bit[maxn];
long long a[maxn], sum[maxn] = {}; inline int lowbit(int x)
{
return x & (-x);
} inline void update(int x, long long w, int timelabel)
{
int size;
long long tw = w * x;
while (x <= n) {
size = bit[x].size();
bit[x].push_back(bit[x][size-]);
size++;
bit[x][size-].d += w;
bit[x][size-].td += tw;
bit[x][size-].timelabel = timelabel;
x += lowbit(x);
}
} inline long long getsum(int x, int timelabel)
{
long long sd = , std = ;
int size, ox = x;
while (x > ) {
size = bit[x].size();
while (bit[x][size-].timelabel > timelabel) {
size--;
}
sd += bit[x][size-].d;
std += bit[x][size-].td;
x -= lowbit(x);
}
return (ox + ) * sd - std;
} inline long long getans(int x, int y, int timelabel)
{
return sum[y] - sum[x-] + getsum(y, timelabel) - getsum(x - , timelabel);
} inline void back(int timelabel)
{
int size;
for (int i = ; i <= n; ++i) {
size = bit[i].size();
while (bit[i][size-].timelabel > timelabel) {
bit[i].pop_back();
size--;
}
}
} void build()
{
for (int i = ; i <= n; ++i) {
bit[i].clear();
bit[i].push_back(Node(0LL, 0LL, ));
}
} int main()
{
//freopen("in.txt","r",stdin);
int cas = ;
while (scanf("%d%d", &n, &m) != EOF) {
if (cas++) printf("\n");
for (int i = ; i <= n; ++i) {
scanf("%lld\n", &a[i]);
sum[i] = sum[i-] + a[i];
}
char op[];
int x, y, z;
int timelabel = ;
build();
while (m--) {
scanf("%s", op);
if (op[] == 'Q') {
scanf("%d%d", &x, &y);
printf("%lld\n", getans(x, y, timelabel));
} else if (op[] == 'C') {
scanf("%d%d%d", &x, &y, &z);
timelabel++;
update(x, z, timelabel);
update(y + , -z, timelabel);
} else if (op[] == 'H') {
scanf("%d%d%d", &x, &y, &z);
printf("%lld\n", getans(x, y, z));
} else {
scanf("%d", &x);
timelabel = x;
back(x);
}
}
}
return ;
}

HDU 4348 I - To the moon 可持续化的更多相关文章

  1. 【HDU - 4348】To the moon(主席树在线区间更新)

    BUPT2017 wintertraining(15) #8G 题意 给一个数组a,有n个数,m次操作.\(N, M ≤ 10^5, |A i| ≤ 10^9, 1 ≤ l ≤ r ≤ N, |d| ...

  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 SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))

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

  4. 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    询问当前时 ...

  5. HDU 4348 To the moon 主席树 在线更新

    http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...

  6. HDU 4348 SPOJ 11470 To the moon

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

  7. HDU 4348 To the moon 可持久化线段树

    To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...

  8. HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和

    To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  9. To the moon HDU - 4348 (主席树,区间修改)

    Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...

随机推荐

  1. IP地址库解析——读取IP地址获得实际地理位置信息的java源码实现

    说明:IP地址库来自QQwry.dat数据库文件,通过解析地址库当中的ip,已经细化最后获取的信息:获取ip地址对应的:国家 / 省 / 市 / 运营商ISP信息. 解析主要用到三个类: (1) IP ...

  2. VR视频原理

    VR视频,这里指的是沉浸式全景视频,基本场景是观影者戴上显示头盔(如cardboard),在其中通过头部的转动可以看到全景视频的每个方向的图像.同时也能听到来自各个方向的声音,声音也会随着头部的转动而 ...

  3. 图数据库初探之Neo4j

    图数据库初试之Neo4j 自从进入了移动互联网时代,各种新事物出现的速度都好像坐上了宇宙飞船,几乎隔几天一个新概念.就拿数据库而言,什么Oracle.DB2.SQL Server.MySQL,这些你都 ...

  4. IOS Carthage安装、使用

    一.Carthage的安装和使用1.安装homebrew后输入如下命令 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercont ...

  5. c#中 这种构造方法Recer(…):this(…){ }

    指的是构造函数首先调用另外一个构造函数. class Program { static void Main(string[] args) { Person p2 = new Person(" ...

  6. 51nod1212【最小生成树kruskal算法】

    思路: 利用破圈法. #include <bits/stdc++.h> using namespace std; typedef long long LL; const int N=1e3 ...

  7. JMeter提取和重用作为变量 - 具有更多提取器

    这是我们最受欢迎的博文,我们添加了更多提取器.这篇文章解释了如何使用正则表达式提取器从第一个请求的响应中提取密钥,并将提取的密钥用于后续请求.我们称之为JMeter Extract并重复使用. 现在您 ...

  8. Action类为何要 extends ActionSupport以及实现ModelDriven

    http://blog.sina.com.cn/s/blog_164e377490102wqhk.html

  9. 实现一个redis连接池

    #*****************jedis连接参数设置*********************#redis服务器ipredis.ip=169.254.130.122#redis服务器端口号red ...

  10. Eclipse+Tomcat环境集成

    1.下载Eclipse: 我用的Version: Mars.2 Release (4.5.2),直接在官网上下:http://www.eclipse.org/downloads/packages/re ...