implementation *1200

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define T int t; cin>>t; while(t--)
#define ll long long
#define vec vector
#define all(x) x.begin(), x.end()
const int N=1e5+5, mod=1e9+7;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
template <typename Tmp>
struct sparse_table{
    vector<vector<Tmp>> ST;
    sparse_table(vector<Tmp> &A){
        int n = A.size();
        int LOG = 32 - __builtin_clz(n);
        ST = vector<vector<Tmp>>(LOG, vector<Tmp>(n+1));
        for (int i = 0; i < n; i++){
            ST[0][i] = A[i];
        }
        for (int i = 0; i < LOG - 1; i++){
            for (int j = 0; j < n - (1 << i); j++){
                ST[i + 1][j] = min(ST[i][j], ST[i][j + (1 << i)]);
            }
        }
    }
    Tmp range_min(int L, int R){
        int d = 31 - __builtin_clz(R - L + 1);
        Tmp op1=ST[d][L], op2=ST[d][R - (1 << d)+1];
        return ( op1 < op2 ? op1 : op2 );
    }
    ll query2(int l,int r)
    {
        int sz=r-l+1;
        ll ret=0;
        for(int i=0;i<22;i++)
        {
            if(sz&(1<<i))
            {
                ret=merge(ret, ST[l][i]);
                l+=(1<<i);
            }
        }
        return ret;
    }
};
int main(){
    ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
    int n, k; cin>>n>>k;
    vec<ll>x(n);
    for(auto &it: x) cin>>it;
    sparse_table sp(x);
    if(k==2){
        ll ans=-1e18;
        for(int i=0;i<n;i++){
            ans=max(ans, sp.range_min(0,i));
            if(i+1<n) ans=max(ans, sp.range_min(i+1,n-1));
        }
        cout<<ans;
    }
    else{
        sort(all(x));
        if(k==1) cout<<x[0];
        else cout<<x[n-1];
    }
}
		 	 	   	   	   					  		 		 	


Comments

Submit
0 Comments
More Questions

1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians