HDU 4348 I - To the moon 可持续化
队友套的可持续化线段树,徘徊在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 可持续化的更多相关文章
- 【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| ...
- 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 SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 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 询问当前时 ...
- HDU 4348 To the moon 主席树 在线更新
http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...
- HDU 4348 SPOJ 11470 To the moon
Vjudge题面 Time limit 2000 ms Memory limit 65536 kB OS Windows Source 2012 Multi-University Training C ...
- HDU 4348 To the moon 可持久化线段树
To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
- To the moon HDU - 4348 (主席树,区间修改)
Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...
随机推荐
- Flutter实战视频-移动电商-64.会员中心_顶部头像UI布局
64.会员中心_顶部头像UI布局 会员中心的样式 member.dart 清除原来的代码生成一个基本的结构 默认返回一个scaffold脚手架工具,body里面布局使用ListView,这样不会出现纵 ...
- java 中判断字符串相等
今天写Java代码时遇到一个问题,就是关于判断两个字符串是否相等的问题.(刚尝试用SSH框架) 在大多编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的 ...
- 网络编程-TCP连接-readLine
Server: package com.net.tcp; import java.io.BufferedReader; import java.io.IOException; import java. ...
- [Swift 开发] 使用闭包传值(typealias)
在Swift中使用闭包来实现两个界面的传值 例如:有A类和B类. B类 //声明闭包 typealias valueBlock = (Float)->() var returnPrice: va ...
- TP5之一次选择多张图片并预览
点击选择图片(可选多张),确定后将选择的图片显示在页面上,已经选择的图片也可以删除,点击提交将图片提交给后台. 1.效果图 2.code 用input标签并选择type=file,记得带上multip ...
- UTF-8和Unicode互转
1.Unicode转UTF-8 void CodeCovertTool::UNICODE_to_UTF8(const CString& unicodeString, std::string&a ...
- JDK 重要目录结构
\bin 目录包含 Java 的开发工具,包括 Java 编译器 javac.exe.Java 解释器 java.exe 等: javac:Java 编译器,用来将 Java 程序编译成字节码 jav ...
- 黑马MySQL数据库学习day02 表数据CRUD 约束CRUD
/* 基础查询练习: 1.字段列表查询 当查询全部字段时,一种简便方式,使用*代替全部字段(企业中不推荐使用) 2.去除重复行 DISTINCT,注意修饰的是行,也就是整个字段列表,而不是单个字段. ...
- python 操作mysql数据库存
代码: 说明:由于我本机没有安装数据库,数据库是在远程访问的,故地址不是localhost # __author__ = 'STEVEN' import pymysql host = '10.1.1. ...
- python 基础(八) os模块
OS模块 概念:包含了普遍的操作 系统的功能 一.函数 函数名 函数说明 os.name 获取操作系统类型 nt->Windows posix->Linux/Unix os.listdir ...