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.
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.
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.
1
10
2
1 5 2
5 9 3
Case 1: The total value of the hook is 24.
题意:表示没玩过dota>__<,不过并无大碍,就是Pudge可以将他的链子转化为金银铜三种状态,然后问他链子的总重量。。。。;
思路:线段树模板。。。。
下面附上代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll o<<1
#define rr o<<1|1
using namespace std;
const int N=100005;
struct Tree
{
int l,r;
int lazy,sum,len;
};
Tree tree[N<<2];
void pushup(int o)
{
tree[o].sum=tree[ll].sum+tree[rr].sum;
}
void pushdown(int o)
{
if(tree[o].lazy!=-1)
{
tree[ll].lazy=tree[o].lazy;
tree[rr].lazy=tree[o].lazy;
tree[ll].sum=tree[o].lazy*tree[ll].len;
tree[rr].sum=tree[o].lazy*tree[rr].len;
tree[o].lazy=-1;
}
}
void build(int o,int L,int R)
{
tree[o].l=L,tree[o].r=R;
tree[o].len=R-L+1;
tree[o].lazy=-1;
if(L==R)
{
tree[o].sum=1;
return ;
}
int mid=(L+R)>>1;
build(ll,L,mid);
build(rr,mid+1,R);
pushup(o);
}
void update(int o,int L,int R,int v)
{
if(tree[o].l>=L&&tree[o].r<=R)
{
tree[o].sum=v*tree[o].len;
tree[o].lazy=v;
return ;
}
pushdown(o);
int mid=(tree[o].l+tree[o].r)>>1;
if(R<=mid)
update(ll,L,R,v);
else if(L>mid)
update(rr,L,R,v);
else
{
update(ll,L,mid,v);
update(rr,mid+1,R,v);
}
pushup(o);
}
int Query(int o,int L,int R)
{
if(tree[o].l>=L&&tree[o].r<=R)
{
return tree[o].sum;
}
pushdown(o);
int mid=(tree[o].l+tree[o].r)>>1;
if(R<=mid)
return Query(ll,L,R);
else if(L>mid)
return Query(rr,L,R);
else
return Query(ll,L,mid)+Query(rr,mid+1,R);
}
int main()
{
int T,n,Q,k=0,a,b,c;
cin>>T;
while(T--)
{
cin>>n;
build(1,1,n);
cin>>Q;
while(Q--)
{
scanf("%d %d %d",&a,&b,&c);
update(1,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n",++k,Query(1,1,n));
}
return 0;
}

本文介绍了一个基于Dota游戏背景的算法问题,主要内容为通过一系列操作改变Pudge钩子中金属棒的状态(铜、银、金),并最终计算钩子的总价值。文章提供了使用线段树实现这一计算的代码示例。
466

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



