range queries and one graph
This commit is contained in:
44
CSES - CSES Problem Set/Counting_Rooms.cpp
Normal file
44
CSES - CSES Problem Set/Counting_Rooms.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
// Counting Rooms
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void dfs(vector<vector<vector<pair<int, int>>>> &m, pair<int, int> s, vector<vector<bool>> &v) {
|
||||
v[s.first][s.second] = true;
|
||||
for (auto e: m[s.first][s.second]) {
|
||||
if (!v[e.first][e.second]) dfs(m, e, v);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, m; cin >> n >> m;
|
||||
// map<pair<int, int>, vector<pair<int, int>>> map1;
|
||||
vector<vector<vector<pair<int, int>>>> map1(n, vector<vector<pair<int, int>>>(m));
|
||||
vector<string> in;
|
||||
for (int i{}; i < n; i++) {
|
||||
string s; cin >> s; in.push_back(s);
|
||||
}
|
||||
for (int i{}; i < n; i++) {
|
||||
for (int j{}; j < m; j++) {
|
||||
if (in[i][j] == '#') continue;
|
||||
if (i > 0 && in[i-1][j] == '.') map1[i][j].push_back({i-1, j});
|
||||
if (i < n - 1 && in[i+1][j] == '.') map1[i][j].push_back({i+1, j});
|
||||
if (j > 0 && in[i][j-1] == '.') map1[i][j].push_back({i, j-1});
|
||||
if (j < m - 1 && in[i][j+1] == '.') map1[i][j].push_back({i, j+1});
|
||||
}
|
||||
}
|
||||
// set<pair<int, int>> v;
|
||||
vector<vector<bool>> v(n, vector<bool>(m, false));
|
||||
int count = 0;
|
||||
for (int i{}; i < n; i++) {
|
||||
for (int j{}; j < m; j++) {
|
||||
if (in[i][j] == '#') continue;
|
||||
if (v[i][j]) continue;
|
||||
count++;
|
||||
dfs(map1, {i, j}, v);
|
||||
}
|
||||
}
|
||||
cout << count << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
// solution from usaco - need to understand this further. my approach was very similar
|
||||
|
||||
typedef long long ll;
|
||||
#define pb push_back
|
||||
#define pi pair<int, int>
|
||||
#define f first
|
||||
#define mp make_pair
|
||||
#define s second
|
||||
|
||||
ll dp[100001][101];
|
||||
const ll MOD = (10e8) + 7;
|
||||
int main() {
|
||||
ios_base::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
int arr[n];
|
||||
for (int i = 0; i < n; i++) { cin >> arr[i]; }
|
||||
if (arr[0] == 0) {
|
||||
fill(dp[0], dp[0] + 101, 1);
|
||||
} else {
|
||||
dp[0][arr[0]] = 1;
|
||||
}
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (arr[i] == 0) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
dp[i][j] += dp[i - 1][j];
|
||||
if (j - 1 > 0) dp[i][j] += dp[i - 1][j - 1];
|
||||
if (j + 1 <= m) dp[i][j] += dp[i - 1][j + 1];
|
||||
dp[i][j] %= MOD;
|
||||
}
|
||||
} else {
|
||||
dp[i][arr[i]] += dp[i - 1][arr[i]];
|
||||
if (arr[i] - 1 > 0) dp[i][arr[i]] += dp[i - 1][arr[i] - 1];
|
||||
if (arr[i] + 1 <= m) dp[i][arr[i]] += dp[i - 1][arr[i] + 1];
|
||||
dp[i][arr[i]] %= MOD;
|
||||
}
|
||||
}
|
||||
ll ans = 0;
|
||||
for (int i = 1; i <= m; i++) {
|
||||
ans += dp[n - 1][i];
|
||||
ans %= MOD;
|
||||
}
|
||||
cout << ans;
|
||||
}
|
||||
24
CSES - CSES Problem Set/Dynamic_Programming/Book_Shop.cpp
Normal file
24
CSES - CSES Problem Set/Dynamic_Programming/Book_Shop.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// Book Shop
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int n, x; cin >> n >> x;
|
||||
vector<int> price(n);
|
||||
vector<int> pages(n);
|
||||
for (auto &e: price) cin >> e;
|
||||
for (auto &e: pages) cin >> e;
|
||||
|
||||
int dp[1000001];
|
||||
for (int i{}; i < n; i++) {
|
||||
for (int j = x; j >= price[i]; j--) {
|
||||
dp[j] = max(dp[j], dp[j - price[i]] + pages[i]);
|
||||
}
|
||||
}
|
||||
|
||||
cout << dp[x] << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
31
CSES - CSES Problem Set/Dynamic_Programming/Grid_Paths.cpp
Normal file
31
CSES - CSES Problem Set/Dynamic_Programming/Grid_Paths.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Grid Paths
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MOD = 1e9 + 7;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
ll dp[1001][1001];
|
||||
|
||||
int main() {
|
||||
int n; cin >> n;
|
||||
vector<string> in(n);
|
||||
for (int i{}; i < n; i++) cin >> in[i];
|
||||
dp[0][0] = in[0][0] != '*';
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (in[0][i] == '*') continue;
|
||||
else dp[0][i] = dp[0][i - 1];
|
||||
}
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j{}; j < n; j++) {
|
||||
if (in[i][j] == '*') continue;
|
||||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
|
||||
dp[i][j] %= MOD;
|
||||
}
|
||||
}
|
||||
cout << dp[n-1][n-1] << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Dynamic Range Sum Queries
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using ll = long long;
|
||||
using namespace std;
|
||||
|
||||
vector<ll> construct(vector<ll> &input) {
|
||||
vector<ll> output(2 * input.size());
|
||||
for (int i = 0; i < input.size(); i++) {
|
||||
output[input.size() + i] = input[i];
|
||||
}
|
||||
for (int i = input.size() - 1; i > 0; i--) {
|
||||
output[i] = output[2*i] + output[2*i + 1];
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
void update(int k, ll x, int n, vector<ll> &tree) {
|
||||
k += n;
|
||||
tree[k] = x;
|
||||
k /= 2;
|
||||
for (; k >= 1; k /= 2) {
|
||||
tree[k] = tree[2*k + 1] + tree[2*k];
|
||||
}
|
||||
}
|
||||
|
||||
ll query(int a, int b, int n, vector<ll> &tree) {
|
||||
a--; b--;
|
||||
a += n; b += n;
|
||||
ll s = 0;
|
||||
while (a <= b) {
|
||||
if (a % 2 == 1) {
|
||||
s += tree[a];
|
||||
a++;
|
||||
}
|
||||
if (b % 2 == 0) {
|
||||
s += tree[b];
|
||||
b--;
|
||||
}
|
||||
a /= 2; b /= 2;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, q; cin >> n >> q;
|
||||
vector<ll> in(n);
|
||||
for (int i{}; i < n; i++) {
|
||||
cin >> in[i];
|
||||
}
|
||||
auto seg = construct(in);
|
||||
for (int i{}; i < q; i++) {
|
||||
int t; cin >> t;
|
||||
if (t == 2) {
|
||||
int a, b; cin >> a >> b;
|
||||
cout << query(a, b, n, seg) << endl;
|
||||
} else if (t == 1) {
|
||||
int k; ll u; cin >> k >> u;
|
||||
k--;
|
||||
update(k, u, n, seg);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Static Range Minimum Queries
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
vector<ll> construct(vector<ll> &input) {
|
||||
int n = input.size();
|
||||
vector<ll> output(2*n);
|
||||
for (int i{}; i < n; i++) {
|
||||
output[n + i] = input[i];
|
||||
}
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
output[i] = min(output[2*i], output[2*i + 1]);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
ll query(int a, int b, int n, vector<ll> &tree) {
|
||||
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 (int i = 0; i < n; i++) {
|
||||
cin >> in[i];
|
||||
}
|
||||
vector<ll> seg = construct(in);
|
||||
cout << endl;
|
||||
for (int i{}; i < q; i++) {
|
||||
int a, b; cin >> a >> b;
|
||||
a--; b--;
|
||||
cout << query(a, b, n, seg) << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Static Range Sum Queries
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
int main() {
|
||||
int n, q; cin >> n >> q;
|
||||
vector<ll> pref(n, 0);
|
||||
ll x; cin >> x;
|
||||
pref[0] = x;
|
||||
for (int i = 1; i < n; i ++) {
|
||||
ll y; cin >> y;
|
||||
pref[i] = y + pref[i-1];
|
||||
}
|
||||
for (int i{}; i < q; i++) {
|
||||
int a, b; cin >> a >> b;
|
||||
if (a >= 2 ) cout << pref[b - 1] - pref[a - 2] << endl;
|
||||
else cout << pref[b - 1] << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user