HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
To the moon
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88748#problem/I
Description
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we'll give you a chance, to implement the logic behind the scene.
You‘ve been given N integers A [1], A [2],..., A [N]. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {A i | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {A i | l <= i <= r}.
3. H l r t: Querying a history sum of {A i | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 10 5, |A [i]| ≤ 10 9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10 4 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.
Input
A 1 A 2 ... A n
... (here following the m operations. )
Output
... (for each query, simply print the result. )
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
2 4
0 0
C 1 1 1
C 2 2 -1
Q 1 2
H 1 2 1
Sample Output
4
55
9
15
0
1
HINT
题意
可持久化线段树
查询第i个时间的区间和
查询现在的区间和
让时间增加一s,并区间修改
回到t秒
题解:
这种题,能离线就离线,在线做会MLE= =
按照时间建一棵树,然后直接暴力dfs,然后线段树修改,再不断回溯就好了
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define maxn 110000
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std; typedef long long SgTreeDataType;
struct treenode
{
int L , R , mid ;
SgTreeDataType sum , lazy;
void updata(SgTreeDataType v)
{
sum += (R-L+)*v;
lazy += v;
}
}; treenode tree[maxn*]; inline void push_down(int o)
{
SgTreeDataType lazyval = tree[o].lazy;
tree[*o].updata(lazyval) ; tree[*o+].updata(lazyval);
tree[o].lazy = ;
} inline void push_up(int o)
{
tree[o].sum = tree[*o].sum + tree[*o+].sum;
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = tree[o].lazy = ;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,long long v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
push_down(o);
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
push_up(o);
}
} inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>;
SgTreeDataType res = ;
if (QL <= mid) res += query(QL,QR,*o);
if (QR > mid) res += query(QL,QR,*o+);
push_up(o);
return res;
}
} int n,m;
struct Query
{
int type;
long long x,y,z;
};
Query P[maxn];
struct node
{
long long x;
int y;
};
bool cmp(node a,node b)
{
return a.y<b.y;
}
vector<node> ans;
vector<int> E[maxn];
long long x;
int Time[maxn];
char c[];
void dfs(int x)
{
for(int i=;i<E[x].size();i++)
{
int v = E[x][i];
if(P[v].type == )
{
updata(P[v].x,P[v].y,P[v].z,);
dfs(v);
updata(P[v].x,P[v].y,-P[v].z,);
}
else
{
ans.push_back((node){query(P[v].x,P[v].y,),v});
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
ans.clear();
for(int i=;i<maxn;i++)
E[i].clear();
memset(Time,,sizeof(Time));
memset(tree,,sizeof(tree));
build_tree(,n+,);
for(int i=;i<=n;i++)
{
scanf("%I64d",&x);
updata(i,i,x,);
}
int now = ;
for(int i=;i<=m;i++)
{
scanf("%s",c);
if(c[]=='C')
{
P[i].type = ;
scanf("%I64d%I64d%I64d",&P[i].x,&P[i].y,&P[i].z);
E[Time[now]].push_back(i);
now++;
Time[now]=i;
}
else if(c[]=='Q')
{
P[i].type = ;
scanf("%I64d%I64d",&P[i].x,&P[i].y);
E[Time[now]].push_back(i);
}
else if(c[]=='H')
{
P[i].type = ;
scanf("%I64d%I64d%I64d",&P[i].x,&P[i].y,&P[i].z);
E[Time[P[i].z]].push_back(i);
}
else
{
scanf("%I64d",&x);
now = x;
}
}
dfs();
sort(ans.begin(),ans.end(),cmp);
int len = ans.size();
for(int i=;i<ans.size();i++)
printf("%I64d\n",ans[i].x);
}
return ;
}
HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和的更多相关文章
- HDU 4348 To the moon 可持久化线段树
To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...
- 线段树&&线段树的创建线段树的查询&&单节点更新&&区间更新
目录 线段树 什么是线段树? 线段树的创建 线段树的查询 单节点更新 区间更新 未完待续 线段树 实现问题:常用于求数组区间最小值 时间复杂度:(1).建树复杂度:nlogn.(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 ...
- HDU 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
- hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...
- hdu4348 To the moon (可持久化线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 题目大意:给定含有n个数的序列,有以下四种操作 1.C l r d:表示对区间[l,r]中的数加 ...
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- 区间第K小——可持久化线段树模板
概念 可持久化线段树又叫主席树,之所以叫主席树是因为这东西是fotile主席创建出来的. 可持久化数据结构思想,就是保留整个操作的历史,即,对一个线段树进行操作之后,保留访问操作前的线段树的能力. 最 ...
- HDU 4348 To the moon(可持久化线段树)
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
随机推荐
- @section Right
布局页 _BaseLayout.cshtml @RenderSection("Right", true) 视图页 Index.cshtml @{ Layout = "~/ ...
- Android-onTouchEvent方法的使用
手机屏幕事件的处理方法onTouchEvent.该方法在View类中的定义,并且所有的View子类全部重写了该方法,应用程序可以通过该方法处理手机屏幕的触摸事件.该方法的签名如下所示. public ...
- Linux下GPIO驱动
编写驱动程序,首先要了解是什么类型的设备.linux下的设备分为三类,分别为:字符设备,块设备和网络设备.字符设备类型是根据是否以字符流为数据的交换方式,大部分设备都是字符设备,如键盘,串口等,块设备 ...
- POJ 1208 The Blocks Problem
The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5397 Accepted: 231 ...
- sql server 2012序列号
MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28 MICROSOFT SQL SERVER 2012 商业智能版 ...
- UIButton 设置为圆形,并且使用图片(UIImage)当做背景
-(UIButton *)shareButtonWithIcon:(NSString *)iconName { UIButton *button = [UIButtonbuttonWithType:U ...
- javascript 面向对象制作坦克大战 (一)
PS:这个坦克大战是在网上下的一段源码之后,自己进行的重写. 写这个的目的是为了巩固自己这段时间对js的学习.整理到博客上,算是对自己近端时间学习js的一个整理. 同时也希望可以帮助到学习js的园 ...
- 排序算法:七大排序算法的PHP实现
由于最近在找工作,面试中难免会遇到一些算法题,所以就用PHP把七大排序算法都实现了一遍,也当做是一种复习于沉淀. 冒泡排序 2. 选择排序 3. 插入排序 4. 快速排序 5. 希尔排序 6. 归并排 ...
- CSAPP(2):程序的汇编表示(Linux版)
程序员学习汇编代码的需求随着时间的推移发生了变化,开始时只要求程序员能直接用汇编语言编写程序,现在则要求他们能够阅读和理解编译器产生的代码. 下面是针对32位机器 数据格式 Intel用术语“字”(w ...
- [算法] 希尔排序 Shell Sort
希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进.该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. 希尔排序实质上是一种分组插入方法.它的基本思想是: ...