为何运行不了?
  • 板块P1531 I Hate It
  • 楼主lzy120406
  • 当前回复4
  • 已保存回复5
  • 发布时间2025/1/13 21:32
  • 上次更新2025/1/14 11:35:16
查看原帖
为何运行不了?
1069308
lzy120406楼主2025/1/13 21:32

rt,看了半天没问题啊

#include <bits/stdc++.h>
#define ll long long
#define db(s,a) cout << s << ":" << a << endl
#define dbarr(a,n) for(ll i=1;i<=n;i++) cout << a[i] << " " ; cout << endl ;
#define isok cout << "ok" << endl
using namespace std ;
ll n , m , i , A , B , f , a[100005] , d[800005] ;
void build(ll l , ll r , ll p){
	if(l==r){
		d[p] = a[l] ;
		return ;
	}
	ll mid = (l+r) >> 1 ;
	build(l,mid,p << 1) ;
	build(mid+1,r,(p << 1)|1) ;
	d[p] = max(d[p << 1],d[(p << 1)|1]) ;
	return ;
}
void update(ll l , ll c , ll s , ll t , ll p){
	if(s==t){
		d[p] = c ;
		return ;
	}
	ll mid = (s+t) >> 1 ;
	if(l<=mid){
		update(l,c,s,mid,p << 1) ;
	}
	if(l>mid){
		update(l,c,mid+1,t,(p << 1)|1) ;
	}
	d[p] = max(d[p << 1],d[(p << 1)|1]) ;
	return ;
}
ll query(ll l , ll r , ll s , ll t , ll p){
	if (l<=s && t<=r){
		return d[p] ;
	}
	ll mid = (s+t) >> 1 , a = LONG_LONG_MIN , b = LONG_LONG_MIN ;
	if(l<=mid){
		a = query(l,r,s,mid,p << 1) ;
	}
	if(r>mid){
		b = query(l,r,mid+1,t,(p << 1)|1) ;
	}
	return max(a,b) ;
}
int main(){
//	freopen(".in","r",stdin) ;
//	freopen(".out","w",stdout) ;
	ios::sync_with_stdio(false) ;
	cin.tie(0) ;
	cout.tie(0) ;
	cin >> n >> m ;
	for(i=1;i<=n;i++){
		cin >> a[i] ;
	}
	build(1,n,1) ;
	while(m--){
		cin >> f >> A >> B ;
		if(f=='Q'){
			cout << query(A,B,1,n,1) << '\n' ;
		}else {
			if(a[A]<a[B]){
				update(A,a[B],1,n,1) ;
			}
		}
	}
	return 0 ;
}

(我知道暴力可过,但我想用线段树写

2025/1/13 21:32
加载中...