Cod sursa(job #3375684)

Utilizator MagicantPlusIuoras Andrei MagicantPlus Data 13 noiembrie 2026 00:06:26
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 7.24 kb
#define programstate2
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned ll
#define vec vector
#define flist forward_list
#define um unordered_map
#define us unordered_set
#define prioq priority_queue
#define all(x) x.begin(), x.end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ld long double
#define cd complex<ld>
#define pll pair<ll, ll>
#define tll tuple<ll, ll, ll>
#define ass assign
#define fun function

#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define ULL_MAX ULLONG_MAX
#define LD_MAX LDBL_MAX
#define LD_MIN LDBL_MIN

#define nl " \n"

#define vv(type, name, n, ...) vector<vector<type>> name(n, vector<type>(__VA_ARGS__))
#define vvv(type, name, n, m, ...) \
	vector<vector<vector<type>>> name(n, vector<vector<type>>(m, vector<type>(__VA_ARGS__)))

// https://trap.jp/post/1224/
#define rep1(n) for(ll i=0; i<(ll)(n); ++i)
#define rep2(i,n) for(ll i=0; i<(ll)(n); ++i)
#define rep3(i,a,b) for(ll i=(ll)(a); i<(ll)(b); ++i)
#define rep4(i,a,b,c) for(ll i=(ll)(a); i<(ll)(b); i+=(ll)(c))
#define cut4(a,b,c,d,e,...) e
#define rep(...) cut4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rep1e(n) for(ll i=1; i<=(ll)(n); ++i)
#define rep2e(i,n) for(ll i=1; i<=(ll)(n); ++i)
#define rep3e(i,a,b) for(ll i=(ll)(a); i<=(ll)(b); ++i)
#define rep4e(i,a,b,c) for(ll i=(ll)(a); i<=(ll)(b); i+=(ll)(c))
#define repe(...) cut4(__VA_ARGS__,rep4e,rep3e,rep2e,rep1e)(__VA_ARGS__)
#define per1(n) for(ll i=(ll)(n)-1; i>=0; --i)
#define per2(i,n) for(ll i=(ll)(n)-1; i>=0; --i)
#define per3(i,a,b) for(ll i=(ll)(a)-1; i>=(ll)(b); --i)
#define per4(i,a,b,c) for(ll i=(ll)(a)-1; i>=(ll)(b); i-=(ll)(c))
#define per(...) cut4(__VA_ARGS__,per4,per3,per2,per1)(__VA_ARGS__)
#define per1e(n) for(ll i=(ll)(n); i>=1; --i)
#define per2e(i,n) for(ll i=(ll)(n); i>=1; --i)
#define per3e(i,a,b) for(ll i=(ll)(a); i>=(ll)(b); --i)
#define per4e(i,a,b,c) for(ll i=(ll)(a); i>=(ll)(b); i-=(ll)(c))
#define pere(...) cut4(__VA_ARGS__,per4e,per3e,per2e,per1e)(__VA_ARGS__)
#define rep_subset(i,s) for(ll i=0;i!=-1;i=i==(s)?-1:(i|~(s))+1&(s))
#define per_subset(i,s) for(ll i=(s);i!=-1;i=i==0?-1:i-1&(s))
#define X(p) (p).real()
#define Y(p) (p).imag()

template<class T, class S> bool mkmin(T& a, const initializer_list<S>& b)
{S m = *min_element(all(b)); if(m<a){a=m;return 1;}return 0;}
template<class T, class S> bool mkmax(T& a, const initializer_list<S>& b)
{S m = *max_element(all(b)); if(m>a){a=m;return 1;}return 0;}
template<class T, class S> bool mkmin(T& a, const S& b){if(b<a){a=b;return 1;}return 0;}
template<class T, class S> bool mkmax(T& a, const S& b){if(b>a){a=b;return 1;}return 0;}

#ifdef programstate0
#endif

#ifdef programstate1
// ifstream fin("./special/input.txt");
// ofstream ferr("./special/messages.txt");
// #define cerr ferr
// #define cin fin
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<vector<T>> x){cerr << "\n"; for(auto i: x) {for(auto j : i) {cerr << j << ' ';} cerr << "\n";}}
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif

constexpr ll MOD = 1e9+7;
constexpr ll INF = (1ll << 59);

#ifdef programstate2
const string FILENAME = "rmq";
ifstream fin(FILENAME + ".in");
ofstream fout(FILENAME + ".out");
#define cin fin
#define cout fout
#endif

ll nnt() {ll x; cin >> x; return x;}
string nst() {string x; cin >> x; return x;}
char nct() {char x; cin >> x; return x;}

ll egcd(ll a, ll b, ll& x, ll& y)
{
    if(!b)
    {
        x=1;
        y=0;
        return a;
    }
    ll d = egcd(b, a%b, y, x);
    y -= a/b*x;
    return d;
}

template<int M>
class Mint
{
public:

    static constexpr int MOD = M;
    int x;
    
    Mint(long long a = 0)
    {
        if(0 <= a && a < MOD) x = a;
        else
        {
            x = a%MOD;
            if(x < 0) x += MOD;
        }
    }
    explicit operator ll() const {return x;}
    Mint operator-() const { return (x==0?0:MOD-x); }

    Mint& operator+=(Mint a)
    {
        if((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    Mint& operator-=(Mint a)
    {
        if((x -= a.x) < 0) x += MOD;
        return *this;
    }
    Mint& operator*=(Mint a)
    {
        x = (long long)(x) * a.x % MOD;
        return *this;
    }

    friend bool operator==(Mint a, Mint b) { return a.x == b.x; }
    friend bool operator!=(Mint a, Mint b) { return a.x != b.x; } 
    friend Mint modInv(Mint a)
    {
        ll x, y;
        egcd(a.x, a.MOD, x, y);
        return x;
    }
    Mint& operator/=(Mint a) { return *this *= modInv(a); }
    friend Mint operator+(Mint a, Mint b) {return a += b;}
    friend Mint operator-(Mint a, Mint b) {return a -= b;}
    friend Mint operator*(Mint a, Mint b) {return a *= b;}
    friend Mint operator/(Mint a, Mint b) {return a /= b;}
    friend istream& operator>>(istream& is, Mint& a)
    {
        ll x; is >> x; a = x; return is;
    }
    friend ostream& operator<<(ostream& os, Mint a)
    {
        return os << a.x;
    }
};
using mint = Mint<MOD>;

struct tnode
{
    ll val, minval, sz, y;
    tnode *l, *r;

    tnode(ll v = 0, ll prio = 0)
    {
        l = r = NULL;
        y = prio;
        minval = val = v;
        sz = 1;
    }
};
using pnode = tnode*;

namespace treap
{
    ll sz(pnode o){return o?o->sz:0;}
    void updateNode(pnode o)
    {
        if(!o) return;
        o->minval = o->val;
        if(o->l) mkmin(o->minval, o->l->minval);
        if(o->r) mkmin(o->minval, o->r->minval);
        o->sz = sz(o->l) + sz(o->r) + 1;
    }
    void split(pnode o, pnode& a, pnode& b, ll x, ll add = 0)
    {
        if(!o) return void(a=b=NULL);
        ll ox = sz(o->l) + add;

        if(ox <= x) split(o->r, o->r, b, x, ox+1), a = o;
        else split(o->l, a, o->l, x, add), b = o;

        updateNode(a);
        updateNode(b);
    }
    void merge(pnode& o, pnode a, pnode b)
    {
        if(!a || !b) o = a?a:b;
        else if(a->y > b->y) merge(a->r, a->r, b), o = a;
        else merge(b->l, a, b->l), o = b;

        updateNode(o);
    }
    ll query(pnode& o, ll x, ll y)
    {
        pnode u, v;
        split(o, o, u, x-1);
        split(u, u, v, y, sz(o));

        ll res = u->minval;

        merge(o, o, u);
        merge(o, o, v);

        return res;
    }
}

void solve()
{
    ll n, q;
    cin >> n >> q;
    
    pnode root = NULL;
    rep(n)
    {
        ll x;
        cin >> x;
        treap::merge(root, root, new tnode(x, rand()));
    }
    
    rep(q)
    {
        ll l, r;
        cin >> l >> r;
        l--, r--;
        cout << treap::query(root, l, r) << nl;
    }
}
int main()
{
    srand(time(0));
    ll t = 1;
    if(1) cin >> t;
    rep(t) solve();

	return 0;
}