T get(int from, int to)const{ // [from, to) assert(0 <= from && from < to && to <= n); int lg = 31 - countl_zero(to - from); returnfunc(mat[lg][from], mat[lg][to - (1 << lg)]); } };
intmain(){ ios::sync_with_stdio(false), cin.tie(0); int N, M; cin >> N >> M; vector<int> a(N); for (auto& x : a) cin >> x; SparseTable<int> st(std::move(a), [](int x, int y) { return max(x, y); }); while (M--) { int l, r; cin >> l >> r; cout << st.get(l - 1, r) << '\n'; } }
python
1 2 3 4 5 6 7 8
from SparseTable import SparseTable
N, M = map(int, input().split()) a = [int(v) for v ininput().split()] st = SparseTable(a, max) for _ inrange(M): l, r = map(int, input().split()) print(st.query(l - 1, r))