本人刚学线段树,40分求调!悬赏关注
查看原帖
本人刚学线段树,40分求调!悬赏关注
797354
Ferdina_zcjb楼主2023/6/14 16:47
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define N 50001
#define TN 100000
int n,q,h[N],ans1,ans2,tree[TN],tree2[TN];
int read() {
	int s = 0, f = 0; char ch = getchar();
	while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
	return f ? -s : s;
}
void build(int k,int L,int R){
    if(L == R){
        //cout << 0 << endl;
        tree[k] = tree2[k] = read();
        return ;
    }
    int mid = (L+R)>>1;
    build(k<<1,L,mid);
    build(k<<1|1,mid+1,R);
    tree[k] = max(tree[k<<1],tree[k<<1|1]);
    tree2[k] = min(tree2[k<<1],tree2[k<<1|1]);
}
void ask(int k,int L,int R,int l,int r){
    if(L > r || R < l)return ;
    if(L <= l && R >= r){
        //cout << tree[k].Max <<" "<<tree[k].Min << endl; 
        ans1 = max(ans1,tree[k]);
        ans2 = min(ans2,tree2[k]);
    }
    if(l == r)return ;
    int mid = (l+r)>>1;
    ask(k<<1,L,R,l,mid);
    ask(k<<1|1,L,R,mid+1,r);
}
signed main(){
    cin >> n >> q;
    build(1,1,n);
    for(int i = 1;i <= q;++i){
        int x,y;
        cin >> x >> y;
        ans1 = 0;
        ans2 = 0x7fffffff;
        ask(1,x,y,1,n);
        cout << ans1-ans2 << endl;
    }
}
2023/6/14 16:47
加载中...