toI(s=readline()) = parse(Int,s) toVI(s=readline()) = map(toI,eachsplit(s)) rep(f,n) = [f() for _ in 1:n] @enum YN Yes=1 No=0 function main() n,q = toVI() a = toVI() queries = rep(toVI,q) solve(n,q,a,queries) .|> println end function solve(n,q,a,queries) st = SegTree(max,0,a) res = fill(-1,q) for (i,(c,x)) in pairs(queries) reduce(st,1,n) > x || continue if c == 1 ok = 0; ng = n+1 while ng-ok > 1 m = (ok+ng)÷2 if reduce(st,1,m) ≤ x ok = m else ng = m end end # if 1 ≤ ok+1 ≤ n res[i] = ok+1 st[ok+1] = 0 # end elseif c == 2 ng = 0; ok = n+1 while ok-ng > 1 m = (ok+ng)÷2 if reduce(st,m,n) ≤ x ok = m else ng = m end end # if 1 ≤ ok-1 ≤ n res[i] = ok-1 st[ok-1] = 0 # end end # @info i,st end res end struct SegTree{T,⊗} e::T n::Int node::Vector{T} function SegTree(⊗,e,n,T=typeof(e)) node = [e for _ in 1:2n-1] new{T,⊗}(e,n,node) end function SegTree(⊗,e,xs::AbstractVector,T=eltype(xs)) n = length(xs) node = Vector{T}(undef,2n-1) node[n:end] .= xs for i ∈ n-1:-1:1 node[i] = node[2i] ⊗ node[2i+1] end new{T,⊗}(e,n,node) end end function Base.setindex!((;n,node)::SegTree{T,⊗},x,i) where {T,⊗} i += n-1 node[i] = x while i > 1 i >>= 1 node[i] = node[2i] ⊗ node[2i+1] end return x end function Base.getindex((;n,node)::SegTree{T,⊗},i) where {T,⊗} i += n-1 x = node[i] return x end function Base.reduce((;e,n,node)::SegTree{T,⊗},l::Int,r::Int) where {T,⊗} l += n-1; r += n-1 sml = e; smr = e while l ≤ r isodd(l) && (sml = sml ⊗ node[l]) iseven(r) && (smr = node[r] ⊗ smr) l = (l+1) >> 1 r = (r+1) >> 1 - 1 end return sml ⊗ smr end function Base.reduce(a::SegTree) (;n) = a reduce(a,1,n) end main()