1 solutions

  • 0
    @ 2024-10-4 15:25:27

    数据较水,直接用线段树暴算即可

    code:

    #include <iostream>
    using namespace std;
    const long long maxn=4*1e6+5;
    long long val1[maxn],val2[maxn];
    void update1(long long u,long long l,long long r,long long s,long long t,long long x) {
    	if(s<=l&&r<=t) {
    		val1[u]=x;
    		return;
    	}
    	long long mid=(l+r)/2;
    	if(s<=mid)update1(u*2,l,mid,s,t,x);
    	if(t>mid)update1(u*2+1,mid+1,r,s,t,x);
    	val1[u]=max(val1[u*2],val1[u*2+1]);
    }
    long long query1(long long u,long long l,long long r,long long s,long long t) {
    	if(s<=l&&r<=t) {
    		return val1[u];
    	}
    	long long mid=(l+r)/2;
    	if(t<=mid)return query1(u*2,l,mid,s,t);
    	if(s>mid)return query1(u*2+1,mid+1,r,s,t);
    	return max(query1(u*2,l,mid,s,t),query1(u*2+1,mid+1,r,s,t));
    }
    void update2(long long u,long long l,long long r,long long s,long long t,long long x) {
    	if(s<=l&&r<=t) {
    		val2[u]=x;
    		return;
    	}
    	long long mid=(l+r)/2;
    	if(s<=mid)update2(u*2,l,mid,s,t,x);
    	if(t>mid)update2(u*2+1,mid+1,r,s,t,x);
    	val2[u]=min(val2[u*2],val2[u*2+1]);
    }
    long long query2(long long u,long long l,long long r,long long s,long long t) {
    	if(s<=l&&r<=t) {
    		return val2[u];
    	}
    	long long mid=(l+r)/2;
    	if(t<=mid)return query2(u*2,l,mid,s,t);
    	if(s>mid)return query2(u*2+1,mid+1,r,s,t);
    	return min(query2(u*2,l,mid,s,t),query2(u*2+1,mid+1,r,s,t));
    }
    int main() {
    	long long n,k;
    	cin>>n>>k;
    	for(long long i=1; i<=n; i++) {
    		long long x;
    		cin>>x;
    		update1(1,1,n,i,i,x);
    		update2(1,1,n,i,i,x);
    	}
    	for(int i=1;i<=n-k+1;i++){
    		cout<<query2(1,1,n,i,i+k-1)<<" ";
    	}
    	cout<<endl;
    	for(int i=1;i<=n-k+1;i++){
    		cout<<query1(1,1,n,i,i+k-1)<<" ";
    	}
    }
    
    
    • 1

    Information

    ID
    843
    Time
    1000ms
    Memory
    500MiB
    Difficulty
    3
    Tags
    # Submissions
    30
    Accepted
    7
    Uploaded By