Task: | Dynamic Range Minimum Queries |
Sender: | paulschulte |
Submission time: | 2024-09-22 11:12:25 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.26 s | details |
Code
// ~/.vim/cpp_template.cpp #include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <string> #define REP(i,a,b) for (int i = a; i < b; i++) #define ll long long // Type Aliases for 1D and 2D vectors with initialization #define vi(n, val) vector<int>(n, val) // 1D vector of ints with size n, initialized to val #define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val #define ll long long #define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val)) // 2D vector of ints (n x m), initialized to val #define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val using namespace std; template <typename T> void pV(const std::vector<T>& vec, const std::string& label = "Vector") { std::cout << label << ": [ "; for (const auto& elem : vec) { std::cout << elem << " "; } std::cout << "]" << std::endl; } using namespace std; void dfs(int s, vector<bool> *visited, vector<int> (*adj)[]) { if ((*visited)[s]) return; (*visited)[s] = true; // process node s for (auto u: (*adj)[s]) { dfs(u, visited, adj); } } /* vector<int> adj[N]; vector<bool> visited(N, false); int u, v; for(int i = 0; i < M;i++){ cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } */ int qmin(int a, int b, ll n, vector<int>* tree) { a += n; b += n; int s = 1e9; while (a <= b) { //cout << a << ' ' << b << endl; if (a%2 == 1) s = min((*tree)[a++], s); if (b%2 == 0) s = min((*tree)[b--], s); a /= 2; b /= 2; } return s; } void change(int k, int u, ll n, vector<int>* tree) { k += n; (*tree)[k] = u; for (k /= 2; k >= 1; k /= 2) { (*tree)[k] = min((*tree)[2*k], (*tree)[2*k+1]); } } int main() { ios::sync_with_stdio(0); cin.tie(0); // Your code starts here int n, q; cin >> n >> q; ll n_2 = pow(2, ceil(log(n)/log(2))); vector<int> v = vi(2*n_2+1, 1e9); for(ll i = n_2; i < n_2+n; i++){ cin >> v[i]; } //pV(v); for(ll i = n_2-1; i > 0; i--){ v[i] = min(v[2*i], v[2*i+1]); } //pV(v); REP(i, 0, q){ int c; cin >> c; if(c==1){//update int k, u; cin >> k >> u; ++k; change(k, u, n_2-2, &v); }else if(c==2){//get min int a, b; cin >> a >> b; ++a;++b; cout << qmin(a, b, n_2-2, &v) << endl; } } return 0; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
8 80 7 6 4 6 2 9 4 8 2 1 1 2 1 2 2 1 3 ... |
correct output |
---|
7 6 4 4 2 ... |
user output |
---|
7 6 4 4 2 ... Truncated |
Test 2
Verdict: ACCEPTED
input |
---|
200000 200000 398739055 65343131 699208332 3... |
correct output |
---|
28609 129890 20378 20378 311522 ... |
user output |
---|
28609 129890 20378 20378 311522 ... Truncated |