卿学姐与公主(线段树区间求最大值)

A - 卿学姐与公主

Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏

在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中。

英勇的卿学姐拔出利刃冲向了拯救公主的道路。

走过了荒野,翻越了高山,跨过了大洋,卿学姐来到了魔王的第一道城关。

在这个城关面前的是魔王的精锐部队,这些士兵成一字排开。

卿学姐的武器每次只能攻击一个士兵,并造成一定伤害,卿学姐想知道某时刻从 L L R R这个区间内,从开始到现在累计受伤最严重的士兵受到的伤害。

最开始每个士兵的受到的伤害都是0

Input

第一行两个整数 N,Q N,Q表示总共有 N N个士兵编号从 1 1 N N,和 Q Q个操作。

接下来 Q Q行,每行三个整数,首先输入一个 t t,如果 t t 1 1,那么输入 p,x p,x,表示卿学姐攻击了 p p这个位置的士兵,并造成了 x x的伤害。如果 t t 2 2,那么输入 L,R L,R,表示卿学姐想知道现在 [L,R] [L,R]闭区间内,受伤最严重的士兵受到的伤害。

1N100000 1≤N≤100000

1Q100000 1≤Q≤100000

1pN 1≤p≤N

1x100000 1≤x≤100000

1LRN 1≤L≤R≤N

Output

对于每个询问,回答相应的值

Sample input and output

Sample Input Sample Output
5 4
2 1 2
1 2 4
1 3 5
2 3 3
0
5

Hint

注意可能会爆int哦


这题就是一道模板题。

AC代码:


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>

using namespace std;
typedef long long ll;
#define rson (rt<<1|1)
#define lson (rt<<1)
#define T 100000+50

struct node
{
	int L,R,mid;
	ll ma;
}tree[T<<2];

void Push_Up(int rt)
{
	tree[rt].ma = max(tree[lson].ma,tree[rson].ma);
}

void Build(int rt,int L,int R)//建树
{
	tree[rt].L = L,tree[rt].R = R;
	tree[rt].mid = (L+R)/2;
	tree[rt].ma = 0;
	if(L==R)return;
	Build(lson,L,tree[rt].mid);
	Build(rson,tree[rt].mid+1,R);
}

void Update(int rt,int pos,int val)//更新点函数
{
	if(tree[rt].L==pos&&tree[rt].R==tree[rt].L){
		tree[rt].ma +=  val;
		return;
	}
	if(tree[rt].mid>=pos){
		Update(lson,pos,val);
	}
	else{
		Update(rson,pos,val);
	}
	Push_Up(rt);
}

ll Hmax(int rt,int L,int R)//区间求最大值函数
{
	if(tree[rt].L>=L&&tree[rt].R<=R){
		return tree[rt].ma;
	}
	if(R<=tree[rt].mid){
		return Hmax(lson,L,R);
	}
	else if(L>tree[rt].mid){
		return Hmax(rson,L,R);
	}
	else {
		return max(Hmax(lson,L,tree[rt].mid),Hmax(rson,tree[rt].mid+1,R));
	}
}

int n,m;

int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif

	int i,j,k;
	while(~scanf("%d%d",&n,&m))
	{
		int t,L,R;
		Build(1,1,n);
		while(m--)
		{
			scanf("%d%d%d",&t,&L,&R);
			if(t==1){
				Update(1,L,R);
			}
			else {
				printf("%lld\n",Hmax(1,L,R));
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值