51 problems solved
This commit is contained in:
68
CSES - CSES Problem Set/Dynamic_Range_Minimum_Queries.cpp
Normal file
68
CSES - CSES Problem Set/Dynamic_Range_Minimum_Queries.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
// Dynamic Range Minimum Queries
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
void update(int k, ll x, vector<ll> &tree, int n) {
|
||||
k--;
|
||||
k += n;
|
||||
tree[k] = x;
|
||||
k /= 2;
|
||||
for (; k >= 1; k /= 2) {
|
||||
tree[k] = min(tree[2*k], tree[2*k + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
vector<ll> construct(vector<ll> &input) {
|
||||
int n = input.size();
|
||||
vector<ll> out(2*n);
|
||||
for (int i{}; i < n; i++) {
|
||||
out[i+n] = input[i];
|
||||
}
|
||||
for (int i = n - 1; i >= 1; i--) {
|
||||
out[i] = min(out[2*i], out[2*i + 1]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
ll query(vector<ll> &tree, int a, int b, int n) {
|
||||
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 (auto &e: in) cin >> e;
|
||||
auto seg = construct(in);
|
||||
for (int i{}; i < q; i++) {
|
||||
int t; cin >> t;
|
||||
if (t == 1) {
|
||||
int k;
|
||||
ll u;
|
||||
cin >> k >> u;
|
||||
update(k, u, seg, n);
|
||||
}
|
||||
if (t == 2) {
|
||||
int a, b; cin >> a >> b;
|
||||
a--; b--;
|
||||
cout << query(seg, a, b, n) << endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user