Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<algorithm>
- #define INT_MAX 1e18;
- typedef long long ll;
- using namespace std;
- void updateQuery(vector<ll>& toQuery, ll s, ll e, ll indexToBeUpdated, ll newVal, ll index)
- {
- if (indexToBeUpdated > e || indexToBeUpdated < s)
- {
- return;
- }
- if (s == e)
- {
- toQuery[index] = newVal;
- return;
- }
- ll mid = (s + e) / 2;
- updateQuery(toQuery, s, mid, indexToBeUpdated, newVal, 2 * index);
- updateQuery(toQuery, mid + 1, e, indexToBeUpdated, newVal, 2 * index + 1);
- toQuery[index] = min(toQuery[2 * index], toQuery[2 * index + 1]);
- return;
- }
- ll minQuery(vector<ll> toQuery, ll qs, ll qe, ll s, ll e, ll currIndex)
- {
- if (s >= qs && e <= qe)
- {
- return toQuery[currIndex];
- }
- if (e<qs || s>qe)
- {
- return INT_MAX;
- }
- ll mid = (s + e) / 2;
- ll left = minQuery(toQuery, qs, qe, s, mid, 2 * currIndex);
- ll right = minQuery(toQuery, qs, qe, mid + 1, e, 2 * currIndex + 1);
- ll result = min(left, right);
- return result;
- }
- void buildtoQuery(vector<ll> arr, ll s, ll e, vector<ll>& toQuery, ll index)
- {
- if (s == e)
- {
- toQuery[index] = arr[s];
- return;
- }
- ll mid = (s + e) / 2;
- buildtoQuery(arr, s, mid, toQuery, 2 * index);
- buildtoQuery(arr, mid + 1, e, toQuery, 2 * index + 1);
- toQuery[index] = min(toQuery[2 * index], toQuery[2 * index + 1]);
- }
- int main()
- {
- ll n, q;
- cin >> n >> q;
- ll i = n;
- vector<ll> arr, toQuery;
- arr.resize(n);
- toQuery.resize(4 * n + 3);
- //...
- for (i = 0; i<n; i++)
- cin >> arr[i];
- //build toQuery ...
- buildtoQuery(arr, 0, n - 1, toQuery, 1);
- while (q--)
- {
- ll decider, result;
- cin >> decider;
- if (decider == 1)
- {
- //asking query
- ll qs, qe;
- cin >> qs >> qe;
- result = minQuery(toQuery, qs - 1, qe - 1, 0, n - 1, 1);
- cout << result << endl;
- }
- else if (decider == 2)
- {
- //updation query;
- ll i, newVal;
- cin >> i >> newVal;
- updateQuery(toQuery, 0, n - 1, i - 1, newVal, 1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement