您的位置:首页 > 其它

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

2016-04-20 20:24 465 查看


A - 卿学姐与公主


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


Submit 
Status

某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏
在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中。
英勇的卿学姐拔出利刃冲向了拯救公主的道路。
走过了荒野,翻越了高山,跨过了大洋,卿学姐来到了魔王的第一道城关。
在这个城关面前的是魔王的精锐部队,这些士兵成一字排开。
卿学姐的武器每次只能攻击一个士兵,并造成一定伤害,卿学姐想知道某时刻从LL到RR这个区间内,从开始到现在累计受伤最严重的士兵受到的伤害。
最开始每个士兵的受到的伤害都是0


Input

第一行两个整数N,QN,Q表示总共有NN个士兵编号从11到NN,和QQ个操作。
接下来QQ行,每行三个整数,首先输入一个tt,如果tt是11,那么输入p,xp,x,表示卿学姐攻击了pp这个位置的士兵,并造成了xx的伤害。如果tt是22,那么输入L,RL,R,表示卿学姐想知道现在[L,R][L,R]闭区间内,受伤最严重的士兵受到的伤害。
1≤N≤1000001≤N≤100000
1≤Q≤1000001≤Q≤100000
1≤p≤N1≤p≤N
1≤x≤1000001≤x≤100000
1≤L≤R≤N1≤L≤R≤N


Output

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


Sample input and output

Sample InputSample 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树