HDU - 4348 To the moon (主席樹)

To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6186    Accepted Submission(s): 1421


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
 

Author
HIT
 

Source

 
題意: 對一個長度爲n的區間進行 4種操作    C 更新 [l,r]每個 數加d   Q當前詢問 區間    H 詢問第t次更新的區間 B返回第t次更新時候的狀態。

分析:直接上主席樹

(自己A了之後看了一下別人的代碼,空間比我少5倍~~當執行了B返回t狀態時可以讓cnt=root[t+1],因爲後面的那些空間之後都不會訪問了。)

AC代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1e5+50;
struct Tree
{
	long long num;
	int add,lson,rson;
}tree[maxn<<5];
int cnt,root[maxn<<5],t;
void init()
{
	cnt=0;
	t=0;
}
void pushup(int rt)
{
	tree[rt].num=tree[tree[rt].lson].num+tree[tree[rt].rson].num;
}
int bulidtree(int l,int r)
{
	int v=++cnt;
	tree[v].add=0;
	if(l==r)
	{
		scanf("%lld",&tree[v].num);
		tree[v].lson=0,tree[v].rson=0;
		return v;
	}
	int mid=(l+r)>>1;
	tree[v].lson=bulidtree(l,mid);
	tree[v].rson=bulidtree(mid+1,r);
	pushup(v);
	return v;
}
int update(int p,int l,int r,int L,int R,int d)
{
	int v=++cnt;
	tree[v]=tree[p];
	tree[v].num+=(R-L+1)*d;
	if(L<=l&&R>=r)
	{
		tree[v].add+=d;
		return v;
	}
	int mid=(l+r)>>1;
	if(R<=mid)
	{
		tree[v].lson=update(tree[p].lson,l,mid,L,R,d);
	}
	else if(L>mid)
	{
		tree[v].rson=update(tree[p].rson,mid+1,r,L,R,d);
	}
	else
	{
		tree[v].lson=update(tree[p].lson,l,mid,L,mid,d);
		tree[v].rson=update(tree[p].rson,mid+1,r,mid+1,R,d);
	}
	return v;
}
long long query(int p,int l,int r,int L,int R)
{
	long long ans=(R-L+1)*tree[p].add;
	if(L<=l&&R>=r)
	{
		return tree[p].num;
	}
	int mid=(l+r)>>1;
	
	if(R<=mid)
	{
		ans+=query(tree[p].lson,l,mid,L,R);
	}
	else if(L>mid)
	{
		ans+=query(tree[p].rson,mid+1,r,L,R);
	}
	else
	{
		ans+=query(tree[p].lson,l,mid,L,mid);
		ans+=query(tree[p].rson,mid+1,r,mid+1,R);
	}
	return ans;
}
int main()
{
	int n,m,flag=0;
	while(scanf("%d%d",&n,&m)==2)
	{
		if(flag)
		printf("\n");
		flag=1;
		init();
		root[0]=bulidtree(1,n);
		for(int i=0;i<m;i++)
		{
			char a[2];
			scanf("%s",a);
			if(a[0]=='C')
			{
				int l,r,d;
				scanf("%d%d%d",&l,&r,&d);
				root[++t]=update(root[t-1],1,n,l,r,d);
				//printf("! %d\n",tree[root[t]].num);
			}
			else if(a[0]=='Q')
			{
				int l,r;
				scanf("%d%d",&l,&r);
				printf("%lld\n",query(root[t],1,n,l,r));
			}
			else if(a[0]=='H')
			{
				int l,r,nt;
				scanf("%d%d%d",&l,&r,&nt);
				printf("%lld\n",query(root[nt],1,n,l,r));
			}
			else
			{
				int a;
				scanf("%d",&a);
				t=a;
				cnt=root[t+1];  //空間優化 
			}
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章