Submission #1239719

#TimeUsernameProblemLanguageResultExecution timeMemory
1239719lunarechoSimple (info1cup19_simple)C++20
30 / 100
144 ms13708 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

struct node {

    ll max_odd,min_even;
};

struct SGT {

    node merge(node a, node b)
    {
        return {max(a.max_odd, b.max_odd), min(a.min_even,b.min_even)};
    }
  
    vector<node> tree;
    int sz;

    void init(int n)
    {
        sz = 1;
        while(sz < n)
            sz <<= 1;
        tree.resize(2 * sz);
    }

    void build(vector<ll>& a, int x,int lx,int rx)
    {
        if(rx - lx == 1)
        {
            if(lx < (int) a.size())
            {
                if(a[lx] & 1)
                    tree[x].max_odd = a[lx], tree[x].min_even = LLONG_MAX;
                else
                    tree[x].min_even = a[lx], tree[x].max_odd = LLONG_MIN;
            }
            return;
        }
        int m = (lx + rx) / 2;
        build(a,2*x+1,lx,m);
        build(a,2*x+2,m,rx);
        tree[x] = merge(tree[2*x+1],tree[2*x+2]);
    }

    void build(vector<ll>& a)
    {
        build(a,0,0,sz);
    }

    node query(int l,int r,int x,int lx,int rx)
    {
        if(rx <= l || lx >= r)
            return {LLONG_MIN,LLONG_MAX};
        if(lx >= l and rx <= r)
            return tree[x];
        int m = (lx + rx) / 2;
        return merge(query(l,r,2*x+1,lx,m),query(l,r,2*x+2,m,rx));
    }

    node query(int l,int r)
    {
        return query(l,r,0,0,sz);
    }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin>>n;
    vector<ll> a(n);
    for(auto &it : a)
        cin>>it;
    SGT st;
    st.init(n);
    st.build(a);
    int q;
    cin>>q;
    while(q--)
    {
        int type;
        cin>>type;
        if(type == 1)
        {
            int l,r;
            cin>>l>>r;
            --l;
            node get_ans = st.query(l,r);
            cout<<(get_ans.min_even == LLONG_MAX ? -1 : get_ans.min_even)<<' '<<(get_ans.max_odd == LLONG_MIN ? -1 : get_ans.max_odd)<<'\n';
        }
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...