range queries and one graph
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user