您的位置:首页 > 其它

线段树 区间更新(成段更新) HDU1698

2012-12-21 16:04 561 查看

转载自:
http://blog.csdn.net/andychanry/article/details/5785624
http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

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

Total Submission(s): 2796    Accepted Submission(s): 1210


Problem Description

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.

The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.

For each silver stick, the value is 2.

For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.

You may consider the original hook is made up of cupreous sticks.

 
 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.

For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.

Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents
the golden kind.

 
 

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

 
 

Sample Input

1
10
2
1 5 2
5 9 3

 

Sample Output
Case 1: The total value of the hook is 24.成段更新,最后总区间求

首先用的数据结构是线段树,这个大家可以去搜索下,本身的数据结构是很简单的,但是变化是非常广的,这就是线段树的魅力所在,这题用线段的主要思想就是把域Cover定位是否整个覆盖,比如[1,5]这个区间的覆盖标记是1说明完全覆盖,所以马上可以算出答案(5-1+1)×val,val为覆盖这个线段的钩子等级,这题主要是考更新,更新的时候要通过判断Cover来做即时更新,如果Cover是0的话那可以按照普通方法来更新,但是如果Cover是1的话那就先要把父节点的相关信息传递给左右儿子,然后改变自己相关信息才可,具体比较难阐述,还是要大家仔细想想,下面放上代码,还有一个小插曲,话说它题目的描述是到10w因此一开始我只开了100010的数组结果一直WA,然后试了下20W结果过了,题目描述还有是有点问题

#include<iostream>
using namespace std;
const int Max=200010;
int N,M;
//采用静态的树结构
struct Node
{
int l,r;
Node *lc,*rc;
//覆盖标记,表示这一段是不是完全覆盖
int Cover;
//记录当前节点的值
int Val;
}No[Max];
int TreeLen;
//新建,每次分配空间
Node *new_node()
{
Node * pt = &No[TreeLen++];
memset(pt,0,sizeof(Node));
return pt;
}
//建树
Node* MakeTree(int l,int r)
{
//每次都初始化一个节点
Node *root= new_node();
int Mid;
root->l=l;
root->r=r;
root->Cover=1;
root->Val=1;
if(l!=r)
{
//递归整棵树
Mid=(l+r)/2;
root->lc=MakeTree(l,Mid);
root->rc=MakeTree(Mid+1,r);
}
//返回根节点
return root;
}
void Update(Node* root ,int l,int r,int Val)
{
int Mid=(root->l+root->r)/2;
if(root->l==l&&root->r==r)////每次只需要更新到(l,r)这个节点就行了 (l,r)的子节点不需要更新 延迟更新
{
//如果找到完全覆盖的,覆盖标记记为1,然后把值赋给Val
root->Cover=1;
root->Val=Val;
return ;
}
//如果之前赋值过的话,则要把值全部传递给子树
if(root->Cover)
{
// 把值传递给左儿子
root->lc->Cover=1;
root->lc->Val=root->Val;
// 把值传递给右儿子
root->rc->Cover=1;
root->rc->Val=root->Val;
root->Cover=0;
}
//递归更新直到要更新的全部更新
if(l>Mid)
{
Update(root->rc,l,r,Val);
}
else
if(r<=Mid)
{
Update(root->lc,l,r,Val);
}
else
{
Update(root->lc,l,Mid,Val);
Update(root->rc,Mid+1,r,Val);
}
}
//求答案
int GetVal(Node *root)
{
//如果覆盖标记为1说明该段全部被覆盖求出值
if(root->Cover)
{
return (root->r-root->l+1)*(root->Val);
}
//负责递归直到找到子树是完全覆盖的
else
{
return GetVal(root->lc)+GetVal(root->rc);
}
}
int main()
{
int T,i;
scanf("%d",&T);
int X,Y,Z;
int Time=1;
while(T--)
{
TreeLen=0;
scanf("%d%d",&N,&M);
Node* Root=MakeTree(1,N);
for(i=0;i<M;i++)
{
scanf("%d%d%d",&X,&Y,&Z);
Update(Root,X,Y,Z);
}
printf("Case %d: The total value of the hook is %d./n",Time++,GetVal(Root));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: