第一届“文翁杯”现场竞技赛 T5 做作业(线段树)

传送门

【题目分析】

博主本着先给自己套个log的态度写了线段树。。。。。

其实可以做到O(n),甚至nm都是可以的因为不超过300。。。

讲讲线段树做法,因为可以将自己的笔给别人用,所以直接按题意区间加最后取个全局最大值就行了。

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e3+10;

int n;
struct Tree{
	int l,r;
	int maxx;
	int add;
}tr[MAXN<<2];

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void push_up(int root){
	tr[root].maxx=max(tr[root<<1].maxx,tr[root<<1|1].maxx);
}

void push_now(int root,int key){
	tr[root].add+=key;
	tr[root].maxx+=key;
}

void push_down(int root){
	if(tr[root].add){
		push_now(root<<1,tr[root].add);
		push_now(root<<1|1,tr[root].add);
		tr[root].add=0;
	}
}

void update(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update(root<<1|1,mid+1,r,L,R,key);
		else
		  update(root<<1,l,mid,L,mid,key),update(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

int main(){
	n=Read();
	for(int i=1;i<=n;++i){
		int x=Read(),y=Read(),z=Read();
		update(1,1,1000,x,y,z);
	}
	cout<<tr[1].maxx;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值