Description

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
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
。。。简单的线段树区间更新
#include<stdio.h>
#include<string.h>
#define maxn 100010
struct node
{
int l,r,add,sum;
}tree[maxn<<2];
void build(int p,int l,int r)
{
tree[p].l=l;
tree[p].r=r;
tree[p].add=0;
tree[p].sum=1;
if(l==r)
return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
void pushdown(int p)
{
int m=(tree[p].r-tree[p].l+1);
if(tree[p].add)
{
tree[p<<1].add=tree[p].add;
tree[p<<1|1].add=tree[p].add;
tree[p<<1].sum=(m-(m>>1))*tree[p].add;
tree[p<<1|1].sum=(m>>1)*tree[p].add;
tree[p].add=0;
}
}
void update(int p,int l,int r,int val)
{
if(l<=tree[p].l && r>=tree[p].r)
{
tree[p].add=val;
tree[p].sum=(tree[p].r-tree[p].l+1)*val;
return ;
}
pushdown(p);
int mid=(tree[p].l+tree[p].r)>>1;
if(r<=mid)
update(p<<1,l,r,val);
else if(l>mid)
update(p<<1|1,l,r,val);
else
{
update(p<<1,l,mid,val);
update(p<<1|1,mid+1,r,val);
}
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}
int main()
{
int n,t,m;
while(~scanf("%d",&t))
{
int p=1;
while(t--)
{
int x,y,z;
scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",p,tree[1].sum);
p++;
}
}
return 0;
}
本文介绍了一个基于线段树的数据结构算法,用于解决《DotA》游戏中Pudge的MeatHook价值计算问题。通过区间更新操作,算法能够高效地计算不同材质钩子的总价值。
119

被折叠的 条评论
为什么被折叠?



