您的位置:首页 > 其它

HDU - 4348 To the moon(主席树)

2017-09-06 21:12 375 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348点击打开链接


To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 6313    Accepted Submission(s): 1459


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

 

Sam
4000
ple 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

因为有历史版本询问。。所以主席树

#include <bits/stdc++.h>
using namespace std;
#define maxn 100100
struct xjy
{
int left ;
int right;
long long int flag;
long long int sum;
};
xjy tree[maxn*30];
int T[maxn];
long long int a[maxn];
int n,m;
int tot;
long long int ans;
int build(int left,int right)
{
int root=tot++;
if(left==right)
{
tree[root].sum=a[left];
tree[root].flag=0;
return root;
}
int mid=(left+right)>>1;
tree[root].left=build(left,mid);
tree[root].right=build(mid+1,right);
tree[root].sum=tree[tree[root].left].sum+tree[tree[root].right].sum;
return root;
}
long long int update(int root,int left,int right,int l,int r,int val)
{
int now=tot++;
tree[now].sum=tree[root].sum;
tree[now].left=tree[root].left;
tree[now].right=tree[root].right;
tree[now].flag=tree[root].flag;
tree[now].sum+=(long long int )(right-left+1)*(long long int )val;
if(left==l&&right==r)
{
tree[now].flag+=(long long int )val;
return now;
}
int mid=(l+r)>>1;
if(right<=mid)
{
tree[now].right=tree[root].right;
tree[now].left=update(tree[root].left,left,right,l,mid,val);
}
else if(left>mid)
{
tree[now].left=tree[root].left;
tree[now].right=update(tree[root].right,left,right,mid+1,r,val);
}
else
{
tree[now].left=update(tree[root].left,left,mid,l,mid,val);
tree[now].right=update(tree[root].right,mid+1,right,mid+1,r,val);
}
return now;
}

void query(int root,int left,int right,int l,int r)
{
if(left==l&&right==r)
{
ans+=tree[root].sum;
return ;
}
ans+=tree[root].flag*(long long int )(right-left+1);
int mid=(l+r)>>1;
if(right<=mid)
{
query(tree[root].left,left,right,l,mid);
}
else if(left>mid)
{
query(tree[root].right,left,right,mid+1,r);
}
else
{
query(tree[root].left,left,mid,l,mid);
query(tree[root].right,mid+1,right,mid+1,r);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int time=0;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
T[time]=build(1,n);
for(int i=1;i<=m;i++)
{
char c;
scanf(" %c",&c);
if(c=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
ans=0;
query(T[time],l,r,1,n);
printf("%lld\n",ans);
}
else if(c=='C')
{
int l,r,val;
scanf("%d%d%lld",&l,&r,&val);
time++;
T[time]=update(T[time-1],l,r,1,n,val);
}
else if(c=='H')
{
int l,r,t;
scanf("%d%d%d",&l,&r,&t);
ans=0;
query(T[t],l,r,1,n);
printf("%lld\n",ans);
}
else
{
int t;
scanf("%d",&t);
time=t;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: