range queries and one graph

This commit is contained in:
2024-06-15 11:50:15 +02:00
parent b67bf7bbea
commit 93ad8c5841
13 changed files with 288 additions and 1 deletions

View File

@@ -0,0 +1,51 @@
// Static Range Minimum Queries
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> construct(vector<ll> &input) {
int n = input.size();
vector<ll> output(2*n);
for (int i{}; i < n; i++) {
output[n + i] = input[i];
}
for (int i = n - 1; i > 0; i--) {
output[i] = min(output[2*i], output[2*i + 1]);
}
return output;
}
ll query(int a, int b, int n, vector<ll> &tree) {
a += n; b += n;
ll s = INT_MAX;
while (a <= b) {
if (a % 2 == 1) {
s = min(s, tree[a]);
a++;
}
if (b % 2 == 0) {
s = min(s, tree[b]);
b--;
}
a /= 2; b /= 2;
}
return s;
}
int main() {
int n, q; cin >> n >> q;
vector<ll> in(n);
for (int i = 0; i < n; i++) {
cin >> in[i];
}
vector<ll> seg = construct(in);
cout << endl;
for (int i{}; i < q; i++) {
int a, b; cin >> a >> b;
a--; b--;
cout << query(a, b, n, seg) << endl;
}
return 0;
}