Problem Description
Background

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 {Ai | 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 {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | 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 ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. 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
n m

A1 A2 ... An

... (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

题意:有一个由n个数组成的序列,有4中操作:

1.C l r d [l,r]这段区间都加上d

2.Q l r 询问[l,r]这段区间的和

3.H l r t 询问之前t时间[l,r]的区间和

4.B t 回到t时间,且下一秒的时间从t开始

思路:有两种,一种是在线写法,即用主席树写,按时间建立主席树,主席树上的每一棵线段树维护[1,n]这段序列的信息,这里成段更新的时候要注意,以往写线段树的时候,都是把lazy标记向下传,但是写主席树的时候每一次下传,那么新的节点数就会非常多,会爆内存,所以我们不把lazy操作下传,只是在询问的时候,最后累加的答案加上每一个父亲节点上的lazy值。

还有一种是离线写法,我们先把要询问历史区间和的问题都保存下来,并标记下这个问题是在第几次操作后被问的,这里"C l r d","B t"都算是一次操作,然后再记录每次操作中所问的历史区间和的时间。那么我们每一次操作,就先把这个时间可能被后面问到的历史区间和的问题都回答完,用nas[id]记录id这个问题的答案,然后我们把这个操作中问的问题都删除,因为之后这些问题都不会问了。

在线做法:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lth th<<1
#define rth th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100010
#define M 3000000
int lson[M],rson[M],add[M],th,T[M];
ll a[maxn];
ll sum[M];
int n;
int build(int L,int R)
{
int mid;
int newroot=++th;
add[newroot]=0;
if(L==R){
sum[newroot]=a[L];
return newroot;
}
mid=(L+R)/2;
lson[newroot]=build(L,mid);
rson[newroot]=build(mid+1,R);
sum[newroot]=sum[lson[newroot] ]+sum[rson[newroot] ];
return newroot;
} int update(int root,int L,int R,int l,int r,int value)
{
int newroot=++th;
int tmp=newroot;
int i,j;
if(L==l && R==r){
lson[newroot]=lson[root];
rson[newroot]=rson[root];
sum[newroot]=sum[root]+(r-l+1)*value;
add[newroot]=add[root]+value;
return tmp;
}
add[newroot]=add[root];
sum[newroot]=sum[root]+(r-l+1)*value;
int mid=(L+R)/2;
if(r<=mid){
lson[newroot]=update(lson[root],L,mid,l,r,value );
rson[newroot ]=rson[root];
}
else if(l>mid){
lson[newroot]=lson[root];
rson[newroot]=update(rson[root],mid+1,R,l,r,value );
}
else{
lson[newroot]=update(lson[root],L,mid,l,mid,value );
rson[newroot]=update(rson[root],mid+1,R,mid+1,r,value );
}
return tmp;
} ll question(int root,int L,int R,int l,int r)
{
int i,j;
ll ans=0;
if(L==l && R==r){
return sum[root];
}
ans+=(ll)add[root]*(ll)(r-l+1);
int mid=(L+R)/2;
if(r<=mid)ans+=question(lson[root],L,mid,l,r);
else if(l>mid)ans+=question(rson[root],mid+1,R,l,r);
else{
ans+=question(lson[root],L,mid,l,mid);
ans+=question(rson[root],mid+1,R,mid+1,r);
}
return ans; } int main()
{
int m,i,j,c,d,e;
char str[5];
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
th=0;
T[0]=build(1,n);
int tm=0;
for(i=1;i<=m;i++){
scanf("%s",str);
if(str[0]=='C'){
scanf("%d%d%d",&c,&d,&e);
tm++;
T[tm]=update(T[tm-1],1,n,c,d,e);
}
else if(str[0]=='Q'){
scanf("%d%d",&c,&d);
printf("%lld\n",question(T[tm],1,n,c,d));
}
else if(str[0]=='H'){
scanf("%d%d%d",&c,&d,&e);
printf("%lld\n",question(T[e],1,n,c,d));
}
else if(str[0]=='B'){
scanf("%d",&e);
tm=e;
}
} }
return 0;
}

离线做法:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lth th<<1
#define rth th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100010
struct edge{
int l,r,d,f;
}q[maxn]; int a[maxn];
struct node{
int l,r,add;
ll sum;
}b[4*maxn]; void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
b[th].add=0;
if(l==r){
b[th].sum=a[l];return;
}
mid=(l+r)/2;
build(l,mid,lth);
build(mid+1,r,rth);
b[th].sum=b[lth].sum+b[rth].sum;
} void pushdown(int th)
{
if(b[th].add){
b[lth].add+=b[th].add;
b[lth].sum+=(ll)b[th].add*(ll)(b[lth].r-b[lth].l+1);
b[rth].add+=b[th].add;
b[rth].sum+=(ll)b[th].add*(ll)(b[rth].r-b[rth].l+1);
b[th].add=0;
} } void update(int l,int r,int num,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
b[th].add+=num;
b[th].sum+=(ll)num*(ll)(r-l+1);
return;
}
pushdown(th);
mid=(b[th].l+b[th].r)/2;
if(r<=mid)update(l,r,num,lth);
else if(l>mid)update(l,r,num,rth);
else{
update(l,mid,num,lth);
update(mid+1,r,num,rth);
}
b[th].sum=b[lth].sum+b[rth].sum;
} ll question(int l,int r,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
return b[th].sum;
}
pushdown(th);
mid=(b[th].l+b[th].r)/2;
if(r<=mid)return question(l,r,lth);
else if(l>mid)return question(l,r,rth);
else{
return question(l,mid,lth)+question(mid+1,r,rth);
}
} struct node1{
int l,r,idx,caozuo;
};
bool operator<(node1 a,node1 b){
return a.caozuo<b.caozuo;
}
multiset<node1>tm[maxn]; //时间为t的历史询问
multiset<node1>::iterator it; ll ans[maxn];
vector<int>cz[maxn]; //操作为i时的可能历史询问的时间
vector<int>::iterator p; int chuli[maxn][3]; int main()
{
int n,m,i,j,cishu,t;
char str[5];
while(scanf("%d%d",&n,&m)!=EOF)
{
tm[0].clear();cz[0].clear();
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
tm[i].clear();
cz[i].clear();
}
build(1,n,1);
node1 temp;
cishu=0;t=0;
for(i=1;i<=m;i++){
scanf("%s",str);
if(str[0]=='C'){
cishu++;t++;
q[i].f=1;
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].d);
}
else if(str[0]=='Q'){
q[i].f=2;
scanf("%d%d",&q[i].l,&q[i].r);
}
else if(str[0]=='H'){
q[i].f=3;
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].d);
if(q[i].d==0){
ans[i]=question(q[i].l,q[i].r,1);
continue;
} temp.l=q[i].l;temp.r=q[i].r;temp.idx=i;temp.caozuo=cishu;
tm[q[i].d].insert(temp); cz[cishu].push_back(q[i].d);
}
else if(str[0]=='B'){
cishu++;
q[i].f=4;
scanf("%d",&q[i].d);
t=q[i].d;
}
} cishu=0;t=0; int shijian;
for(i=1;i<=m;i++){
if(q[i].f==1){
cishu++;t++;
chuli[t ][0]=q[i].l;chuli[t][1]=q[i].r;chuli[t][2]=q[i].d;
update(q[i].l,q[i].r,q[i].d,1);
for(it=tm[t].begin();it!=tm[t].end();it++){
temp=*it;
ans[temp.idx ]=question(temp.l,temp.r,1);
} for(j=0;j<cz[cishu].size();j++){
shijian=cz[cishu][j];
for(it=tm[shijian].begin();it!=tm[shijian].end() && (*it).caozuo==cishu; ){
tm[shijian].erase(it++);
}
}
}
else if(q[i].f==2){
ans[i]=question(q[i].l,q[i].r,1);
}
else if(q[i].f==3){
continue;
}
else if(q[i].f==4){
cishu++;
while(t!=q[i].d){
update(chuli[t][0],chuli[t][1],-chuli[t][2],1);
t--;
}
for(j=0;j<cz[cishu].size();j++){
shijian=cz[cishu][j];
for(it=tm[shijian].begin();it!=tm[shijian].end() && (*it).caozuo==cishu; ){
tm[shijian].erase(it++);
}
}
}
}
for(i=1;i<=m;i++){
if(q[i].f==2 || q[i].f==3){
printf("%lld\n",ans[i]);
}
}
}
return 0;
}

hdu4348 To the moon (主席树 || 离线线段树)的更多相关文章

  1. HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...

  2. Codeforces 1000F One Occurrence 主席树|| 离线+线段树

    One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...

  3. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  4. BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)

    BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...

  5. dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448

    4448: [Scoi2015]情报传递 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 588  Solved: 308[Submit][Status ...

  6. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  7. bzoj2333 离线 + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...

  8. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  9. 线段树简单入门 (含普通线段树, zkw线段树, 主席树)

    线段树简单入门 递归版线段树 线段树的定义 线段树, 顾名思义, 就是每个节点表示一个区间. 线段树通常维护一些区间的值, 例如区间和. 比如, 上图 \([2, 5]\) 区间的和, 为以下区间的和 ...

随机推荐

  1. 每日CSS_滚动页面动画效果

    每日CSS_滚动页面动画效果 2021_1_13 源码链接 1. 代码解析 1.1 html 代码片段 <section> <h2>开 始 滑 动</h2> < ...

  2. 【.NET 与树莓派】使用 GPIO 库

    上回老周在说准备工作的时候,提到过树莓派用金属盒散热的事情.有朋友会说,加了金属盒子接线不方便,就算用了"T"形板,毕竟是把导线延长了的.其实扩展板就是把原有的引脚引出(类似于延长 ...

  3. Vulnhub靶场——DC-1

    记一次Vulnhub靶场练习记录 靶机DC-1下载地址: 官方地址 https://download.vulnhub.com/dc/DC-1.zip 该靶场共有5个flag,下面我们一个一个寻找 打开 ...

  4. FI_F4_ZTERM付款条件代码

    这个函数可以弹出一个选择帮助,返回一个付款条件代码 CALL FUNCTION 'FI_F4_ZTERM' EXPORTING I_KOART = 'K' " K为供应商,D为客户 * I_ ...

  5. JMM在X86下的原理与实现

    JMM在X86下的原理与实现 Java的happen-before模型 众所周知 Java有一个happen-before模型,可以帮助程序员隔离各个平台多线程并发的复杂性,只要Java程序员遵守ha ...

  6. Redis 实战 —— 02. Redis 简单实践 - 文章投票

    需求 功能: P15 发布文章 获取文章 文章分组 投支持票 数值及限制条件 P15 如果一篇文章获得了至少 200 张支持票,那么这篇文章就是一篇有趣的文章 如果这个网站每天有 50 篇有趣的文章, ...

  7. Ribbon负载均衡服务调用

    1.在听周阳老师讲解时,使用Ribbon核心组件IRule时是这样用的: ribbon版本 : 自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,项目结构如下 MySelfR ...

  8. 1.5V升压3V集成电路升压芯片

    干电池1.5V升压3V的升压芯片,适用于干电池升压产品输出3V供电 1.5V输入时,输出3V,电流可达500MA. PW5100是一款效率大.10uA低功耗 PW5100输入电压:0.7V-5V PW ...

  9. redis修改requirepass 参数 改密码

    1. 不重启redis如何配置密码? a. 在配置文件中配置requirepass的密码(当redis重启时密码依然有效). # requirepass foobared  ->  修改成 :  ...

  10. fiddler常用过滤

    一.过滤器 过滤这块集中在request栏目的Filter部分,可以根据自己的需要过滤掉不需要的,里面的每个模块都可以设置,这里只说常用的和注意点. 1.假如我只关心某个域名下的抓包,这时可以利用fi ...