Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int INF = 1e9;
- struct SegTree {
- vector<int> data; // двоичное дерево, представленное в виде массива (левое - ind * 2, правое - ind * 2 + 1)
- int n; // количество элементов отрезка
- SegTree(const vector<int> & dots) {
- n = dots.size();
- data.resize(4 * n, INF);
- build_data(1, 1, n, dots);
- }
- void build_data(int id, int l, int r, const vector<int> & dots) {
- if(l == r) {
- data[id] = dots[l - 1];
- return;
- }
- int m = (l + r) / 2;
- build_data(id * 2, l, m, dots);
- build_data(id * 2 + 1, m + 1, r, dots);
- data[id] = min(data[2 * id], data[2 * id + 1]);
- }
- int get(int l, int r) {
- return get(1, l, r, 1, n);
- }
- int get(int id, int l, int r, int curr_l, int curr_r) {
- if(l <= curr_l && curr_r <= r)
- return data[id];
- int m = (curr_l + curr_r) / 2;
- if(r <= m)
- return get(2 * id, l, r, curr_l, m);
- if(m <= l)
- return get(2 * id + 1, l, r, m + 1, curr_r);
- return min(
- get(2 * id, l, r, curr_l, m),
- get(2 * id + 1, l, r, m + 1, curr_r)
- );
- }
- void set(int pos, int val) {
- set(1, 1, n, pos, val);
- }
- void set(int id, int l, int r, int pos, int val) {
- if(l == r) {
- data[id] = val;
- return;
- }
- int m = (l + r) / 2;
- if(pos <= m) set(id * 2, l, m, pos, val);
- else set(id * 2 + 1, m + 1, r, pos, val);
- data[id] = min(data[id * 2], data[id * 2 + 1]);
- }
- };
- int main() {
- vector<int> dots;
- int n, m;
- string command;
- cin >> n;
- dots.resize(n, 0);
- for(auto & el : dots)
- cin >> el;
- SegTree tree(dots);
- cin >> m;
- while(m--) {
- cin >> command;
- if(command == "get") {
- int l, r;
- cin >> l >> r;
- cout << tree.get(l, r) << endl;
- }
- else if(command == "set") {
- int pos, val;
- cin >> pos >> val;
- tree.set(pos, val);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement