51 problems solved

This commit is contained in:
2024-06-15 20:56:45 +02:00
parent 93ad8c5841
commit 252ca707b3
4 changed files with 109 additions and 1 deletions

File diff suppressed because one or more lines are too long

View 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;
}

View 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;
}