hdu 1698 Just a Hook

本文介绍了一种解决游戏DotA中Pudge's Hook问题的算法实现,通过线段树结构进行区间更新与求和操作,高效计算钩子在经过一系列材质变化后的总价值。

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9943    Accepted Submission(s): 4884


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.
 

Source
 

Recommend
wangye


线段树  区间更新 区间求和


#include<stdio.h>

#define MAXN 200001
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int num[MAXN<<2];
int top[MAXN<<2];


void PushUP(int rt)
{
    num[rt]=num[rt<<1]+num[rt<<1|1];
}

void PushDown(int rt,int m)
{
    if(top[rt])
    {
        top[rt<<1]=top[rt<<1|1]=top[rt];
        num[rt<<1]=top[rt]*(m-(m>>1));
        num[rt<<1|1]=top[rt]*(m>>1);
        top[rt]=0;
    }
}

void build(int l,int r,int rt)
{
    top[rt]=0;
    if(l==r)
    {
        num[rt]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUP(rt);
}

void update(int L,int R,int tmp,int l,int r,int rt)
{
    if(L<=l && r<=R)
    {
        top[rt]=tmp;
        num[rt]=tmp*(r-l+1);
        return ;
    }
    PushDown(rt,r-l+1);
    int m=(l+r)>>1;
    if(m>=L)
        update(L,R,tmp,lson);
    if(m<R)
        update(L,R,tmp,rson);
    PushUP(rt);
}

int main(void)
{
    int T,t;
//    freopen("d:\\in.txt","r",stdin);
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        int n,q,X,Y,Z;
        scanf("%d%d",&n,&q);
        build(1,n,1);
        while(q--)
        {
            scanf("%d%d%d",&X,&Y,&Z);
            update(X,Y,Z,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",t,num[1]);
    }
    return 0;
}



按照自己对线段树的理解重新写的代码,没必要一直求和,最后求就好了,开一个数组就好了,num[rt]表示区间内的值是多少,为0的时候代表区间里的值不统一.。


#include<cstdio>

#define maxn 110000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int num[maxn<<2];


void PushDown(int rt)
{
	num[rt<<1]=num[rt<<1|1]=num[rt];
	num[rt]=0;
}

void build(int l,int r,int rt)
{
	num[rt]=0;
	if(l==r)
	{
		num[rt]=1;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
}

void updata(int l,int r,int rt,int L,int R,int x)
{
	if(l>=L && r<=R)
	{
		num[rt]=x;
		return ;
	}
	if(num[rt])
		PushDown(rt);
	int m=(l+r)>>1;
	if(L<=m)
		updata(lson,L,R,x);
	if(R>m)
		updata(rson,L,R,x);
}

int Sum(int l,int r,int rt)
{
	if(num[rt])
		return (r-l+1)*num[rt];
	int m=(l+r)>>1;
	return Sum(lson)+Sum(rson);
}

int main()
{
	int T,N,Q;
	int x,y,z,t=1;

	scanf("%d",&T);

	while(T--)
	{
		scanf("%d%d",&N,&Q);
		build(1,N,1);
		while(Q--)
		{
			scanf("%d%d%d",&x,&y,&z);
			updata(1,N,1,x,y,z);
		}
		printf("Case %d: The total value of the hook is %d.\n",t++,Sum(1,N,1));
	}
	return 0;

}




【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值