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;
|
||||||
|
}
|
||||||
40
CSES - CSES Problem Set/Graph_Algorithms/Building_Roads.cpp
Normal file
40
CSES - CSES Problem Set/Graph_Algorithms/Building_Roads.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
// Building Roads
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void dfs(vector<vector<int>> &g, int s, vector<bool> &v) {
|
||||||
|
v[s] = true;
|
||||||
|
for (auto e: g[s]) {
|
||||||
|
if (!v[e]) dfs(g, e, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n, m; cin >> n >> m;
|
||||||
|
vector<vector<int>> g(n);
|
||||||
|
for (int i{}; i < m; i++) {
|
||||||
|
int a, b;
|
||||||
|
cin >> a >> b;
|
||||||
|
a--; b--;
|
||||||
|
g[a].push_back(b);
|
||||||
|
g[b].push_back(a);
|
||||||
|
}
|
||||||
|
int count = -1;
|
||||||
|
vector<pair<int, int>> roads;
|
||||||
|
vector<bool> v(n);
|
||||||
|
for (int i{}; i < n; i++) {
|
||||||
|
if (v[i]) continue;
|
||||||
|
count ++;
|
||||||
|
dfs(g, i, v);
|
||||||
|
if (i > 0) roads.push_back({i, i - 1});
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << count << endl;
|
||||||
|
for (auto [x, y]: roads) {
|
||||||
|
cout << x+1 << " " << y + 1 << endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user