Compare commits
6 Commits
48169fb6f0
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 37dd51801c | |||
| e6b65a9d55 | |||
| 7454bd4d0d | |||
| 38756e1068 | |||
| 8b03716512 | |||
| 2a94def2d6 |
256
.gdb_history
Normal file
256
.gdb_history
Normal file
@@ -0,0 +1,256 @@
|
||||
run
|
||||
p sum
|
||||
p count
|
||||
break 86
|
||||
run
|
||||
break 87
|
||||
run
|
||||
next
|
||||
next
|
||||
p tmp
|
||||
next
|
||||
next
|
||||
p i
|
||||
p s.top().second
|
||||
p
|
||||
p s
|
||||
next
|
||||
p s
|
||||
p tmp
|
||||
next
|
||||
p s
|
||||
p i
|
||||
p fds[i]
|
||||
next
|
||||
break 135
|
||||
run
|
||||
next
|
||||
next
|
||||
step
|
||||
next
|
||||
break 157
|
||||
run
|
||||
next
|
||||
p pdis[i
|
||||
p pdis[i
|
||||
break 120
|
||||
run
|
||||
p parent
|
||||
break 121
|
||||
run
|
||||
p reachable
|
||||
break 121
|
||||
run
|
||||
p reachable
|
||||
break 121
|
||||
ru
|
||||
run
|
||||
p reachable
|
||||
break 94
|
||||
run
|
||||
next
|
||||
break 25
|
||||
break 120
|
||||
run
|
||||
next
|
||||
p j
|
||||
next
|
||||
p res
|
||||
break 120
|
||||
run
|
||||
ruk
|
||||
run
|
||||
next
|
||||
p i
|
||||
next
|
||||
p ans
|
||||
next
|
||||
p i
|
||||
p res
|
||||
break 183
|
||||
run
|
||||
1 2
|
||||
1 3
|
||||
p loop
|
||||
p cyclesize
|
||||
groupsize
|
||||
p cyclepos
|
||||
p groupsize
|
||||
run
|
||||
p loop
|
||||
p groupsize
|
||||
p groups
|
||||
p group
|
||||
p cyclechoice
|
||||
next
|
||||
next
|
||||
p choice
|
||||
next
|
||||
p pos[a]
|
||||
p ((cyclepos[b] - cyclepos[a]) % groupsize[g] + groupsize[g]) % groupsize[g]
|
||||
p b
|
||||
p ((cyclepos[b] - cyclepos[choice]) % groupsize[g] + groupsize[g]) % groupsize[g]
|
||||
next
|
||||
run
|
||||
next
|
||||
next
|
||||
p j
|
||||
run
|
||||
run
|
||||
continue
|
||||
next
|
||||
next
|
||||
p ff
|
||||
p F
|
||||
step
|
||||
next
|
||||
p ff
|
||||
step
|
||||
break 1154
|
||||
break 115
|
||||
continue
|
||||
p f
|
||||
next
|
||||
p f
|
||||
p e.cap - e.flow
|
||||
p e.cap
|
||||
p e.flow
|
||||
p f
|
||||
p e.cap - e.flow
|
||||
continue
|
||||
p v
|
||||
p f
|
||||
ru
|
||||
run
|
||||
break 168
|
||||
run
|
||||
next
|
||||
p now
|
||||
next
|
||||
p lower->first
|
||||
p upper->second
|
||||
p lower->second
|
||||
p now.a
|
||||
p now.b
|
||||
p i.i
|
||||
p now.i
|
||||
next
|
||||
p p var
|
||||
p var
|
||||
p ans
|
||||
p l
|
||||
p r
|
||||
p bit
|
||||
run
|
||||
next
|
||||
p var
|
||||
p bit
|
||||
next
|
||||
p bit
|
||||
next
|
||||
p bit
|
||||
next
|
||||
p bit
|
||||
next
|
||||
break 156
|
||||
run
|
||||
break 97
|
||||
run
|
||||
p a
|
||||
next
|
||||
p
|
||||
p n
|
||||
next
|
||||
p cur
|
||||
break 97
|
||||
run
|
||||
next
|
||||
p i
|
||||
p cur
|
||||
next
|
||||
p cur
|
||||
next
|
||||
p cur
|
||||
p ans
|
||||
break 98
|
||||
ru
|
||||
next
|
||||
p cur
|
||||
p ans
|
||||
p a[j]
|
||||
p a
|
||||
p n / 17
|
||||
next
|
||||
p cur
|
||||
p n / (a[0] * a[1])
|
||||
next
|
||||
p cur
|
||||
next
|
||||
next
|
||||
p ans
|
||||
next
|
||||
break 100
|
||||
run
|
||||
continue
|
||||
next
|
||||
p cur
|
||||
p i
|
||||
p n / a[2]
|
||||
next
|
||||
p cur
|
||||
p n / (a[0] * a[2])
|
||||
next
|
||||
p cur
|
||||
p i
|
||||
n / (a[1] * a[2])
|
||||
p n / (a[1] * a[2])
|
||||
next
|
||||
p ans
|
||||
next
|
||||
p cur
|
||||
p i
|
||||
p n / (a[0] * a[1] * a[2])
|
||||
next
|
||||
p cur
|
||||
next
|
||||
p n / a[3]
|
||||
next
|
||||
p cur
|
||||
p i
|
||||
p n / (a[0] * a[3])
|
||||
p ans
|
||||
next
|
||||
p ans
|
||||
next
|
||||
p cur
|
||||
p n / (a[1] * a[3])
|
||||
next
|
||||
p cur
|
||||
next
|
||||
p cur
|
||||
p i
|
||||
p n / (a[2] * a[3])
|
||||
next
|
||||
p cur
|
||||
next
|
||||
p cur
|
||||
next
|
||||
pc ur
|
||||
next
|
||||
p ans
|
||||
break 101
|
||||
run
|
||||
next
|
||||
continue
|
||||
next
|
||||
p cur
|
||||
p cur2
|
||||
p n / cur2
|
||||
p cur
|
||||
next
|
||||
p cur
|
||||
p cur2
|
||||
p ans + cur
|
||||
p ans2 + n / cur2
|
||||
p n / cur2
|
||||
p cur
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -34,3 +34,8 @@
|
||||
|
||||
# Ignore testcases files from competitest
|
||||
*.testcases
|
||||
|
||||
# Ignore gdb
|
||||
.gdb_history
|
||||
|
||||
*sync*
|
||||
|
||||
105
CSES Problem Set/Advertisement.cpp
Normal file
105
CSES Problem Set/Advertisement.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1142/ */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
ll ans = 0;
|
||||
|
||||
stack<pair<ll, int>> s;
|
||||
rep(i, n) {
|
||||
rmax(ans, fds[i]);
|
||||
int tmp = i;
|
||||
while (!s.empty() && s.top().first >= fds[i]) {
|
||||
rmax(ans, s.top().first * (i - s.top().second));
|
||||
rmax(ans, fds[i] * (i - s.top().second + 1));
|
||||
tmp = s.top().second;
|
||||
s.pop();
|
||||
}
|
||||
|
||||
s.emplace(fds[i], tmp);
|
||||
}
|
||||
|
||||
while (!s.empty()) {
|
||||
rmax(ans, s.top().first * (n - s.top().second));
|
||||
s.pop();
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
124
CSES Problem Set/Apartments.cpp
Normal file
124
CSES Problem Set/Apartments.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1084 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
ll n, m, k;
|
||||
cin >> n >> m >> k;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
vl b(m);
|
||||
cin >> b;
|
||||
|
||||
sortv(a);
|
||||
sortv(b);
|
||||
|
||||
int i = 0;
|
||||
int ans = 0;
|
||||
|
||||
repv(now, b) {
|
||||
int low = i;
|
||||
int high = a.size() - 1;
|
||||
|
||||
int choice = -1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low + high) / 2;
|
||||
|
||||
if (a[mid] + k < now) {
|
||||
low = mid + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (a[mid] - k > now) {
|
||||
high = mid - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
choice = mid;
|
||||
high = mid - 1;
|
||||
}
|
||||
|
||||
if (choice == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
i = choice + 1;
|
||||
|
||||
ans++;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
101
CSES Problem Set/Apple Division.cpp
Normal file
101
CSES Problem Set/Apple Division.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1623 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
ll ans = INT64_MAX >> 1;
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
rep(i, (1 << n)) {
|
||||
ll g1 = 0;
|
||||
ll g2 = 0;
|
||||
|
||||
rep(j, n) {
|
||||
if ((i >> j) & 1) {
|
||||
g1 += fds[j];
|
||||
} else {
|
||||
g2 += fds[j];
|
||||
}
|
||||
}
|
||||
|
||||
rmin(ans, abs(g1 - g2));
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
110
CSES Problem Set/Array Division.cpp
Normal file
110
CSES Problem Set/Array Division.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1085 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
ll low = *max_element(all(fds));
|
||||
ll high = INT64_MAX >> 1;
|
||||
ll ans = INT64_MAX >> 1;
|
||||
|
||||
while (low <= high) {
|
||||
ll mid = (low + high) / 2;
|
||||
|
||||
int tmp = 1;
|
||||
ll total = 0;
|
||||
repv(i, fds) {
|
||||
if (total + i > mid) {
|
||||
total = 0;
|
||||
tmp++;
|
||||
}
|
||||
total += i;
|
||||
}
|
||||
|
||||
if (tmp > k) {
|
||||
low = mid + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
ans = mid;
|
||||
high = mid - 1;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
101
CSES Problem Set/Bit Strings.cpp
Normal file
101
CSES Problem Set/Bit Strings.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1617 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
ll fastpow(ll v, ll p)
|
||||
{
|
||||
ll ans = 1;
|
||||
ll now = v;
|
||||
|
||||
rep(i, 20) {
|
||||
if (p & (1 << i)) {
|
||||
ans *= now;
|
||||
ans %= mod;
|
||||
}
|
||||
|
||||
now *= now;
|
||||
now %= mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
cout << fastpow(2, n) << '\n';
|
||||
}
|
||||
132
CSES Problem Set/Building Roads.cpp
Normal file
132
CSES Problem Set/Building Roads.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1666 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
vi group(n);
|
||||
|
||||
int now = 1;
|
||||
V<bool> vis(n, false);
|
||||
|
||||
function<void(int, int)> dfs = [&](int i, int g){
|
||||
vis[i] = true;
|
||||
|
||||
group[i] = g;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(j, g);
|
||||
}
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (!vis[i]) {
|
||||
dfs(i, now);
|
||||
now++;
|
||||
}
|
||||
}
|
||||
|
||||
V<pair<int, int>> ans;
|
||||
now = 1;
|
||||
int node = 0;
|
||||
|
||||
nrep(i, 1, n) {
|
||||
if (group[i] > now) {
|
||||
ans.emplace_back(node + 1, i + 1);
|
||||
now = group[i];
|
||||
node = i;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans.size() << '\n';
|
||||
for (auto [i, j] : ans) {
|
||||
cout << i << ' ' << j << '\n';
|
||||
}
|
||||
}
|
||||
128
CSES Problem Set/Building Teams.cpp
Normal file
128
CSES Problem Set/Building Teams.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1668 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
V<bool> group(n);
|
||||
V<bool> vis(n);
|
||||
|
||||
function<bool(int, bool)> dfs = [&](int i, bool g) {
|
||||
vis[i] = true;
|
||||
group[i] = g;
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (vis[j]) {
|
||||
if (group[j] == g) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!dfs(j, g ^ 1)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!dfs(i, false)) {
|
||||
cout << "IMPOSSIBLE\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
cout << group[i] + 1 << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
109
CSES Problem Set/Chessboard and Queens.cpp
Normal file
109
CSES Problem Set/Chessboard and Queens.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1624 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
V<string> fds(8);
|
||||
cin >> fds;
|
||||
|
||||
vi perm = {0, 1, 2, 3, 4, 5, 6, 7};
|
||||
|
||||
int ans = 0;
|
||||
do {
|
||||
auto valid = [&](){
|
||||
rep(i, 8) {
|
||||
if (fds[i][perm[i]] == '*') {
|
||||
return false;
|
||||
}
|
||||
rep(j, 8) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (abs(i - j) == abs(perm[i] - perm[j])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
if (valid()) {
|
||||
ans++;
|
||||
}
|
||||
} while (next_permutation(all(perm)));
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
185
CSES Problem Set/Coin Collector.cpp
Normal file
185
CSES Problem Set/Coin Collector.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1686 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vl val(n);
|
||||
cin >> val;
|
||||
|
||||
vvi graph(n);
|
||||
vvi inv(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
inv[b].push_back(a);
|
||||
}
|
||||
|
||||
vi order;
|
||||
V<bool> vis(n);
|
||||
|
||||
function<void(int)> preorder = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
preorder(j);
|
||||
}
|
||||
|
||||
order.push_back(i);
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
preorder(i);
|
||||
}
|
||||
|
||||
reverse(all(order));
|
||||
fill(all(vis), false);
|
||||
|
||||
vi group(n);
|
||||
vl costs;
|
||||
V<set<int>> cond_graph;
|
||||
int g = 0;
|
||||
|
||||
function<void(int)> scc = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
group[i] = g;
|
||||
costs[g] += val[i];
|
||||
|
||||
for (auto j : inv[i]) {
|
||||
if (vis[j]) {
|
||||
if (group[j] != group[i]) {
|
||||
cond_graph[group[j]].insert(group[i]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
scc(j);
|
||||
}
|
||||
};
|
||||
|
||||
repv(i, order) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cond_graph.emplace_back();
|
||||
costs.emplace_back();
|
||||
scc(i);
|
||||
g++;
|
||||
}
|
||||
|
||||
vl memo(g, -1);
|
||||
|
||||
function<ll(int)> dfs = [&](int i) {
|
||||
ll &ans = memo[i];
|
||||
if (ans != -1) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
ans = costs[i];
|
||||
for (auto j : cond_graph[i]) {
|
||||
rmax(ans, dfs(j) + costs[i]);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, g) {
|
||||
rmax(ans, dfs(i));
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
93
CSES Problem Set/Coin Piles.cpp
Normal file
93
CSES Problem Set/Coin Piles.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1754 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
ll a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
if (a < b) {
|
||||
swap(a, b);
|
||||
}
|
||||
|
||||
if ((2 * b - a) >= 0 && (2 * b - a) % 3 == 0) {
|
||||
cout << "YES\n";
|
||||
} else {
|
||||
cout << "NO\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
139
CSES Problem Set/Collecting Numbers II.cpp
Normal file
139
CSES Problem Set/Collecting Numbers II.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2217 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int inf = INT32_MAX >> 1;
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vi adj(n + 2, inf);
|
||||
adj[0] = -1;
|
||||
adj[n + 1] = n;
|
||||
|
||||
vi nums(n);
|
||||
cin >> nums;
|
||||
|
||||
ll rounds = 1;
|
||||
rep(i, n) {
|
||||
if (adj[nums[i] - 1] == inf) {
|
||||
rounds++;
|
||||
}
|
||||
adj[nums[i]] = i;
|
||||
}
|
||||
|
||||
vi prev = adj;
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
swap(adj[nums[a]], adj[nums[b]]);
|
||||
swap(nums[a], nums[b]);
|
||||
|
||||
if (adj[nums[a] - 1] > adj[nums[a]] && prev[nums[a] - 1] < prev[nums[a]]) {
|
||||
rounds++;
|
||||
}
|
||||
if (adj[nums[a] - 1] < adj[nums[a]] && prev[nums[a] - 1] > prev[nums[a]]) {
|
||||
rounds--;
|
||||
}
|
||||
if (nums[b] != nums[a] + 1) {
|
||||
if (adj[nums[b] - 1] > adj[nums[b]] && prev[nums[b] - 1] < prev[nums[b]]) {
|
||||
rounds++;
|
||||
}
|
||||
if (adj[nums[b] - 1] < adj[nums[b]] && prev[nums[b] - 1] > prev[nums[b]]) {
|
||||
rounds--;
|
||||
}
|
||||
}
|
||||
if (adj[nums[a] + 1] < adj[nums[a]] && prev[nums[a] + 1] > prev[nums[a]]) {
|
||||
rounds++;
|
||||
}
|
||||
if (adj[nums[a] + 1] > adj[nums[a]] && prev[nums[a] + 1] < prev[nums[a]]) {
|
||||
rounds--;
|
||||
}
|
||||
if (nums[b] != nums[a] - 1) {
|
||||
if (adj[nums[b] + 1] < adj[nums[b]] && prev[nums[b] + 1] > prev[nums[b]]) {
|
||||
rounds++;
|
||||
}
|
||||
if (adj[nums[b] + 1] > adj[nums[b]] && prev[nums[b] + 1] < prev[nums[b]]) {
|
||||
rounds--;
|
||||
}
|
||||
}
|
||||
cout << rounds << '\n';
|
||||
|
||||
swap(prev[nums[a]], prev[nums[b]]);
|
||||
}
|
||||
}
|
||||
93
CSES Problem Set/Collecting Numbers.cpp
Normal file
93
CSES Problem Set/Collecting Numbers.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2216 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<bool> nums(n + 1, false);
|
||||
nums[0] = true;
|
||||
ll count = 1;
|
||||
rep(i, n) {
|
||||
int num;
|
||||
cin >> num;
|
||||
nums[num] = true;
|
||||
if (!nums[num - 1]) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << count << '\n';
|
||||
}
|
||||
104
CSES Problem Set/Common Divisors.cpp
Normal file
104
CSES Problem Set/Common Divisors.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1081 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int divs[(ll)1e6 + 1];
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
rep(i, n) {
|
||||
int num;
|
||||
cin >> num;
|
||||
|
||||
const size_t lim = sqrt(num);
|
||||
for (size_t j = 1; j <= lim; j++) {
|
||||
if (num % j == 0) {
|
||||
divs[j]++;
|
||||
if (j != num / j) {
|
||||
divs[num / j]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t fds = 1;
|
||||
for (size_t i = 1e6; i >= fds; i--) {
|
||||
if (divs[i] >= 2) {
|
||||
cout << i << '\n';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
103
CSES Problem Set/Concert Tickets.cpp
Normal file
103
CSES Problem Set/Concert Tickets.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1091 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
multiset<ll> tickets;
|
||||
|
||||
while (n--) {
|
||||
ll num;
|
||||
cin >> num;
|
||||
|
||||
tickets.insert(num);
|
||||
}
|
||||
|
||||
while (m--) {
|
||||
ll man;
|
||||
cin >> man;
|
||||
|
||||
auto itr = tickets.upper_bound(man);
|
||||
if (itr == tickets.begin()) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
itr--;
|
||||
cout << *itr << '\n';
|
||||
tickets.erase(itr);
|
||||
}
|
||||
}
|
||||
192
CSES Problem Set/Convex Hull.cpp
Normal file
192
CSES Problem Set/Convex Hull.cpp
Normal file
@@ -0,0 +1,192 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2195 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
struct pt {
|
||||
ll x, y;
|
||||
|
||||
pt() = default;
|
||||
pt(ll x, ll y): x(x), y(y) {}
|
||||
pt(const pt &a): x(a.x), y(a.y) {}
|
||||
|
||||
friend istream &operator >> (istream &is, pt &a) {
|
||||
is >> a.x >> a.y;
|
||||
return is;
|
||||
}
|
||||
|
||||
friend ostream &operator << (ostream &os, pt a) {
|
||||
os << a.x << ' ' << a.y << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
ll operator ^ (pt b) {
|
||||
return x * b.y - y * b.x;
|
||||
}
|
||||
|
||||
pt operator - (pt b) {
|
||||
return pt(x - b.x, y - b.y);
|
||||
}
|
||||
|
||||
bool operator < (pt b) {
|
||||
if (x == b.x) {
|
||||
return y < b.y;
|
||||
}
|
||||
return x < b.x;
|
||||
}
|
||||
};
|
||||
|
||||
ll sarea2(pt a, pt b, pt c) {
|
||||
return (b - a) ^ (c - b);
|
||||
}
|
||||
|
||||
int ccw(pt a, pt b, pt c)
|
||||
{
|
||||
ll ar = sarea2(a, b, c);
|
||||
return (ar > 0) - (ar < 0);
|
||||
}
|
||||
|
||||
V<pt> convex_hull(V<pt> v)
|
||||
{
|
||||
sortv(v);
|
||||
if (v.size() <= 2) {
|
||||
return v;
|
||||
}
|
||||
|
||||
int n = v.size();
|
||||
|
||||
V<pt> ac;
|
||||
V<pt> tmp;
|
||||
|
||||
rep(i, n) {
|
||||
while (ac.size() >= 2 && ccw(ac.end()[-2], ac.end()[-1], v[i]) > 0) {
|
||||
ac.pop_back();
|
||||
}
|
||||
ac.emplace_back(v[i]);
|
||||
}
|
||||
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
while (tmp.size() >= 2 && ccw(tmp.end()[-2], tmp.end()[-1], v[i]) > 0) {
|
||||
tmp.pop_back();
|
||||
}
|
||||
tmp.emplace_back(v[i]);
|
||||
}
|
||||
|
||||
ac.pop_back();
|
||||
tmp.pop_back();
|
||||
|
||||
repv(i, tmp) {
|
||||
ac.emplace_back(i);
|
||||
}
|
||||
|
||||
return ac;
|
||||
}
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
V<pt> a(n);
|
||||
cin >> a;
|
||||
auto conv = convex_hull(a);
|
||||
cout << conv.size() << '\n';
|
||||
repv(i, conv) {
|
||||
cout << i.x << ' ' << i.y << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
112
CSES Problem Set/Counting Rooms.cpp
Normal file
112
CSES Problem Set/Counting Rooms.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1192 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<string> mat(n);
|
||||
cin >> mat;
|
||||
|
||||
V<V<bool>> vis(n, V<bool>(m, false));
|
||||
|
||||
function<void(int, int)> floodfill = [&](int i, int j)
|
||||
{
|
||||
vis[i][j] = true;
|
||||
|
||||
int add[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
|
||||
|
||||
for (auto &k : add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id >= 0 && id < n && jd >= 0 && jd < m && !vis[id][jd] && mat[id][jd] != '#') {
|
||||
floodfill(id, jd);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int ans = 0;
|
||||
rep(i, n) {
|
||||
rep(j, m) {
|
||||
if (!vis[i][j] && mat[i][j] == '.') {
|
||||
ans++;
|
||||
floodfill(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
128
CSES Problem Set/Course Schedule.cpp
Normal file
128
CSES Problem Set/Course Schedule.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1679 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
graph[a - 1].push_back(b - 1);
|
||||
}
|
||||
|
||||
V<bool> frame(n);
|
||||
V<bool> vis(n);
|
||||
|
||||
vi ans;
|
||||
|
||||
function<bool(int)> dfs = [&](int i) {
|
||||
vis[i] = true;
|
||||
frame[i] = true;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (frame[j]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dfs(j)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
ans.push_back(i + 1);
|
||||
|
||||
frame[i] = false;
|
||||
return false;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (!vis[i]) {
|
||||
if (dfs(i)) {
|
||||
cout << "IMPOSSIBLE\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
reverse(all(ans));
|
||||
cout << ans;
|
||||
}
|
||||
123
CSES Problem Set/Creating Strings II.cpp
Normal file
123
CSES Problem Set/Creating Strings II.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1715 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
vl count(26);
|
||||
repv(i, a) {
|
||||
count[i - 'a']++;
|
||||
}
|
||||
|
||||
auto fpow = [&](ll a, ll p) {
|
||||
ll ans = 1;
|
||||
ll now = a;
|
||||
|
||||
rep(i, 30) {
|
||||
if ((p >> i) & 1) {
|
||||
ans *= now;
|
||||
ans %= mod;
|
||||
}
|
||||
|
||||
now *= now;
|
||||
now %= mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
vl fact(a.size() + 1);
|
||||
fact[0] = 1;
|
||||
nrep(i, 1, a.size() + 1) {
|
||||
fact[i] = (fact[i - 1] * i) % mod;
|
||||
}
|
||||
|
||||
ll ans = fact[a.size()];
|
||||
|
||||
rep(i, 26) {
|
||||
ans = (ans * fpow(fact[count[i]], mod - 2)) % mod;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
93
CSES Problem Set/Creating Strings.cpp
Normal file
93
CSES Problem Set/Creating Strings.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1622 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
sortv(a);
|
||||
|
||||
V<string> fds;
|
||||
|
||||
do {
|
||||
fds.emplace_back(a);
|
||||
} while (next_permutation(all(a)));
|
||||
|
||||
cout << fds.size() << '\n';
|
||||
|
||||
repv(i, fds) {
|
||||
cout << i << '\n';
|
||||
}
|
||||
}
|
||||
78
CSES Problem Set/Cut and Paste.cpp
Normal file
78
CSES Problem Set/Cut and Paste.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2072 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
|
||||
}
|
||||
130
CSES Problem Set/Cycle Finding.cpp
Normal file
130
CSES Problem Set/Cycle Finding.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1197 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<tuple<int, int, ll>> edges;
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
a--, b--;
|
||||
|
||||
edges.emplace_back(a, b, c);
|
||||
}
|
||||
|
||||
vl dis(n, 0);
|
||||
vi parent(n, -1);
|
||||
|
||||
rep(i, n - 1) {
|
||||
for (auto [a, b, c] : edges) {
|
||||
if (dis[b] > dis[a] + c) {
|
||||
dis[b] = dis[a] + c;
|
||||
parent[b] = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int now = -1;
|
||||
for (auto [a, b, c] : edges) {
|
||||
if (dis[b] > dis[a] + c) {
|
||||
parent[b] = a;
|
||||
now = b;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (now == -1) {
|
||||
cout << "NO\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
now = parent[now];
|
||||
}
|
||||
|
||||
vi path = {now + 1};
|
||||
|
||||
int tmp = parent[now];
|
||||
while (tmp != now) {
|
||||
path.push_back(tmp + 1);
|
||||
tmp = parent[tmp];
|
||||
}
|
||||
|
||||
path.push_back(now + 1);
|
||||
reverse(all(path));
|
||||
cout << "YES\n" << path;
|
||||
}
|
||||
113
CSES Problem Set/De Bruijn Sequence.cpp
Normal file
113
CSES Problem Set/De Bruijn Sequence.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1692 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
#define INF INT64_MAX >> 1
|
||||
|
||||
vector<int> de_brujin(int n, int k, int lim = INF) {
|
||||
if (k == 1) return vector<int>(lim == INF ? 1 : n, 0);
|
||||
vector<int> l = {0}, ret; // l eh lyndon word
|
||||
while (true) {
|
||||
if (l.size() == 0) {
|
||||
if (lim == INF) break;
|
||||
l.push_back(0);
|
||||
}
|
||||
if (n % l.size() == 0) for (int i : l) {
|
||||
ret.push_back(i);
|
||||
if (ret.size() == n+lim-1) return ret;
|
||||
}
|
||||
int p = l.size();
|
||||
while (l.size() < n) l.push_back(l[l.size()%p]);
|
||||
while (l.size() and l.back() == k-1) l.pop_back();
|
||||
if (l.size()) l.back()++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
auto ans = de_brujin(n, 2, 1 << n);
|
||||
repv(i, ans) {
|
||||
cout << i;
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
99
CSES Problem Set/Digit Queries.cpp
Normal file
99
CSES Problem Set/Digit Queries.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2431 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
ll k;
|
||||
cin >> k;
|
||||
k--;
|
||||
|
||||
ll now = 1;
|
||||
int digs = 1;
|
||||
while (k >= digs * now * 9) {
|
||||
k -= digs * now * 9;
|
||||
now *= 10;
|
||||
digs++;
|
||||
}
|
||||
|
||||
ll act = now;
|
||||
act += (k / digs);
|
||||
k %= digs;
|
||||
|
||||
string fds = to_string(act);
|
||||
cout << fds[k] << '\n';
|
||||
}
|
||||
}
|
||||
127
CSES Problem Set/Distinct Colors.cpp
Normal file
127
CSES Problem Set/Distinct Colors.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1139 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vi colors(n);
|
||||
cin >> colors;
|
||||
|
||||
V<set<int>> c(n);
|
||||
vi ans(n);
|
||||
|
||||
vvi graph(n);
|
||||
rep(i, n - 1) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
function<void(int, int)> dfs = [&](int i, int p) {
|
||||
c[i].insert(colors[i]);
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(j, i);
|
||||
|
||||
if (c[j].size() > c[i].size()) {
|
||||
swap(c[j], c[i]);
|
||||
}
|
||||
|
||||
for (auto k : c[j]) {
|
||||
c[i].insert(k);
|
||||
}
|
||||
}
|
||||
|
||||
ans[i] = c[i].size();
|
||||
};
|
||||
|
||||
dfs(0, 0);
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
88
CSES Problem Set/Distinct Numbers.cpp
Normal file
88
CSES Problem Set/Distinct Numbers.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1621 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
set<int> fds;
|
||||
while (n--) {
|
||||
int f;
|
||||
cin >> f;
|
||||
fds.insert(f);
|
||||
}
|
||||
|
||||
cout << fds.size() << '\n';
|
||||
}
|
||||
195
CSES Problem Set/Distinct Routes.cpp
Normal file
195
CSES Problem Set/Distinct Routes.cpp
Normal file
@@ -0,0 +1,195 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1711 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
V<tuple<int, ll, ll>> edges;
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(edges.size());
|
||||
edges.emplace_back(b, 1, 0);
|
||||
graph[b].push_back(edges.size());
|
||||
edges.emplace_back(a, 0, 0);
|
||||
}
|
||||
|
||||
int inf = INT32_MAX >> 1;
|
||||
|
||||
vi level(n);
|
||||
auto bfs = [&](){
|
||||
level[0] = 0;
|
||||
queue<int> q;
|
||||
q.push(0);
|
||||
|
||||
while (!q.empty()) {
|
||||
auto a = q.front();
|
||||
q.pop();
|
||||
|
||||
for (auto id : graph[a]) {
|
||||
auto [b, c, u] = edges[id];
|
||||
|
||||
if (level[b] > level[a] + 1 && c != u) {
|
||||
level[b] = level[a] + 1;
|
||||
q.push(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return level[n - 1] != inf;
|
||||
};
|
||||
|
||||
V<bool> vis(n);
|
||||
|
||||
function<ll(int, ll)> dfs = [&](int i, ll flux){
|
||||
if (flux == 0) {
|
||||
return 0LL;
|
||||
}
|
||||
|
||||
if (i == n - 1) {
|
||||
return flux;
|
||||
}
|
||||
|
||||
for (auto id : graph[i]) {
|
||||
auto &[j, c, u] = edges[id];
|
||||
|
||||
if (level[j] <= level[i] || vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ll ans = dfs(j, min(flux, c - u));
|
||||
|
||||
if (ans == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto &[_, __, ud] = edges[id ^ 1];
|
||||
|
||||
u += ans;
|
||||
ud -= ans;
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
vis[i] = true;
|
||||
return 0LL;
|
||||
};
|
||||
|
||||
ll ans = 0;
|
||||
while (true) {
|
||||
fill(all(level), inf);
|
||||
|
||||
if (!bfs()) {
|
||||
break;
|
||||
}
|
||||
|
||||
fill(all(vis), false);
|
||||
|
||||
ll count = 0;
|
||||
while ((count = dfs(0, inf))) {
|
||||
ans += count;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
|
||||
while (ans--) {
|
||||
vi tmp;
|
||||
|
||||
function<void(int)> dfs = [&](int i){
|
||||
tmp.push_back(i + 1);
|
||||
|
||||
for (auto id : graph[i]) {
|
||||
auto &[j, c, u] = edges[id];
|
||||
|
||||
if (u == 1) {
|
||||
u--;
|
||||
dfs(j);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
dfs(0);
|
||||
cout << tmp.size() << '\n';
|
||||
cout << tmp;
|
||||
tmp.clear();
|
||||
}
|
||||
}
|
||||
176
CSES Problem Set/Distinct Values Queries.cpp
Normal file
176
CSES Problem Set/Distinct Values Queries.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1734 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vl tmp(n);
|
||||
cin >> tmp;
|
||||
vl fds(n);
|
||||
set<ll> nums;
|
||||
rep(i, n) {
|
||||
nums.insert(tmp[i]);
|
||||
}
|
||||
|
||||
int v = 0;
|
||||
map<ll, int> var;
|
||||
repv(i, nums) {
|
||||
var[i] = v;
|
||||
v++;
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
fds[i] = var[tmp[i]];
|
||||
}
|
||||
|
||||
int len = (int)sqrt(n) + 1;
|
||||
|
||||
vi prev(n, -1);
|
||||
vi next(n, n);
|
||||
vl blocks(len, 0);
|
||||
V<bool> uniq(n, false);
|
||||
|
||||
rep(i, n) {
|
||||
if (prev[fds[i]] == -1) {
|
||||
uniq[i] = true;
|
||||
prev[fds[i]] = i;
|
||||
blocks[i / len]++;
|
||||
continue;
|
||||
}
|
||||
|
||||
next[prev[fds[i]]] = i;
|
||||
prev[fds[i]] = i;
|
||||
}
|
||||
|
||||
V<tuple<int, int, size_t>> query(q);
|
||||
size_t act = 0;
|
||||
for (auto &[l, r, i] : query) {
|
||||
cin >> l >> r;
|
||||
l--, r--;
|
||||
i = act;
|
||||
act++;
|
||||
}
|
||||
|
||||
sortv(query);
|
||||
|
||||
int ql = 0;
|
||||
vl ans(q);
|
||||
for (auto [l, r, i] : query) {
|
||||
while (ql < l) {
|
||||
blocks[ql / len]--;
|
||||
uniq[next[ql]] = true;
|
||||
blocks[next[ql] / len]++;
|
||||
ql++;
|
||||
}
|
||||
|
||||
if (l / len == r / len) {
|
||||
ll sum = 0;
|
||||
for (size_t i = l; i <= r; i++) {
|
||||
if (uniq[i]) {
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
ans[i] = sum;
|
||||
continue;
|
||||
}
|
||||
|
||||
ll sum = 0;
|
||||
int bl = l / len;
|
||||
int br = r / len;
|
||||
|
||||
for (int i = l, end = (bl + 1) * len - 1; i <= end; i++) {
|
||||
if (uniq[i]) {
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = bl + 1; i <= br - 1; i++) {
|
||||
sum += blocks[i];
|
||||
}
|
||||
|
||||
for (int i = br * len; i <= r; i++) {
|
||||
if (uniq[i]) {
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
|
||||
ans[i] = sum;
|
||||
}
|
||||
|
||||
rep(i, q) {
|
||||
cout << ans[i] << '\n';
|
||||
}
|
||||
}
|
||||
102
CSES Problem Set/Distinct Values Subarrays.cpp
Normal file
102
CSES Problem Set/Distinct Values Subarrays.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3420 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
set<ll> seen;
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
ll ans = 0;
|
||||
|
||||
while (i < n) {
|
||||
while (seen.count(fds[i])) {
|
||||
seen.erase(fds[j]);
|
||||
j++;
|
||||
}
|
||||
|
||||
seen.insert(fds[i]);
|
||||
i++;
|
||||
|
||||
ans += i - j;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
101
CSES Problem Set/Distinct Values Subsequences.cpp
Normal file
101
CSES Problem Set/Distinct Values Subsequences.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3421 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
map<ll, ll> var;
|
||||
while (n--) {
|
||||
ll num;
|
||||
cin >> num;
|
||||
var[num]++;
|
||||
}
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
ll ans = 0;
|
||||
|
||||
ll now = 0;
|
||||
|
||||
repv(i, var) {
|
||||
ans += i.second;
|
||||
ans += i.second * now;
|
||||
ans %= mod;
|
||||
|
||||
now = (now * i.second + now + i.second) % mod;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
117
CSES Problem Set/Distributing Apples.cpp
Normal file
117
CSES Problem Set/Distributing Apples.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1716 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
vl fact(2e6 + 1);
|
||||
fact[0] = 1;
|
||||
|
||||
nrep(i, 1, fact.size()) {
|
||||
fact[i] = (fact[i - 1] * i) % mod;
|
||||
}
|
||||
|
||||
auto fpow = [&](ll a, ll b) {
|
||||
ll ans = 1;
|
||||
ll now = a;
|
||||
|
||||
rep(i, 30) {
|
||||
if ((b >> i) & 1) {
|
||||
ans *= now;
|
||||
ans %= mod;
|
||||
}
|
||||
|
||||
now *= now;
|
||||
now %= mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
auto comb = [&](ll n, ll k) {
|
||||
return (fact[n] * fpow((fact[k] * fact[n - k]) % mod, mod - 2)) % mod;
|
||||
};
|
||||
|
||||
cout << comb(n + m - 1, m) << '\n';
|
||||
}
|
||||
173
CSES Problem Set/Download Speed.cpp
Normal file
173
CSES Problem Set/Download Speed.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1694 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
V<tuple<int, ll, ll>> edges;
|
||||
|
||||
while (m--) {
|
||||
ll a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(edges.size());
|
||||
edges.emplace_back(b, c, 0);
|
||||
graph[b].push_back(edges.size());
|
||||
edges.emplace_back(a, 0, 0);
|
||||
}
|
||||
|
||||
int inf = INT32_MAX >> 1;
|
||||
auto flux = [&](){
|
||||
vi level(n);
|
||||
|
||||
auto bfs = [&](){
|
||||
queue<int> q;
|
||||
level[0] = 0;
|
||||
q.push(0);
|
||||
|
||||
|
||||
while (!q.empty()) {
|
||||
int i = q.front();
|
||||
q.pop();
|
||||
|
||||
for (auto id : graph[i]) {
|
||||
auto [j, c, u] = edges[id];
|
||||
if (level[i] + 1 < level[j] && c != u) {
|
||||
level[j] = level[i] + 1;
|
||||
q.push(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return level[n - 1] != inf;
|
||||
};
|
||||
|
||||
V<bool> vis(n);
|
||||
|
||||
function<ll(int, ll)> dfs = [&](int i, ll flux){
|
||||
if (flux == 0) {
|
||||
return 0LL;
|
||||
}
|
||||
|
||||
if (i == n - 1) {
|
||||
return flux;
|
||||
}
|
||||
|
||||
for (auto id : graph[i]) {
|
||||
auto &[j, c, u] = edges[id];
|
||||
|
||||
if (level[i] >= level[j] || vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ll ans = dfs(j, min(flux, c - u));
|
||||
|
||||
if (ans == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto &[_, __, ud] = edges[id ^ 1];
|
||||
|
||||
u += ans;
|
||||
ud -= ans;
|
||||
return ans;
|
||||
}
|
||||
|
||||
vis[i] = true;
|
||||
return 0LL;
|
||||
};
|
||||
|
||||
ll ans = 0;
|
||||
while (true) {
|
||||
fill(all(level), inf);
|
||||
if (!bfs()) {
|
||||
break;
|
||||
}
|
||||
|
||||
fill(all(vis), false);
|
||||
ll pushed = 0;
|
||||
while ((pushed = dfs(0, inf))) {
|
||||
ans += pushed;
|
||||
}
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << flux() << '\n';
|
||||
}
|
||||
103
CSES Problem Set/Exponentiation II.cpp
Normal file
103
CSES Problem Set/Exponentiation II.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1712 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
ll fastpow(ll n, ll p, ll mod)
|
||||
{
|
||||
ll total = n;
|
||||
ll ans = 1;
|
||||
|
||||
for (size_t i = 0; i < 30; i++) {
|
||||
if (p & (1 << i)) {
|
||||
ans *= total;
|
||||
ans %= mod;
|
||||
}
|
||||
total *= total;
|
||||
total %= mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
ll a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
|
||||
ll p = fastpow(b, c, 1e9 + 6);
|
||||
cout << fastpow(a, p, 1e9 + 7) << '\n';
|
||||
}
|
||||
}
|
||||
108
CSES Problem Set/Exponentiation.cpp
Normal file
108
CSES Problem Set/Exponentiation.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1095 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
ll m = 1e9 + 7;
|
||||
|
||||
ll fastpow(ll a, ll p)
|
||||
{
|
||||
if (p == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
ll ans = 1;
|
||||
ll total = a;
|
||||
for (size_t i = 0; i < 30; i++) {
|
||||
if (p & (1 << i)) {
|
||||
ans *= total;
|
||||
ans %= m;
|
||||
}
|
||||
total *= total;
|
||||
total %= m;
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
while (n--) {
|
||||
ll a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
cout << fastpow(a, b) << '\n';
|
||||
}
|
||||
}
|
||||
108
CSES Problem Set/Factory Machines.cpp
Normal file
108
CSES Problem Set/Factory Machines.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1620 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
ll n, t;
|
||||
cin >> n >> t;
|
||||
|
||||
vl machines(n);
|
||||
cin >> machines;
|
||||
|
||||
ll ans = 0;
|
||||
ll low = 0;
|
||||
ll high = INT64_MAX;
|
||||
|
||||
while (low <= high) {
|
||||
ll mid = (high - low) / 2 + low;
|
||||
|
||||
ll sum = 0;
|
||||
repv(i, machines) {
|
||||
sum += mid / i;
|
||||
if (sum >= t) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sum >= t) {
|
||||
ans = mid;
|
||||
high = mid - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
low = mid + 1;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
110
CSES Problem Set/Ferris Wheel.cpp
Normal file
110
CSES Problem Set/Ferris Wheel.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1090 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
ll n, x;
|
||||
cin >> n >> x;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
int ans = 0;
|
||||
ll now = 0;
|
||||
|
||||
sortv(fds);
|
||||
|
||||
int i = 0;
|
||||
int j = n - 1;
|
||||
|
||||
int get = 0;
|
||||
|
||||
while (i <= j) {
|
||||
if (i == j) {
|
||||
ans++;
|
||||
break;
|
||||
}
|
||||
|
||||
if (fds[i] + fds[j] <= x) {
|
||||
i++;
|
||||
j--;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
|
||||
ans++;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
151
CSES Problem Set/Fibonacci Numbers.cpp
Normal file
151
CSES Problem Set/Fibonacci Numbers.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1722 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n;
|
||||
cin >> n;
|
||||
|
||||
if (n == 0) {
|
||||
cout << "0\n";
|
||||
return;
|
||||
}
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
auto mul = [&](vvl &a, vvl &b) {
|
||||
vvl c(a.size(), vl(b[0].size()));
|
||||
|
||||
rep(i, c.size()) {
|
||||
rep(j, c[i].size()) {
|
||||
rep(k, b.size()) {
|
||||
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
};
|
||||
|
||||
auto fpow = [&](ll p) {
|
||||
vvl ans = {
|
||||
{0},
|
||||
{1}
|
||||
};
|
||||
|
||||
vvl po = {
|
||||
{0, 1},
|
||||
{1, 1}
|
||||
};
|
||||
|
||||
rep(i, 61) {
|
||||
if ((p >> i) & 1) {
|
||||
ans = mul(po, ans);
|
||||
}
|
||||
po = mul(po, po);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << fpow(n - 1)[1] << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
128
CSES Problem Set/Finding a Centroid.cpp
Normal file
128
CSES Problem Set/Finding a Centroid.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2079 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vvi graph(n);
|
||||
rep(i, n - 1) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
a--, b--;
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
vi sizes(n);
|
||||
function<int(int, int)> calc = [&](int i, int p) {
|
||||
sizes[i] = 1;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sizes[i] += calc(j, i);
|
||||
}
|
||||
|
||||
return sizes[i];
|
||||
};
|
||||
|
||||
calc(0, 0);
|
||||
|
||||
function<int(int)> dfs = [&](int i) {
|
||||
sizes[i] = 1;
|
||||
int choice = -1;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (sizes[j] > n / 2) {
|
||||
choice = j;
|
||||
continue;
|
||||
}
|
||||
|
||||
sizes[i] += sizes[j];
|
||||
}
|
||||
|
||||
if (choice == -1) {
|
||||
return i;
|
||||
}
|
||||
|
||||
return dfs(choice);
|
||||
};
|
||||
|
||||
cout << dfs(0) + 1 << '\n';
|
||||
}
|
||||
149
CSES Problem Set/Flight Routes Check.cpp
Normal file
149
CSES Problem Set/Flight Routes Check.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1682 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
vvi inv(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
inv[b].push_back(a);
|
||||
}
|
||||
|
||||
V<bool> vis(n);
|
||||
stack<int> s;
|
||||
|
||||
function<void(int)> postorder = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
postorder(j);
|
||||
}
|
||||
|
||||
s.push(i);
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
postorder(i);
|
||||
}
|
||||
|
||||
int choice = s.top();
|
||||
fill(all(vis), false);
|
||||
|
||||
function<void(int)> dfs = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
for (auto j : inv[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(j);
|
||||
}
|
||||
};
|
||||
|
||||
dfs(choice);
|
||||
|
||||
rep(i, n) {
|
||||
if (!vis[i]) {
|
||||
cout << "NO\n";
|
||||
cout << i + 1 << ' ' << choice + 1 << '\n';
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "YES\n";
|
||||
}
|
||||
124
CSES Problem Set/Flight Routes.cpp
Normal file
124
CSES Problem Set/Flight Routes.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1196 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m, k;
|
||||
cin >> n >> m >> k;
|
||||
|
||||
V<V<pair<int, ll>>> graph(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
a--, b--;
|
||||
|
||||
graph[a].emplace_back(b, c);
|
||||
}
|
||||
|
||||
ll inf = INT64_MAX >> 1;
|
||||
V<multiset<ll>> dis(n, multiset<ll>({inf}));
|
||||
|
||||
auto dijkstra = [&]() {
|
||||
dis[0].insert(0);
|
||||
priority_queue<pair<ll, int>, V<pair<ll, int>>, greater<>> pq;
|
||||
pq.emplace(0, 0);
|
||||
|
||||
while (!pq.empty()) {
|
||||
auto [c, i] = pq.top();
|
||||
pq.pop();
|
||||
|
||||
if (c > *prev(dis[i].end())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto [j, cc] : graph[i]) {
|
||||
if (dis[j].size() < k || c + cc < *prev(dis[j].end())) {
|
||||
dis[j].insert(c + cc);
|
||||
pq.emplace(c + cc, j);
|
||||
if (dis[j].size() > k) {
|
||||
dis[j].erase(prev(dis[j].end()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
dijkstra();
|
||||
|
||||
for (auto i : dis.back()) {
|
||||
cout << i << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
136
CSES Problem Set/Forest Queries.cpp
Normal file
136
CSES Problem Set/Forest Queries.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1652 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vvi count(n, vi(n));
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, n) {
|
||||
char c;
|
||||
cin >> c;
|
||||
|
||||
if (c == '*') {
|
||||
count[i][j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, n) {
|
||||
if (i > 0) {
|
||||
count[i][j] += count[i - 1][j];
|
||||
}
|
||||
|
||||
if (j > 0) {
|
||||
count[i][j] += count[i][j - 1];
|
||||
}
|
||||
|
||||
if (i > 0 && j > 0) {
|
||||
count[i][j] -= count[i - 1][j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int x1, y1, x2, y2;
|
||||
cin >> x1 >> y1 >> x2 >> y2;
|
||||
x1--, y1--, x2--, y2--;
|
||||
|
||||
int ans = count[x2][y2];
|
||||
|
||||
if (x1 > 0) {
|
||||
ans -= count[x1 - 1][y2];
|
||||
}
|
||||
|
||||
if (y1 > 0) {
|
||||
ans -= count[x2][y1 - 1];
|
||||
}
|
||||
|
||||
if (x1 > 0 && y1 > 0) {
|
||||
ans += count[x1 - 1][y1 - 1];
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
}
|
||||
114
CSES Problem Set/Game Routes.cpp
Normal file
114
CSES Problem Set/Game Routes.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1681 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
graph[a - 1].push_back(b - 1);
|
||||
}
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
vl dp(n, -1);
|
||||
|
||||
function<ll(int)> dfs = [&](int i) {
|
||||
if (i == n - 1) {
|
||||
dp[i] = 1;
|
||||
return dp[i];
|
||||
}
|
||||
|
||||
if (dp[i] != -1) {
|
||||
return dp[i];
|
||||
}
|
||||
|
||||
dp[i] = 0;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
dp[i] += dfs(j);
|
||||
dp[i] %= mod;
|
||||
}
|
||||
|
||||
return dp[i];
|
||||
};
|
||||
|
||||
cout << dfs(0) << '\n';
|
||||
}
|
||||
207
CSES Problem Set/Giant Pizza.cpp
Normal file
207
CSES Problem Set/Giant Pizza.cpp
Normal file
@@ -0,0 +1,207 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1684 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> m >> n;
|
||||
|
||||
n <<= 1;
|
||||
|
||||
vvi graph(n);
|
||||
vvi inv(n);
|
||||
|
||||
while (m--) {
|
||||
char op1, op2;
|
||||
int a, b;
|
||||
cin >> op1 >> a >> op2 >> b;
|
||||
|
||||
a--, b--;
|
||||
|
||||
if (op1 == '-') {
|
||||
a += n >> 1;
|
||||
}
|
||||
|
||||
if (op2 == '-') {
|
||||
b += n >> 1;
|
||||
}
|
||||
|
||||
graph[a].push_back((b + (n >> 1)) % n);
|
||||
graph[b].push_back((a + (n >> 1)) % n);
|
||||
inv[(b + (n >> 1)) % n].push_back(a);
|
||||
inv[(a + (n >> 1)) % n].push_back(b);
|
||||
}
|
||||
|
||||
V<bool> vis(n);
|
||||
vi top;
|
||||
|
||||
function<void(int)> preorder = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
preorder(j);
|
||||
}
|
||||
|
||||
top.push_back(i);
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
preorder(i);
|
||||
}
|
||||
|
||||
reverse(all(top));
|
||||
fill(all(vis), false);
|
||||
|
||||
vi group(n);
|
||||
vi representant(n);
|
||||
int g = 0;
|
||||
|
||||
function<void(int)> scc = [&](int i) {
|
||||
vis[i] = true;
|
||||
group[i] = g;
|
||||
|
||||
for (auto j : inv[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
scc(j);
|
||||
}
|
||||
};
|
||||
|
||||
repv(i, top) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
scc(i);
|
||||
representant.push_back(i);
|
||||
g++;
|
||||
}
|
||||
|
||||
rep(i, n >> 1) {
|
||||
if (group[i] == group[i + (n >> 1)]) {
|
||||
cout << "IMPOSSIBLE\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
fill(all(vis), false);
|
||||
reverse(all(representant));
|
||||
|
||||
string ans(n >> 1, ' ');
|
||||
|
||||
function<void(int)> getans = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
if (ans[i % (n >> 1)] == ' ') {
|
||||
ans[i % (n >> 1)] = i < (n >> 1) ? '-' : '+';
|
||||
}
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (vis[j] || group[j] != group[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
getans(j);
|
||||
}
|
||||
};
|
||||
|
||||
repv(i, representant) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
getans(i);
|
||||
}
|
||||
|
||||
repv(i, ans) {
|
||||
cout << i << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
152
CSES Problem Set/Graph Paths I.cpp
Normal file
152
CSES Problem Set/Graph Paths I.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1723 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m, k;
|
||||
cin >> n >> m >> k;
|
||||
|
||||
vvl adj(n, vl(n));
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
adj[a - 1][b - 1]++;
|
||||
}
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
auto mul = [&](vvl &a, vvl &b) {
|
||||
vvl c(a.size(), vl(b[0].size()));
|
||||
|
||||
rep(i, c.size()) {
|
||||
rep(j, c[0].size()) {
|
||||
rep(k, b.size()) {
|
||||
c[i][j] += a[i][k] * b[k][j];
|
||||
c[i][j] %= mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
};
|
||||
|
||||
auto fpow = [&]() {
|
||||
vvl ans(n, vl(n));
|
||||
rep(i, n) {
|
||||
ans[i][i] = 1;
|
||||
}
|
||||
|
||||
rep(i, 30) {
|
||||
if ((k >> i) & 1) {
|
||||
ans = mul(ans, adj);
|
||||
}
|
||||
adj = mul(adj, adj);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
auto ans = fpow();
|
||||
|
||||
cout << ans[0][n - 1] << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
149
CSES Problem Set/Graph Paths II.cpp
Normal file
149
CSES Problem Set/Graph Paths II.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1724 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m, k;
|
||||
cin >> n >> m >> k;
|
||||
|
||||
vvl adj(n, vl(n, OO));
|
||||
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
a--, b--;
|
||||
rmin(adj[a][b], (ll)c);
|
||||
}
|
||||
|
||||
auto mul = [&](vvl &a, vvl &b) {
|
||||
vvl c(a.size(), vl(b[0].size(), OO));
|
||||
|
||||
rep(i, c.size()) {
|
||||
rep(j, c[0].size()) {
|
||||
rep(k, b.size()) {
|
||||
rmin(c[i][j], a[i][k] + b[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
};
|
||||
|
||||
auto fpow = [&]() {
|
||||
vvl ans(n, vl(n, OO));
|
||||
rep(i, n) {
|
||||
ans[i][i] = 0;
|
||||
}
|
||||
|
||||
rep(i, 30) {
|
||||
if ((k >> i) & 1) {
|
||||
ans = mul(ans, adj);
|
||||
}
|
||||
adj = mul(adj, adj);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
ll ans = fpow()[0][n - 1];
|
||||
cout << (ans == OO ? -1 : ans) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
89
CSES Problem Set/Gray Code.cpp
Normal file
89
CSES Problem Set/Gray Code.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2205 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
rep(i, (1 << n)) {
|
||||
int act = i ^ (i >> 1);
|
||||
|
||||
for (int j = n - 1; j >= 0; j--) {
|
||||
int bit = (act >> j) & 1;
|
||||
cout << (char)(bit + '0');
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
}
|
||||
100
CSES Problem Set/Grid Coloring I.cpp
Normal file
100
CSES Problem Set/Grid Coloring I.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3311 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<string> fds(n);
|
||||
cin >> fds;
|
||||
|
||||
V<string> ans(n, string(m, ' '));
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, m) {
|
||||
int now = 0;
|
||||
char act;
|
||||
while (act = now + 'A', act == fds[i][j] || (i > 0 && act == ans[i - 1][j]) || (j > 0 && act == ans[i][j - 1])) {
|
||||
now++;
|
||||
}
|
||||
|
||||
ans[i][j] = act;
|
||||
}
|
||||
}
|
||||
|
||||
repv(i, ans) {
|
||||
cout << i << '\n';
|
||||
}
|
||||
}
|
||||
145
CSES Problem Set/Grid Path Description.cpp
Normal file
145
CSES Problem Set/Grid Path Description.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1625 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
char a[49];
|
||||
bool frame[7][7];
|
||||
|
||||
int func(int i, int j, int pos)
|
||||
{
|
||||
if (pos >= 48) {
|
||||
return (i == 6 && j == 0);
|
||||
}
|
||||
|
||||
if (i == 6 && j == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((i == 0 || frame[i - 1][j]) && (i == 6 || frame[i + 1][j]) && (j != 0 && !frame[i][j - 1]) && (j != 6 && !frame[i][j + 1])) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((j == 0 || frame[i][j - 1]) && (j == 6 || frame[i][j + 1]) && (i != 0 && !frame[i - 1][j]) && (i != 6 && !frame[i + 1][j])) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
frame[i][j] = true;
|
||||
|
||||
if (a[pos] == '?') {
|
||||
int add[][4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
|
||||
int ans = 0;
|
||||
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id >= 0 && id < 7 && jd >= 0 && jd < 7 && !frame[id][jd]) {
|
||||
ans += func(id, jd, pos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
frame[i][j] = false;
|
||||
return ans;
|
||||
}
|
||||
|
||||
int id = i;
|
||||
int jd = j;
|
||||
switch (a[pos]) {
|
||||
case 'U':
|
||||
id--;
|
||||
break;
|
||||
case 'D':
|
||||
id++;
|
||||
break;
|
||||
case 'L':
|
||||
jd--;
|
||||
break;
|
||||
case 'R':
|
||||
jd++;
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
if (id >= 0 && id < 7 && jd >= 0 && jd < 7 && !frame[id][jd]) {
|
||||
ans = func(id, jd, pos + 1);
|
||||
}
|
||||
|
||||
frame[i][j] = false;
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
scanf("%s", a);
|
||||
|
||||
printf("%d\n", func(0, 0, 0));
|
||||
}
|
||||
121
CSES Problem Set/Hamiltonian Flights.cpp
Normal file
121
CSES Problem Set/Hamiltonian Flights.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1690 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
graph[a - 1].push_back(b - 1);
|
||||
}
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
vvl memo(n, vl(1 << n, -1));
|
||||
|
||||
function<ll(int, ll)> dp = [&](int i, ll mask) -> ll {
|
||||
if (i == n - 1) {
|
||||
return mask == 0;
|
||||
}
|
||||
|
||||
ll &ans = memo[i][mask];
|
||||
if (ans != -1) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
ans = 0;
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (mask & (1 << j)) {
|
||||
ans += dp(j, mask ^ (1 << j));
|
||||
ans %= mod;
|
||||
}
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << dp(0, ((1 << n) - 1) ^ 1) << '\n';
|
||||
}
|
||||
147
CSES Problem Set/High Score.cpp
Normal file
147
CSES Problem Set/High Score.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1673 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<tuple<int, int, ll>> edges;
|
||||
V<V<pair<ll, int>>> graph(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
a--, b--;
|
||||
|
||||
edges.emplace_back(a, b, -c);
|
||||
graph[a].emplace_back(b, c);
|
||||
}
|
||||
|
||||
V<bool> reachable(n, false);
|
||||
V<bool> vis(n, false);
|
||||
|
||||
reachable.back() = true;
|
||||
|
||||
function<bool(int)> dfs = [&](int i) -> bool {
|
||||
vis[i] = true;
|
||||
|
||||
for (auto [j, c] : graph[i]) {
|
||||
if (reachable[j]) {
|
||||
reachable[i] = true;
|
||||
}
|
||||
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dfs(j)) {
|
||||
reachable[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return reachable[i];
|
||||
};
|
||||
|
||||
dfs(0);
|
||||
|
||||
auto bellman = [&]() -> ll {
|
||||
ll inf = INT64_MAX >> 1;
|
||||
vl dis(n, inf);
|
||||
dis[0] = 0;
|
||||
|
||||
rep(i, n - 1) {
|
||||
for (auto [a, b, c] : edges) {
|
||||
if (dis[a] < inf) {
|
||||
rmin(dis[b], dis[a] + c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto [a, b, c] : edges) {
|
||||
if (dis[a] < inf && dis[b] > dis[a] + c) {
|
||||
if (reachable[b]) {
|
||||
return -1;
|
||||
}
|
||||
dis[b] = dis[a] + c;
|
||||
}
|
||||
}
|
||||
|
||||
return -dis.back();
|
||||
};
|
||||
|
||||
cout << bellman() << '\n';
|
||||
}
|
||||
131
CSES Problem Set/Increasing Subsequence II.cpp
Normal file
131
CSES Problem Set/Increasing Subsequence II.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1748 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
map<int, int> var;
|
||||
vi fds(n);
|
||||
repv(i, fds) {
|
||||
cin >> i;
|
||||
var[i] = 0;
|
||||
}
|
||||
|
||||
int v = 0;
|
||||
repv(i, var) {
|
||||
i.second = v;
|
||||
v++;
|
||||
}
|
||||
|
||||
repv(i, fds) {
|
||||
i = var[i];
|
||||
}
|
||||
|
||||
while (__builtin_popcount(fds.size()) != 1) {
|
||||
fds.push_back(0);
|
||||
}
|
||||
|
||||
vl seg(fds.size() * 2);
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
auto update = [&](int i, ll v) {
|
||||
seg[i + fds.size()] += v;
|
||||
seg[i + fds.size()] %= mod;
|
||||
for (i = (i + fds.size()) / 2; i > 0; i /= 2) {
|
||||
seg[i] = (seg[i * 2] + seg[i * 2 + 1]) % mod;
|
||||
}
|
||||
};
|
||||
|
||||
function<ll(int, int, int, int, int)> search = [&](int i, int l, int r, int tl, int tr) -> ll {
|
||||
if (l > tr || r < tl) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return seg[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) / 2;
|
||||
return (search(i * 2, l, mid, tl, tr) + search(i * 2 + 1, mid + 1, r, tl, tr)) % mod;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
ll tmp = search(1, 0, fds.size() - 1, 0, fds[i] - 1) + 1;
|
||||
update(fds[i], tmp);
|
||||
}
|
||||
|
||||
cout << search(1, 0, n - 1, 0, n - 1) << '\n';
|
||||
}
|
||||
195
CSES Problem Set/Investigation.cpp
Normal file
195
CSES Problem Set/Investigation.cpp
Normal file
@@ -0,0 +1,195 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1202 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<V<pair<int, ll>>> graph(n);
|
||||
V<V<pair<int, ll>>> rev(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
a--, b--;
|
||||
|
||||
graph[a].emplace_back(b, c);
|
||||
rev[b].emplace_back(a, c);
|
||||
}
|
||||
|
||||
ll inf = INT64_MAX >> 1;
|
||||
vl dis(n, inf);
|
||||
|
||||
auto dijkstra = [&]() {
|
||||
priority_queue<pair<ll, int>, V<pair<ll, int>>, greater<>> pq;
|
||||
dis[0] = 0;
|
||||
pq.emplace(0, 0);
|
||||
|
||||
while (!pq.empty()) {
|
||||
auto [c, i] = pq.top();
|
||||
pq.pop();
|
||||
|
||||
if (c > dis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto [j, cc] : graph[i]) {
|
||||
if (dis[j] > c + cc) {
|
||||
dis[j] = c + cc;
|
||||
pq.emplace(dis[j], j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dis.back();
|
||||
};
|
||||
|
||||
ll ans = dijkstra();
|
||||
|
||||
vl dp1(n, -1);
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
function<ll(int, ll)> dfs1 = [&](int i, ll c) {
|
||||
if (i == 0) {
|
||||
dp1[i] = 1;
|
||||
return dp1[i];
|
||||
}
|
||||
|
||||
if (dp1[i] != -1) {
|
||||
return dp1[i];
|
||||
}
|
||||
|
||||
dp1[i] = 0;
|
||||
|
||||
for (auto [j, cc] : rev[i]) {
|
||||
if (dis[j] == c - cc) {
|
||||
dp1[i] += dfs1(j, c - cc);
|
||||
dp1[i] %= mod;
|
||||
}
|
||||
}
|
||||
|
||||
return dp1[i];
|
||||
};
|
||||
|
||||
vi dp2(n, -1);
|
||||
function<int(int, ll)> dfsmax = [&](int i, ll c) {
|
||||
if (i == 0) {
|
||||
dp2[i] = 1;
|
||||
return dp2[i];
|
||||
}
|
||||
|
||||
if (dp2[i] != -1) {
|
||||
return dp2[i];
|
||||
}
|
||||
|
||||
dp2[i] = 0;
|
||||
|
||||
for (auto [j, cc] : rev[i]) {
|
||||
if (dis[j] == c - cc) {
|
||||
int res = dfsmax(j, c - cc);
|
||||
if (res > 0) {
|
||||
rmax(dp2[i], res + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp2[i];
|
||||
};
|
||||
|
||||
vi dp3(n, -1);
|
||||
function<int(int, ll)> dfsmin = [&](int i, ll c) {
|
||||
if (i == 0) {
|
||||
dp3[i] = 1;
|
||||
return dp3[i];
|
||||
}
|
||||
|
||||
if (dp3[i] != -1) {
|
||||
return dp3[i];
|
||||
}
|
||||
|
||||
dp3[i] = INT32_MAX >> 1;
|
||||
|
||||
for (auto [j, cc] : rev[i]) {
|
||||
if (dis[j] == c - cc) {
|
||||
rmin(dp3[i], dfsmin(j, c - cc) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return dp3[i];
|
||||
};
|
||||
|
||||
cout << ans << ' ' << dfs1(n - 1, ans) << ' ' << dfsmin(n - 1, ans) - 1 << ' ' << dfsmax(n - 1, ans) - 1 << '\n';
|
||||
}
|
||||
100
CSES Problem Set/Josephus Problem I.cpp
Normal file
100
CSES Problem Set/Josephus Problem I.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2162 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
list<int> children;
|
||||
rep(i, n) {
|
||||
children.push_back(i + 1);
|
||||
}
|
||||
|
||||
auto itr = children.begin();
|
||||
while (!children.empty()) {
|
||||
itr++;
|
||||
if (itr == children.end()) {
|
||||
itr = children.begin();
|
||||
}
|
||||
|
||||
cout << *itr << ' ';
|
||||
itr = children.erase(itr);
|
||||
if (itr == children.end()) {
|
||||
itr = children.begin();
|
||||
}
|
||||
}
|
||||
|
||||
cout << '\n';
|
||||
}
|
||||
100
CSES Problem Set/Josephus Problem II.cpp
Normal file
100
CSES Problem Set/Josephus Problem II.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2163 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
#include <ext/pb_ds/detail/standard_policies.hpp>
|
||||
|
||||
using namespace __gnu_pbds;
|
||||
using namespace std;
|
||||
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
ll now = 0;
|
||||
ordered_set set;
|
||||
rep(i, n) {
|
||||
set.insert(i + 1);
|
||||
}
|
||||
|
||||
while (!set.empty()) {
|
||||
now = (now + k) % set.size();
|
||||
auto itr = set.find_by_order(now);
|
||||
cout << *itr << ' ';
|
||||
set.erase(itr);
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
92
CSES Problem Set/Josephus Queries.cpp
Normal file
92
CSES Problem Set/Josephus Queries.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2164 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
ll count = 1;
|
||||
while (k > n / 2 + n % 2) {
|
||||
k -= n / 2 + n % 2;
|
||||
n /= 2;
|
||||
count <<= 1;
|
||||
}
|
||||
|
||||
cout << k * count << '\n';
|
||||
}
|
||||
}
|
||||
105
CSES Problem Set/Knight Moves Grid.cpp
Normal file
105
CSES Problem Set/Knight Moves Grid.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3217 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
int inf = INT32_MAX >> 1;
|
||||
vvi dis(n, vi(n, inf));
|
||||
|
||||
dis[0][0] = 0;
|
||||
queue<pair<int, int>> q;
|
||||
q.emplace(0, 0);
|
||||
|
||||
while (!q.empty()) {
|
||||
auto [i, j] = q.front();
|
||||
q.pop();
|
||||
|
||||
int add[][2] = {{2, 1}, {-2, 1}, {1, 2}, {-1, 2}, {2, -1}, {1, -2}, {-2, -1}, {-1, -2}};
|
||||
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id >= 0 && id < n && jd >= 0 && jd < n && dis[id][jd] > dis[i][j] + 1) {
|
||||
dis[id][jd] = dis[i][j] + 1;
|
||||
q.emplace(id, jd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << dis;
|
||||
}
|
||||
173
CSES Problem Set/Knight's Tour.cpp
Normal file
173
CSES Problem Set/Knight's Tour.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1689 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int x, y;
|
||||
cin >> x >> y;
|
||||
x--, y--;
|
||||
|
||||
int n = 8;
|
||||
|
||||
vvi ans(n, vi(n));
|
||||
|
||||
vvi count(n, vi(n));
|
||||
|
||||
int add[][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
|
||||
|
||||
auto check = [&](int i, int j) {
|
||||
return i >= 0 && i < n && j >= 0 && j < n;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, n) {
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (check(id, jd)) {
|
||||
count[id][jd]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
swap(x, y);
|
||||
|
||||
int oo = INT32_MAX >> 1;
|
||||
|
||||
int curx = x;
|
||||
int cury = y;
|
||||
ans[x][y] = 1;
|
||||
|
||||
function<bool(int, int)> dfs = [&](int i, int j) {
|
||||
if (ans[i][j] == 64) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int prev = count[i][j];
|
||||
|
||||
count[i][j] = oo;
|
||||
|
||||
priority_queue<tuple<int, int, int>, V<tuple<int, int, int>>, greater<>> pq;
|
||||
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (check(id, jd)) {
|
||||
count[id][jd]--;
|
||||
pq.emplace(count[id][jd], id, jd);
|
||||
}
|
||||
}
|
||||
|
||||
while (!pq.empty()) {
|
||||
auto [ty, id, jd] = pq.top();
|
||||
pq.pop();
|
||||
|
||||
if (ty > 8) {
|
||||
break;
|
||||
}
|
||||
|
||||
ans[id][jd] = ans[i][j] + 1;
|
||||
|
||||
if (dfs(id, jd)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (check(id, jd)) {
|
||||
count[id][jd]++;
|
||||
}
|
||||
}
|
||||
|
||||
count[i][j] = prev;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
dfs(x, y);
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
163
CSES Problem Set/Labyrinth.cpp
Normal file
163
CSES Problem Set/Labyrinth.cpp
Normal file
@@ -0,0 +1,163 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1193 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<string> fds(n);
|
||||
cin >> fds;
|
||||
|
||||
int is;
|
||||
int js;
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, m) {
|
||||
if (fds[i][j] == 'A') {
|
||||
is = i;
|
||||
js = j;
|
||||
goto leave;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
leave:
|
||||
|
||||
string ans;
|
||||
auto bfs = [&](int i, int j) {
|
||||
queue<pair<int, int>> q;
|
||||
q.emplace(i, j);
|
||||
|
||||
while (!q.empty()) {
|
||||
auto [i, j] = q.front();
|
||||
q.pop();
|
||||
|
||||
int add[][3] = {{0, 1, 0}, {1, 0, 1}, {0, -1, 2}, {-1, 0, 3}};
|
||||
|
||||
for (auto &k : add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id >= 0 && id < n && jd >= 0 && jd < m) {
|
||||
if (fds[id][jd] == 'B') {
|
||||
char var[] = {'R', 'D', 'L', 'U'};
|
||||
ans.push_back(var[k[2]]);
|
||||
return make_pair(i, j);
|
||||
}
|
||||
|
||||
if (fds[id][jd] == '.') {
|
||||
fds[id][jd] = k[2];
|
||||
q.emplace(id, jd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return make_pair(-1, -1);
|
||||
};
|
||||
|
||||
auto [i, j] = bfs(is, js);
|
||||
|
||||
if (i == -1) {
|
||||
cout << "NO\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << "YES\n";
|
||||
|
||||
while (fds[i][j] != 'A') {
|
||||
switch(fds[i][j]) {
|
||||
case 0:
|
||||
ans.push_back('R');
|
||||
j--;
|
||||
break;
|
||||
case 1:
|
||||
ans.push_back('D');
|
||||
i--;
|
||||
break;
|
||||
case 2:
|
||||
ans.push_back('L');
|
||||
j++;
|
||||
break;
|
||||
case 3:
|
||||
ans.push_back('U');
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
reverse(all(ans));
|
||||
cout << ans.size() << '\n' << ans << '\n';
|
||||
}
|
||||
104
CSES Problem Set/List Removals.cpp
Normal file
104
CSES Problem Set/List Removals.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1749 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
ordered_set<pair<int, int>> ord;
|
||||
|
||||
rep(i, n) {
|
||||
int x;
|
||||
cin >> x;
|
||||
|
||||
ord.insert({i, x});
|
||||
}
|
||||
|
||||
while (n--) {
|
||||
int i;
|
||||
cin >> i;
|
||||
i--;
|
||||
|
||||
auto itr = ord.find_by_order(i);
|
||||
cout << itr->second << " \n"[n == 0];
|
||||
ord.erase(itr);
|
||||
}
|
||||
}
|
||||
126
CSES Problem Set/Longest Common Subsequence.cpp
Normal file
126
CSES Problem Set/Longest Common Subsequence.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3403 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vi a(n);
|
||||
vi b(m);
|
||||
cin >> a >> b;
|
||||
|
||||
vvi dp(n + 1, vi(m + 1));
|
||||
|
||||
nrep(i, 1, n + 1) {
|
||||
nrep(j, 1, m + 1) {
|
||||
if (a[i - 1] == b[j - 1]) {
|
||||
dp[i][j] = dp[i - 1][j - 1] + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
cout << dp[n][m] << '\n';
|
||||
// if (dp[n][m] == 0) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
vi ans(dp[n][m]);
|
||||
int act = dp[n][m] - 1;
|
||||
|
||||
int i = n;
|
||||
int j = m;
|
||||
|
||||
while (i > 0 && j > 0) {
|
||||
if (a[i - 1] == b[j - 1]) {
|
||||
ans[act] = a[i - 1];
|
||||
i--;
|
||||
j--;
|
||||
act--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dp[i - 1][j] > dp[i][j - 1]) {
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
|
||||
j--;
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
132
CSES Problem Set/Longest Flight Route.cpp
Normal file
132
CSES Problem Set/Longest Flight Route.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1680 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
graph[a - 1].push_back(b - 1);
|
||||
}
|
||||
|
||||
vi dp(n, -1);
|
||||
|
||||
function<int(int)> dfs = [&](int i) {
|
||||
if (i == n - 1) {
|
||||
dp[i] = 1;
|
||||
return dp[i];
|
||||
}
|
||||
|
||||
if (dp[i] != -1) {
|
||||
return dp[i];
|
||||
}
|
||||
|
||||
dp[i] = 0;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
int res = dfs(j);
|
||||
if (res > 0) {
|
||||
rmax(dp[i], res + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return dp[i];
|
||||
};
|
||||
|
||||
if (dfs(0) == 0) {
|
||||
cout << "IMPOSSIBLE\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
vi ans = {1};
|
||||
|
||||
int now = 0;
|
||||
while (now != n - 1) {
|
||||
for (auto j : graph[now]) {
|
||||
if (dp[j] == dp[now] - 1) {
|
||||
ans.push_back(j + 1);
|
||||
now = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans.size() << '\n' << ans;
|
||||
}
|
||||
130
CSES Problem Set/Mail Delivery.cpp
Normal file
130
CSES Problem Set/Mail Delivery.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1691 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<V<pair<int, int>>> graph(n);
|
||||
V<bool> used(m);
|
||||
rep(i, m) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].emplace_back(b, i);
|
||||
graph[b].emplace_back(a, i);
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
if (graph[i].size() & 1) {
|
||||
cout << "IMPOSSIBLE\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
vi ans;
|
||||
stack<int> euler;
|
||||
euler.push(0);
|
||||
while (!euler.empty()) {
|
||||
int i = euler.top();
|
||||
while (!graph[i].empty() && used[graph[i].back().second]) {
|
||||
graph[i].pop_back();
|
||||
}
|
||||
|
||||
if (graph[i].empty()) {
|
||||
ans.push_back(i + 1);
|
||||
euler.pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
used[graph[i].back().second] = true;
|
||||
euler.push(graph[i].back().first);
|
||||
graph[i].pop_back();
|
||||
}
|
||||
|
||||
if (ans.size() != m + 1) {
|
||||
cout << "IMPOSSIBLE\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
104
CSES Problem Set/Maximum Subarray Sum II.cpp
Normal file
104
CSES Problem Set/Maximum Subarray Sum II.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1644 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, a, b;
|
||||
cin >> n >> a >> b;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
vl pref(n + 1);
|
||||
rep(i, n) {
|
||||
pref[i + 1] = pref[i] + fds[i];
|
||||
}
|
||||
|
||||
ll ans = INT64_MIN >> 1;
|
||||
|
||||
int i = a;
|
||||
|
||||
multiset<ll> var;
|
||||
|
||||
while (i <= n) {
|
||||
if (i - b - 1 >= 0) {
|
||||
var.erase(var.find(pref[i - b - 1]));
|
||||
}
|
||||
var.insert(pref[i - a]);
|
||||
rmax(ans, pref[i] - *var.begin());
|
||||
i++;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
102
CSES Problem Set/Maximum Subarray Sum.cpp
Normal file
102
CSES Problem Set/Maximum Subarray Sum.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1643 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
ll now = 0;
|
||||
ll ans = 0;
|
||||
ll maximus = INT64_MIN;
|
||||
repv(i, fds) {
|
||||
now += i;
|
||||
rmax(now, 0LL);
|
||||
|
||||
rmax(ans, now);
|
||||
rmax(maximus, i);
|
||||
}
|
||||
|
||||
if (maximus < 0) {
|
||||
cout << maximus << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
150
CSES Problem Set/Meet in the Middle.cpp
Normal file
150
CSES Problem Set/Meet in the Middle.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1628/ */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
ll s;
|
||||
cin >> n >> s;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
int mid = n / 2;
|
||||
|
||||
vl count;
|
||||
|
||||
function<void(int, ll)> func1 = [&](int i, ll sum) {
|
||||
if (sum > s) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (i >= mid) {
|
||||
count.push_back(sum);
|
||||
return;
|
||||
}
|
||||
|
||||
func1(i + 1, sum);
|
||||
func1(i + 1, sum + fds[i]);
|
||||
};
|
||||
|
||||
ll ans = 0;
|
||||
vl sum;
|
||||
vl c;
|
||||
|
||||
function<void(int, ll)> func2 = [&](int i, ll ss) {
|
||||
if (ss > s) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (i >= n - mid) {
|
||||
// ans += count[s - sum];
|
||||
auto itr = lower_bound(all(sum), s - ss);
|
||||
if (itr == sum.end() || *itr != s - ss) {
|
||||
return;
|
||||
}
|
||||
ans += c[itr - sum.begin()];
|
||||
return;
|
||||
}
|
||||
|
||||
func2(i + 1, ss);
|
||||
func2(i + 1, ss + fds[i + mid]);
|
||||
};
|
||||
|
||||
func1(0, 0);
|
||||
|
||||
sortv(count);
|
||||
ll prev = count[0];
|
||||
int co = 1;
|
||||
|
||||
nrep(i, 1, count.size()) {
|
||||
if (prev == count[i]) {
|
||||
co++;
|
||||
continue;
|
||||
}
|
||||
|
||||
sum.push_back(prev);
|
||||
c.push_back(co);
|
||||
|
||||
prev = count[i];
|
||||
co = 1;
|
||||
}
|
||||
|
||||
sum.push_back(prev);
|
||||
c.push_back(co);
|
||||
|
||||
func2(0, 0);
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
131
CSES Problem Set/Message Route.cpp
Normal file
131
CSES Problem Set/Message Route.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1667 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
vi p(n, -1);
|
||||
|
||||
auto bfs = [&]() {
|
||||
p[0] = 0;
|
||||
queue<int> q;
|
||||
q.push(0);
|
||||
|
||||
while (!q.empty()) {
|
||||
int i = q.front();
|
||||
q.pop();
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (p[j] != -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
p[j] = i;
|
||||
q.push(j);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bfs();
|
||||
|
||||
if (p[n - 1] == -1) {
|
||||
cout << "IMPOSSIBLE\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
vi ans;
|
||||
ans.push_back(n);
|
||||
int i = p[n - 1];
|
||||
while (i != 0) {
|
||||
ans.push_back(i + 1);
|
||||
i = p[i];
|
||||
}
|
||||
|
||||
ans.push_back(1);
|
||||
reverse(all(ans));
|
||||
|
||||
cout << ans.size() << '\n' << ans;
|
||||
}
|
||||
134
CSES Problem Set/Mex Grid Construction.cpp
Normal file
134
CSES Problem Set/Mex Grid Construction.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3419 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vvi ans(n, vi(n));
|
||||
ans[0][0] = 0;
|
||||
|
||||
V<V<set<int>>> mexleft(n, V<set<int>>(n));
|
||||
V<V<set<int>>> mexup(n, V<set<int>>(n));
|
||||
mexleft[0][0].insert(0);
|
||||
mexup[0][0].insert(0);
|
||||
|
||||
auto find_mex = [](set<int> &a) {
|
||||
int now = 0;
|
||||
auto itr = a.begin();
|
||||
|
||||
while (itr != a.end() && now == *itr) {
|
||||
itr++;
|
||||
now++;
|
||||
}
|
||||
|
||||
return now;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, n) {
|
||||
if (i == 0 && j == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
swap(mexup[i][j], mexup[i - 1][j]);
|
||||
}
|
||||
|
||||
if (j > 0) {
|
||||
swap(mexleft[i][j], mexleft[i][j - 1]);
|
||||
}
|
||||
|
||||
set<int> tmp;
|
||||
repv(k, mexup[i][j]) {
|
||||
tmp.insert(k);
|
||||
}
|
||||
repv(k, mexleft[i][j]) {
|
||||
tmp.insert(k);
|
||||
}
|
||||
|
||||
int mex = find_mex(tmp);
|
||||
ans[i][j] = mex;
|
||||
mexup[i][j].insert(mex);
|
||||
mexleft[i][j].insert(mex);
|
||||
}
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, n) {
|
||||
cout << ans[i][j] << " \n"[j == n - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
125
CSES Problem Set/Minimal Grid Path.cpp
Normal file
125
CSES Problem Set/Minimal Grid Path.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3359 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<string> fds(n);
|
||||
cin >> fds;
|
||||
|
||||
string ans(2 * n - 1, 'Z' + 1);
|
||||
|
||||
queue<pair<int, int>> q;
|
||||
V<V<bool>> vis(n, V<bool>(n, false));
|
||||
vis[0][0] = true;
|
||||
q.emplace(0, 0);
|
||||
queue<pair<int, int>> tmp;
|
||||
|
||||
rep(_, n * 2 - 1) {
|
||||
while (!q.empty()) {
|
||||
auto [i, j] = q.front();
|
||||
q.pop();
|
||||
|
||||
int pos = i + j;
|
||||
|
||||
rmin(ans[pos], fds[i][j]);
|
||||
tmp.emplace(i, j);
|
||||
}
|
||||
|
||||
while (!tmp.empty()) {
|
||||
auto [i, j] = tmp.front();
|
||||
tmp.pop();
|
||||
|
||||
if (ans[i + j] < fds[i][j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int add[][2] = {{0, 1}, {1, 0}};
|
||||
|
||||
for (auto &k : add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id < n && jd < n && !vis[id][jd]) {
|
||||
vis[id][jd] = true;
|
||||
q.emplace(id, jd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
95
CSES Problem Set/Missing Coin Sum.cpp
Normal file
95
CSES Problem Set/Missing Coin Sum.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2183 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
sortv(fds);
|
||||
|
||||
if (fds[0] > 1) {
|
||||
cout << "1\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
ll sum = 1;
|
||||
|
||||
for (size_t i = 1; i < n && fds[i] <= sum + 1; i++) {
|
||||
sum += fds[i];
|
||||
}
|
||||
|
||||
cout << sum + 1 << '\n';
|
||||
}
|
||||
196
CSES Problem Set/Monsters.cpp
Normal file
196
CSES Problem Set/Monsters.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1194 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<string> fds(n);
|
||||
cin >> fds;
|
||||
|
||||
int si, sj;
|
||||
|
||||
int inf = INT32_MAX >> 1;
|
||||
|
||||
queue<pair<int, int>> mq;
|
||||
vvi mdis(n, vi(m, inf));
|
||||
|
||||
rep(i, n){
|
||||
rep(j, m) {
|
||||
if (fds[i][j] == 'A') {
|
||||
si = i;
|
||||
sj = j;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fds[i][j] == 'M') {
|
||||
mq.emplace(i, j);
|
||||
mdis[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto mbfs = [&](){
|
||||
while (!mq.empty()) {
|
||||
auto [i, j] = mq.front();
|
||||
mq.pop();
|
||||
|
||||
int add[][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
|
||||
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id >= 0 && id < n && jd >= 0 && jd < m && fds[id][jd] != '#' && mdis[id][jd] > mdis[i][j] + 1) {
|
||||
mdis[id][jd] = mdis[i][j] + 1;
|
||||
mq.emplace(id, jd);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mbfs();
|
||||
|
||||
vvi pdis(n, vi(m, inf));
|
||||
|
||||
auto bfs = [&](){
|
||||
pdis[si][sj] = 0;
|
||||
queue<pair<int, int>> q;
|
||||
q.emplace(si, sj);
|
||||
|
||||
while (!q.empty()) {
|
||||
auto [i, j] = q.front();
|
||||
q.pop();
|
||||
|
||||
|
||||
int add[][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
|
||||
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id >= 0 && id < n && jd >= 0 && jd < m && fds[id][jd] == '.' && pdis[id][jd] > pdis[i][j] + 1) {
|
||||
pdis[id][jd] = pdis[i][j] + 1;
|
||||
q.emplace(id, jd);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bfs();
|
||||
|
||||
string ans;
|
||||
|
||||
function<bool(int, int)> dfs = [&](int i, int j) {
|
||||
if (i < 0 || i >= n || j < 0 || j >= m) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pdis[i][j] >= mdis[i][j] || fds[i][j] == '#' || fds[i][j] == 'M') {
|
||||
return false;
|
||||
}
|
||||
|
||||
int add[][3] = {{0, 1, 'R'}, {0, -1, 'L'}, {-1, 0, 'U'}, {1, 0, 'D'}};
|
||||
|
||||
repv(k, add) {
|
||||
int id = i + k[0];
|
||||
int jd = j + k[1];
|
||||
|
||||
if (id >= 0 && id < n && jd >= 0 && jd < m && pdis[id][jd] <= pdis[i][j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ans.push_back(k[2]);
|
||||
|
||||
if (dfs(id, jd)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ans.pop_back();
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
if (!dfs(si, sj)) {
|
||||
cout << "NO\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
ans.pop_back();
|
||||
|
||||
cout << "YES\n" << ans.size() << '\n';
|
||||
cout << ans << '\n';
|
||||
}
|
||||
190
CSES Problem Set/Mountain Range.cpp
Normal file
190
CSES Problem Set/Mountain Range.cpp
Normal file
@@ -0,0 +1,190 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3314 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
while (__builtin_popcount(fds.size()) != 1) {
|
||||
fds.push_back(0);
|
||||
}
|
||||
|
||||
vi seg(fds.size() * 2, 1);
|
||||
|
||||
auto update = [&](int i, int v) {
|
||||
seg[i + fds.size()] = v;
|
||||
|
||||
for (int j = (i + fds.size()) / 2; j > 0; j /= 2) {
|
||||
seg[j] = max(seg[j * 2], seg[j * 2 + 1]);
|
||||
}
|
||||
};
|
||||
|
||||
function<int(int, int, int, int, int)> search = [&](int i, int l, int r, int tl, int tr) {
|
||||
if (l > tr || r < tl) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return seg[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) / 2;
|
||||
return max(search(i * 2, l, mid, tl, tr), search(i * 2 + 1, mid + 1, r, tl, tr));
|
||||
};
|
||||
|
||||
priority_queue<pair<ll, ll>, V<pair<ll, ll>>, greater<>> order;
|
||||
|
||||
vvl sparse(n, vl(20));
|
||||
rep(i, n) {
|
||||
sparse[i][0] = fds[i];
|
||||
}
|
||||
|
||||
nrep(j, 1, 20) {
|
||||
for (int i = 0; i + (1 << (j - 1)) < n; i++) {
|
||||
sparse[i][j] = max(sparse[i][j - 1], sparse[i + (1 << (j - 1))][j - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
auto sparsequery = [&](int l, int r) {
|
||||
if (l == r) {
|
||||
return sparse[l][0];
|
||||
}
|
||||
|
||||
int log = 31 - __builtin_clz(r - l + 1);
|
||||
return max(sparse[l][log], sparse[r - (1 << log) + 1][log]);
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
order.emplace(fds[i], i);
|
||||
}
|
||||
|
||||
while (!order.empty()) {
|
||||
int i = order.top().second;
|
||||
order.pop();
|
||||
int ans1 = 0;
|
||||
int ans2 = 0;
|
||||
|
||||
if (i > 0 && fds[i - 1] < fds[i]) {
|
||||
int ans = 0;
|
||||
int low = 0;
|
||||
int high = i - 1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low + high) / 2;
|
||||
|
||||
if (sparsequery(mid, i - 1) >= fds[i]) {
|
||||
ans = mid + 1;
|
||||
low = mid + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
high = mid - 1;
|
||||
}
|
||||
|
||||
if (ans != i) {
|
||||
ans1 = search(1, 0, fds.size() - 1, ans, i - 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (i < n - 1 && fds[i + 1] < fds[i]) {
|
||||
int ans = n - 1;
|
||||
int low = i + 1;
|
||||
int high = n - 1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low + high) / 2;
|
||||
|
||||
if (sparsequery(i + 1, mid) >= fds[i]) {
|
||||
ans = mid - 1;
|
||||
high = mid - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
low = mid + 1;
|
||||
}
|
||||
|
||||
if (ans != i) {
|
||||
ans2 = search(1, 0, fds.size() - 1, i + 1, ans);
|
||||
}
|
||||
}
|
||||
|
||||
update(i, max(ans1, ans2) + 1);
|
||||
}
|
||||
|
||||
cout << seg[1] << '\n';
|
||||
}
|
||||
106
CSES Problem Set/Movie Festival II.cpp
Normal file
106
CSES Problem Set/Movie Festival II.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1632 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
V<pair<ll, ll>> fds(n);
|
||||
|
||||
repv(i, fds) {
|
||||
cin >> i.second >> i.first;
|
||||
}
|
||||
|
||||
sortv(fds);
|
||||
|
||||
multiset<ll> ends;
|
||||
rep(i, k) {
|
||||
ends.insert(0);
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
repv(i, fds) {
|
||||
auto itr = ends.upper_bound(i.second);
|
||||
if (itr == ends.begin()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ans++;
|
||||
ends.erase(prev(itr));
|
||||
ends.insert(i.first);
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
101
CSES Problem Set/Movie Festival.cpp
Normal file
101
CSES Problem Set/Movie Festival.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1629 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<pair<ll, ll>> dura;
|
||||
|
||||
while (n--) {
|
||||
ll a, b;
|
||||
cin >> a >> b;
|
||||
dura.emplace_back(b, a);
|
||||
}
|
||||
|
||||
sortv(dura);
|
||||
|
||||
int ans = 1;
|
||||
int nowe = dura[0].first;
|
||||
|
||||
nrep(i, 1, dura.size()) {
|
||||
if (dura[i].second >= nowe) {
|
||||
ans++;
|
||||
nowe = dura[i].first;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
100
CSES Problem Set/Nearest Smaller Values.cpp
Normal file
100
CSES Problem Set/Nearest Smaller Values.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1645 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
stack<pair<int, int>> s;
|
||||
|
||||
rep(i, fds.size()) {
|
||||
while (!s.empty() && s.top().first >= fds[i]) {
|
||||
s.pop();
|
||||
}
|
||||
|
||||
if (s.empty()) {
|
||||
cout << "0 ";
|
||||
s.emplace(fds[i], i);
|
||||
continue;
|
||||
}
|
||||
|
||||
cout << s.top().second + 1 << ' ';
|
||||
s.emplace(fds[i], i);
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
112
CSES Problem Set/Nested Ranges Check.cpp
Normal file
112
CSES Problem Set/Nested Ranges Check.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2168 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <iterator>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
int prev = n;
|
||||
|
||||
V<pair<int, pair<ll, ll>>> ranges(n);
|
||||
rep(i, n) {
|
||||
cin >> ranges[i].second.first >> ranges[i].second.second;
|
||||
ranges[i].first = i;
|
||||
}
|
||||
|
||||
sort(all(ranges), [&](pair<int, pair<ll, ll>> &a, pair<int, pair<ll, ll>> &b){
|
||||
if (a.second.first == b.second.first) {
|
||||
return a.second.second > b.second.second;
|
||||
}
|
||||
return a.second.first < b.second.first;
|
||||
});
|
||||
|
||||
V<bool> ans1(prev);
|
||||
V<bool> ans2(prev);
|
||||
|
||||
ll minimus = INT64_MAX;
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
ans1[ranges[i].first] = ranges[i].second.second >= minimus;
|
||||
rmin(minimus, ranges[i].second.second);
|
||||
}
|
||||
|
||||
ll maximus = 0;
|
||||
rep(i, n) {
|
||||
ans2[ranges[i].first] = ranges[i].second.second <= maximus;
|
||||
rmax(maximus, ranges[i].second.second);
|
||||
}
|
||||
|
||||
cout << ans1 << ans2;
|
||||
}
|
||||
141
CSES Problem Set/Nested Ranges Count.cpp
Normal file
141
CSES Problem Set/Nested Ranges Count.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2169 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<pair<int, pair<ll, ll>>> ranges(n);
|
||||
|
||||
map<ll, int> var;
|
||||
rep(i, n) {
|
||||
cin >> ranges[i].second.first >> ranges[i].second.second;
|
||||
ranges[i].first = i;
|
||||
var[ranges[i].second.first] = 0;
|
||||
var[ranges[i].second.second] = 0;
|
||||
}
|
||||
|
||||
int v = 0;
|
||||
repv(i, var) {
|
||||
i.second = v;
|
||||
v++;
|
||||
}
|
||||
|
||||
repv(i, ranges) {
|
||||
i.second.first = var[i.second.first];
|
||||
i.second.second = var[i.second.second];
|
||||
}
|
||||
|
||||
sort(all(ranges), [&](pair<int, pair<ll, ll>> &a, pair<int, pair<ll, ll>> &b){
|
||||
if (a.second.first == b.second.first) {
|
||||
return a.second.second > b.second.second;
|
||||
}
|
||||
return a.second.first < b.second.first;
|
||||
});
|
||||
|
||||
vi bit(var.size());
|
||||
|
||||
auto add = [&](int x){
|
||||
for (; x < var.size(); x |= (x + 1)) {
|
||||
bit[x]++;
|
||||
}
|
||||
};
|
||||
|
||||
auto get = [&](int x){
|
||||
int ans = 0;
|
||||
for (; x >= 0; x = (x & (x + 1)) - 1) {
|
||||
ans += bit[x];
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
vi ans1(n);
|
||||
vi ans2(n);
|
||||
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
ans1[ranges[i].first] = get(ranges[i].second.second);
|
||||
add(ranges[i].second.second);
|
||||
}
|
||||
|
||||
fill(all(bit), 0);
|
||||
|
||||
rep(i, n) {
|
||||
ans2[ranges[i].first] = get(var.size() - 1) - get(ranges[i].second.second - 1);
|
||||
add(ranges[i].second.second);
|
||||
}
|
||||
|
||||
cout << ans1 << ans2;
|
||||
}
|
||||
199
CSES Problem Set/New Roads Queries.cpp
Normal file
199
CSES Problem Set/New Roads Queries.cpp
Normal file
@@ -0,0 +1,199 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2101/ */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m, q;
|
||||
cin >> n >> m >> q;
|
||||
|
||||
V<tuple<int, int, int>> edges;
|
||||
|
||||
rep(i, m) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
edges.emplace_back(i, a, b);
|
||||
}
|
||||
|
||||
vi dsu(n);
|
||||
rep(i, n) {
|
||||
dsu[i] = i;
|
||||
}
|
||||
|
||||
function<int(int)> find_p = [&](int i){
|
||||
if (dsu[i] == i) {
|
||||
return i;
|
||||
}
|
||||
return dsu[i] = find_p(dsu[i]);
|
||||
};
|
||||
|
||||
auto make_pair = [&](int a, int b) {
|
||||
a = find_p(a);
|
||||
b = find_p(b);
|
||||
|
||||
if (a == b) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dsu[b] = a;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
V<V<pair<int, int>>> graph(n);
|
||||
|
||||
for (auto [i, a, b] : edges) {
|
||||
if (make_pair(a, b)) {
|
||||
graph[a].emplace_back(b, i + 1);
|
||||
graph[b].emplace_back(a, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
vvi par(n, vi(20, 0));
|
||||
vvi maximus(n, vi(20, 0));
|
||||
vi depth(n, 0);
|
||||
|
||||
function<void(int, int, int)> dfs = [&](int i, int p, int big){
|
||||
par[i][0] = p;
|
||||
maximus[i][0] = big;
|
||||
depth[i] = depth[p] + 1;
|
||||
|
||||
nrep(j, 1, 20) {
|
||||
par[i][j] = par[par[i][j - 1]][j - 1];
|
||||
maximus[i][j] = max(maximus[i][j - 1], maximus[par[i][j - 1]][j - 1]);
|
||||
}
|
||||
|
||||
for (auto [u, v] : graph[i]) {
|
||||
if (u == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(u, i, v);
|
||||
}
|
||||
};
|
||||
|
||||
auto lca = [&](int a, int b){
|
||||
if (depth[a] > depth[b]) {
|
||||
swap(a, b);
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
|
||||
int diff = depth[b] - depth[a];
|
||||
|
||||
for (int i = 19; i >= 0; i--) {
|
||||
if (diff & (1 << i)) {
|
||||
rmax(ans, maximus[b][i]);
|
||||
b = par[b][i];
|
||||
}
|
||||
}
|
||||
|
||||
if (a == b) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
for (int i = 19; i >= 0; i--) {
|
||||
if (par[a][i] != par[b][i]) {
|
||||
rmax(ans, maximus[a][i]);
|
||||
rmax(ans, maximus[b][i]);
|
||||
a = par[a][i];
|
||||
b = par[b][i];
|
||||
}
|
||||
}
|
||||
|
||||
return max({ans, maximus[a][0], maximus[b][0]});
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (dsu[i] == i) {
|
||||
fill(all(par[i]), i);
|
||||
dfs(i, i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
if (find_p(a) != find_p(b)) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
cout << lca(a, b) << '\n';
|
||||
}
|
||||
}
|
||||
133
CSES Problem Set/Next Prime.cpp
Normal file
133
CSES Problem Set/Next Prime.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3396 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n;
|
||||
cin >> n;
|
||||
|
||||
n++;
|
||||
if (n == 2) {
|
||||
cout << "2\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (~n & 1) {
|
||||
n++;
|
||||
}
|
||||
|
||||
auto isprime = [&]() {
|
||||
for (ll i = 2; i * i <= n; i++) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
while (!isprime()) {
|
||||
n += 2;
|
||||
}
|
||||
|
||||
cout << n << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
111
CSES Problem Set/Number Spiral.cpp
Normal file
111
CSES Problem Set/Number Spiral.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1071 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
ll x, y;
|
||||
cin >> x >> y;
|
||||
|
||||
if (x > y) {
|
||||
ll sz = (x - 1) * (x - 1);
|
||||
|
||||
ll pos1, pos2;
|
||||
if (x & 1) {
|
||||
pos1 = y;
|
||||
pos2 = x;
|
||||
} else {
|
||||
pos1 = x;
|
||||
pos2 = y;
|
||||
}
|
||||
|
||||
cout << pos1 + x - pos2 + sz << '\n';
|
||||
} else {
|
||||
ll sz = (y - 1) * (y - 1);
|
||||
|
||||
ll pos1, pos2;
|
||||
if (y & 1) {
|
||||
pos1 = y;
|
||||
pos2 = x;
|
||||
} else {
|
||||
pos1 = x;
|
||||
pos2 = y;
|
||||
}
|
||||
|
||||
cout << pos1 + y - pos2 + sz << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
121
CSES Problem Set/Palindrome Reorder.cpp
Normal file
121
CSES Problem Set/Palindrome Reorder.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1755 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
vi count(26);
|
||||
|
||||
repv(i, a) {
|
||||
count[i - 'A']++;
|
||||
}
|
||||
|
||||
int odds = 0;
|
||||
int odd = -1;
|
||||
rep(i, 26) {
|
||||
if (count[i] & 1) {
|
||||
odds++;
|
||||
odd = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (odds > 1) {
|
||||
cout << "NO SOLUTION\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (odd != -1) {
|
||||
count[odd]--;
|
||||
}
|
||||
|
||||
rep(i, 26) {
|
||||
rep(j, count[i] / 2) {
|
||||
cout << (char)(i + 'A');
|
||||
}
|
||||
}
|
||||
|
||||
if (odd != -1) {
|
||||
cout << (char)(odd + 'A');
|
||||
}
|
||||
|
||||
for (int i = 25; i >= 0; i--) {
|
||||
nrep(j, count[i] / 2, count[i]) {
|
||||
cout << (char)(i + 'A');
|
||||
}
|
||||
}
|
||||
|
||||
cout << '\n';
|
||||
}
|
||||
166
CSES Problem Set/Pizzeria Queries.cpp
Normal file
166
CSES Problem Set/Pizzeria Queries.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2206 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
ll oo = INT64_MAX >> 1;
|
||||
|
||||
while (__builtin_popcount(n) != 1) {
|
||||
n++;
|
||||
fds.push_back(oo);
|
||||
}
|
||||
|
||||
vl segpref(n * 2);
|
||||
vl segsuf(n * 2);
|
||||
|
||||
rep(i, n) {
|
||||
segpref[i + n] = fds[i] + i;
|
||||
segsuf[i + n] = fds[i] + (n - i - 1);
|
||||
}
|
||||
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
segpref[i] = min(segpref[i * 2], segpref[i * 2 + 1]);
|
||||
segsuf[i] = min(segsuf[i * 2], segsuf[i * 2 + 1]);
|
||||
}
|
||||
|
||||
auto update = [&](int i, ll v) {
|
||||
i += n;
|
||||
segpref[i] = v + (i - n);
|
||||
segsuf[i] = v + (2 * n - i - 1);
|
||||
|
||||
for (i >>= 1; i > 0; i >>= 1) {
|
||||
segpref[i] = min(segpref[i * 2], segpref[i * 2 + 1]);
|
||||
segsuf[i] = min(segsuf[i * 2], segsuf[i * 2 + 1]);
|
||||
}
|
||||
};
|
||||
|
||||
function<ll(int, int, int, int, int)> querypref = [&](int i, int l, int r, int tl, int tr) {
|
||||
if (l > tr || r < tl) {
|
||||
return oo;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return segpref[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
return min(querypref(i * 2, l, mid, tl, tr), querypref(i * 2 + 1, mid + 1, r, tl, tr));
|
||||
};
|
||||
|
||||
function<ll(int, int, int, int, int)> querysuf = [&](int i, int l, int r, int tl, int tr) {
|
||||
if (l > tr || r < tl) {
|
||||
return oo;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return segsuf[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
return min(querysuf(i * 2, l, mid, tl, tr), querysuf(i * 2 + 1, mid + 1, r, tl, tr));
|
||||
};
|
||||
|
||||
while (q--) {
|
||||
int op;
|
||||
cin >> op;
|
||||
|
||||
if (op == 1) {
|
||||
int k, x;
|
||||
cin >> k >> x;
|
||||
|
||||
update(k - 1, x);
|
||||
continue;
|
||||
}
|
||||
|
||||
int k;
|
||||
cin >> k;
|
||||
k--;
|
||||
|
||||
cout << min({querypref(1, 0, n - 1, k, n - 1) - k, querysuf(1, 0, n - 1, 0, k) - (n - k - 1)}) << '\n';
|
||||
}
|
||||
}
|
||||
166
CSES Problem Set/Planets Cycles.cpp
Normal file
166
CSES Problem Set/Planets Cycles.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1751 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vi next(n);
|
||||
repv(i, next) {
|
||||
cin >> i;
|
||||
i--;
|
||||
}
|
||||
|
||||
V<bool> vis(n);
|
||||
V<bool> frame(n);
|
||||
V<bool> cycle(n);
|
||||
vi cyclesize(n);
|
||||
|
||||
auto getcycle = [&](int i) {
|
||||
set<int> rem;
|
||||
frame[i] = true;
|
||||
vis[i] = true;
|
||||
rem.insert(i);
|
||||
int j = next[i];
|
||||
|
||||
while (!vis[j] && !frame[j]) {
|
||||
vis[j] = true;
|
||||
frame[j] = true;
|
||||
rem.insert(j);
|
||||
j = next[j];
|
||||
}
|
||||
|
||||
if (!frame[j]) {
|
||||
repv(i, rem) {
|
||||
frame[i] = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int now = 1;
|
||||
i = j;
|
||||
j = next[j];
|
||||
cycle[i] = true;
|
||||
|
||||
while (j != i) {
|
||||
cycle[j] = true;
|
||||
now++;
|
||||
j = next[j];
|
||||
}
|
||||
|
||||
cyclesize[i] = now;
|
||||
j = next[j];
|
||||
while (j != i) {
|
||||
cyclesize[j] = now;
|
||||
j = next[j];
|
||||
}
|
||||
|
||||
repv(i, rem) {
|
||||
frame[i] = false;
|
||||
}
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
getcycle(i);
|
||||
}
|
||||
|
||||
vi memo(n, -1);
|
||||
rep(i, n) {
|
||||
if (cycle[i]) {
|
||||
memo[i] = cyclesize[i];
|
||||
}
|
||||
}
|
||||
|
||||
function<int(int)> dfs = [&](int i) {
|
||||
int &ans = memo[i];
|
||||
if (ans != -1) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
ans = dfs(next[i]) + 1;
|
||||
return ans;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
cout << dfs(i) << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
116
CSES Problem Set/Planets Queries I.cpp
Normal file
116
CSES Problem Set/Planets Queries I.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1750 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vvi parent(30, vi(n));
|
||||
|
||||
rep(i, n) {
|
||||
int a;
|
||||
cin >> a;
|
||||
|
||||
parent[0][i] = a - 1;
|
||||
}
|
||||
|
||||
nrep(i, 1, 30) {
|
||||
rep(j, n) {
|
||||
parent[i][j] = parent[i - 1][parent[i - 1][j]];
|
||||
}
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int i;
|
||||
ll k;
|
||||
cin >> i >> k;
|
||||
|
||||
i--;
|
||||
|
||||
rep(j, 30) {
|
||||
if ((k >> j) & 1) {
|
||||
i = parent[j][i];
|
||||
}
|
||||
}
|
||||
|
||||
cout << i + 1 << '\n';
|
||||
}
|
||||
}
|
||||
240
CSES Problem Set/Planets Queries II.cpp
Normal file
240
CSES Problem Set/Planets Queries II.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1160 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vi parent(n);
|
||||
rep(i, n) {
|
||||
int a;
|
||||
cin >> a;
|
||||
|
||||
parent[i] = a - 1;
|
||||
}
|
||||
|
||||
V<bool> loop(n);
|
||||
vi group(n);
|
||||
vi cyclepos(n);
|
||||
|
||||
V<bool> vis(n);
|
||||
V<bool> frame(n);
|
||||
vi groupsize;
|
||||
vi cyclechoice(n);
|
||||
int g = 0;
|
||||
|
||||
auto func = [&](int i) {
|
||||
set<int> used;
|
||||
while (!vis[i] && !frame[i]) {
|
||||
vis[i] = true;
|
||||
frame[i] = true;
|
||||
used.insert(i);
|
||||
i = parent[i];
|
||||
}
|
||||
|
||||
if (frame[i]) {
|
||||
loop[i] = true;
|
||||
group[i] = g;
|
||||
cyclechoice[i] = i;
|
||||
|
||||
int now = 1;
|
||||
|
||||
int j = parent[i];
|
||||
|
||||
while (j != i) {
|
||||
cyclepos[j] = now;
|
||||
now++;
|
||||
loop[j] = true;
|
||||
group[j] = g;
|
||||
cyclechoice[j] = j;
|
||||
j = parent[j];
|
||||
}
|
||||
|
||||
g++;
|
||||
groupsize.push_back(now);
|
||||
}
|
||||
|
||||
for (auto j : used) {
|
||||
frame[j] = false;
|
||||
}
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
func(i);
|
||||
}
|
||||
|
||||
fill(all(vis), false);
|
||||
vi pos(n);
|
||||
|
||||
function<void(int)> dfs = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
if (!vis[parent[i]] && !loop[parent[i]]) {
|
||||
dfs(parent[i]);
|
||||
}
|
||||
|
||||
pos[i] = pos[parent[i]] + 1;
|
||||
cyclechoice[i] = cyclechoice[parent[i]];
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (!loop[i] && !vis[i]) {
|
||||
dfs(i);
|
||||
}
|
||||
}
|
||||
|
||||
vvi lift(17, vi(n));
|
||||
|
||||
vi valids;
|
||||
rep(i, n) {
|
||||
if (!loop[i]) {
|
||||
valids.push_back(i);
|
||||
lift[0][i] = parent[i];
|
||||
}
|
||||
}
|
||||
|
||||
nrep(i, 1, 17) {
|
||||
repv(j, valids) {
|
||||
lift[i][j] = lift[i - 1][lift[i - 1][j]];
|
||||
}
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
if (loop[a] && loop[b]) {
|
||||
if (group[a] != group[b]) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
int g = group[a];
|
||||
|
||||
cout << ((cyclepos[b] - cyclepos[a]) % groupsize[g] + groupsize[g]) % groupsize[g] << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
if (loop[a] && !loop[b]) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!loop[a] && loop[b]) {
|
||||
int choice = cyclechoice[a];
|
||||
|
||||
if (group[choice] != group[b]) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
int g = group[choice];
|
||||
|
||||
cout << pos[a] + ((cyclepos[b] - cyclepos[choice]) % groupsize[g] + groupsize[g]) % groupsize[g] << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
int diff = pos[a] - pos[b];
|
||||
|
||||
if (diff < 0) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
rep(i, 17) {
|
||||
if ((diff >> i) & 1) {
|
||||
a = lift[i][a];
|
||||
}
|
||||
}
|
||||
|
||||
if (a != b) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
cout << diff << '\n';
|
||||
}
|
||||
}
|
||||
154
CSES Problem Set/Planets and Kingdoms.cpp
Normal file
154
CSES Problem Set/Planets and Kingdoms.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1683 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vvi graph(n);
|
||||
vvi inv(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
inv[b].push_back(a);
|
||||
}
|
||||
|
||||
V<bool> vis(n);
|
||||
stack<int> s;
|
||||
|
||||
function<void(int)> postorder = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
postorder(j);
|
||||
}
|
||||
|
||||
s.push(i);
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
postorder(i);
|
||||
}
|
||||
|
||||
vi group(n);
|
||||
int g = 1;
|
||||
fill(all(vis), false);
|
||||
|
||||
function<void(int)> dfs = [&](int i) {
|
||||
vis[i] = true;
|
||||
group[i] = g;
|
||||
|
||||
for (auto j : inv[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(j);
|
||||
}
|
||||
};
|
||||
|
||||
while (!s.empty()) {
|
||||
int now = s.top();
|
||||
s.pop();
|
||||
|
||||
if (vis[now]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(now);
|
||||
g++;
|
||||
}
|
||||
|
||||
cout << g - 1 << '\n';
|
||||
cout << group;
|
||||
}
|
||||
101
CSES Problem Set/Playlist.cpp
Normal file
101
CSES Problem Set/Playlist.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1141 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
map<ll, int> appear;
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
int ans = 0;
|
||||
int now = 0;
|
||||
int time = 0;
|
||||
rep(i, n) {
|
||||
ll nm;
|
||||
cin >> nm;
|
||||
|
||||
now++;
|
||||
|
||||
if (appear.count(nm) && appear[nm] >= time) {
|
||||
now -= appear[nm] - time + 1;
|
||||
time = appear[nm] + 1;
|
||||
}
|
||||
|
||||
rmax(ans, now);
|
||||
appear[nm] = i;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
126
CSES Problem Set/Point Location Test.cpp
Normal file
126
CSES Problem Set/Point Location Test.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2189 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll x1, y1, x2, y2, x3, y3;
|
||||
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
|
||||
|
||||
x3 -= x2;
|
||||
y3 -= y2;
|
||||
x2 -= x1;
|
||||
y2 -= y1;
|
||||
|
||||
ll ar = x2 * y3 - y2 * x3;
|
||||
|
||||
if (ar > 0) {
|
||||
cout << "LEFT\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (ar < 0) {
|
||||
cout << "RIGHT\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "TOUCH\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
174
CSES Problem Set/Police Chase.cpp
Normal file
174
CSES Problem Set/Police Chase.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1695 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int INF = INT32_MAX >> 1;
|
||||
|
||||
struct dinitz {
|
||||
const bool scaling = false; // com scaling -> O(nm log(MAXCAP)),
|
||||
int lim; // com constante alta
|
||||
struct edge {
|
||||
int to, cap, rev, flow;
|
||||
bool res;
|
||||
edge(int to_, int cap_, int rev_, bool res_)
|
||||
: to(to_), cap(cap_), rev(rev_), flow(0), res(res_) {}
|
||||
};
|
||||
|
||||
vector<vector<edge>> g;
|
||||
vector<int> lev, beg;
|
||||
ll F;
|
||||
dinitz(int n) : g(n), F(0) {}
|
||||
|
||||
void add(int a, int b, int c) {
|
||||
g[a].emplace_back(b, c, g[b].size(), false);
|
||||
g[b].emplace_back(a, 0, g[a].size()-1, true);
|
||||
}
|
||||
bool bfs(int s, int t) {
|
||||
lev = vector<int>(g.size(), -1); lev[s] = 0;
|
||||
beg = vector<int>(g.size(), 0);
|
||||
queue<int> q; q.push(s);
|
||||
while (q.size()) {
|
||||
int u = q.front(); q.pop();
|
||||
for (auto& i : g[u]) {
|
||||
if (lev[i.to] != -1 or (i.flow == i.cap)) continue;
|
||||
if (scaling and i.cap - i.flow < lim) continue;
|
||||
lev[i.to] = lev[u] + 1;
|
||||
q.push(i.to);
|
||||
}
|
||||
}
|
||||
return lev[t] != -1;
|
||||
}
|
||||
int dfs(int v, int s, int f = INF) {
|
||||
if (!f or v == s) return f;
|
||||
for (int& i = beg[v]; i < g[v].size(); i++) {
|
||||
auto& e = g[v][i];
|
||||
if (lev[e.to] != lev[v] + 1) continue;
|
||||
int foi = dfs(e.to, s, min(f, e.cap - e.flow));
|
||||
if (!foi) continue;
|
||||
e.flow += foi, g[e.to][e.rev].flow -= foi;
|
||||
return foi;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
ll max_flow(int s, int t) {
|
||||
for (lim = scaling ? (1<<30) : 1; lim; lim /= 2)
|
||||
while (bfs(s, t)) while (int ff = dfs(s, t)) F += ff;
|
||||
return F;
|
||||
}
|
||||
};
|
||||
|
||||
// Recupera as arestas do corte s-t
|
||||
vector<pair<int, int>> get_cut(dinitz& g, int s, int t) {
|
||||
g.max_flow(s, t);
|
||||
vector<pair<int, int>> cut;
|
||||
vector<int> vis(g.g.size(), 0), st = {s};
|
||||
vis[s] = 1;
|
||||
while (st.size()) {
|
||||
int u = st.back(); st.pop_back();
|
||||
for (auto e : g.g[u]) if (!vis[e.to] and e.flow < e.cap)
|
||||
vis[e.to] = 1, st.push_back(e.to);
|
||||
}
|
||||
for (int i = 0; i < g.g.size(); i++) for (auto e : g.g[i])
|
||||
if (vis[i] and !vis[e.to] and !e.res) cut.emplace_back(i, e.to);
|
||||
return cut;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
dinitz din(n);
|
||||
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
din.add(a, b, 1);
|
||||
din.add(b, a, 1);
|
||||
}
|
||||
|
||||
V<pair<int, int>> ans = get_cut(din, 0, n - 1);
|
||||
|
||||
cout << ans.size() << '\n';
|
||||
for (auto [a, b] : ans) {
|
||||
cout << a + 1 << ' ' << b + 1 << '\n';
|
||||
}
|
||||
}
|
||||
122
CSES Problem Set/Polygon Area.cpp
Normal file
122
CSES Problem Set/Polygon Area.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2191 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<pair<ll, ll>> a(n);
|
||||
repv(i, a) {
|
||||
cin >> i.first >> i.second;
|
||||
}
|
||||
|
||||
ll ar = 0;
|
||||
rep(i, n) {
|
||||
auto [x1, y1] = a[i];
|
||||
auto [x2, y2] = a[(i + 1) % n];
|
||||
|
||||
ar += x1 * y2 - y1 * x2;
|
||||
}
|
||||
|
||||
cout << abs(ar) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
181
CSES Problem Set/Polynomial Queries.cpp
Normal file
181
CSES Problem Set/Polynomial Queries.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1736 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
while (__builtin_popcount(n) != 1) {
|
||||
n++;
|
||||
fds.push_back(0);
|
||||
}
|
||||
|
||||
vl seg(n * 2);
|
||||
vl lazy(n * 2);
|
||||
vl c(n * 2);
|
||||
V<bool> upd(n * 2);
|
||||
|
||||
nrep(i, n, n * 2) {
|
||||
seg[i] = fds[i - n];
|
||||
}
|
||||
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
seg[i] = seg[i * 2] + seg[i * 2 + 1];
|
||||
}
|
||||
|
||||
auto propagate = [&](int i, int l, int r) {
|
||||
if (!upd[i]) {
|
||||
return;
|
||||
}
|
||||
|
||||
upd[i] = false;
|
||||
|
||||
ll size = (r - l + 1);
|
||||
|
||||
seg[i] += (size * (r + l) / 2) * c[i] + c[i] * size - lazy[i] * size;
|
||||
|
||||
if (i < n) {
|
||||
lazy[i * 2] += lazy[i];
|
||||
lazy[i * 2 + 1] += lazy[i];
|
||||
c[i * 2] += c[i];
|
||||
c[i * 2 + 1] += c[i];
|
||||
upd[i * 2] = true;
|
||||
upd[i * 2 + 1] = true;
|
||||
}
|
||||
|
||||
c[i] = 0;
|
||||
lazy[i] = 0;
|
||||
};
|
||||
|
||||
function<void(int, int, int, int, int, ll)> update = [&](int i, int l, int r, int tl, int tr, ll v) {
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l > tr || r < tl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
lazy[i] += v;
|
||||
upd[i] = true;
|
||||
c[i]++;
|
||||
propagate(i, l, r);
|
||||
return;
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
update(i * 2, l, mid, tl, tr, v);
|
||||
update(i * 2 + 1, mid + 1, r, tl, tr, v);
|
||||
|
||||
seg[i] = seg[i * 2] + seg[i * 2 + 1];
|
||||
};
|
||||
|
||||
function<ll(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) {
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l > tr || r < tl) {
|
||||
return 0LL;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return seg[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr);
|
||||
};
|
||||
|
||||
while (q--) {
|
||||
int op, l, r;
|
||||
cin >> op >> l >> r;
|
||||
|
||||
if (op == 1) {
|
||||
update(1, 1, n, l, r, l);
|
||||
continue;
|
||||
}
|
||||
|
||||
cout << query(1, 1, n, l, r) << '\n';
|
||||
}
|
||||
}
|
||||
211
CSES Problem Set/Prefix Sum Queries.cpp
Normal file
211
CSES Problem Set/Prefix Sum Queries.cpp
Normal file
@@ -0,0 +1,211 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2166 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vl fds(n);
|
||||
|
||||
cin >> fds;
|
||||
|
||||
ll oo = INT64_MAX >> 1;
|
||||
|
||||
while (__builtin_popcount(n) != 1) {
|
||||
n++;
|
||||
fds.push_back(0);
|
||||
}
|
||||
|
||||
vl seg(n * 2);
|
||||
vl pref(n * 2);
|
||||
vl lazy(n * 2);
|
||||
|
||||
auto propagate = [&](int i, int l, int r) {
|
||||
if (lazy[i] == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (i >= n) {
|
||||
seg[i] += lazy[i];
|
||||
lazy[i] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
seg[i] += lazy[i];
|
||||
lazy[i * 2] += lazy[i];
|
||||
lazy[i * 2 + 1] += lazy[i];
|
||||
lazy[i] = 0;
|
||||
};
|
||||
|
||||
function<void(int, int, int, int, int, ll)> update = [&](int i, int l, int r, int tl, int tr, ll v) {
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l > tr || r < tl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
lazy[i] += v;
|
||||
propagate(i, l, r);
|
||||
return;
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
update(i * 2, l, mid, tl, tr, v);
|
||||
update(i * 2 + 1, mid + 1, r, tl, tr, v);
|
||||
|
||||
seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
|
||||
};
|
||||
|
||||
function<void(int, ll)> updatepref = [&](int i, ll v) {
|
||||
i += n;
|
||||
pref[i] = v;
|
||||
|
||||
for (i >>= 1; i > 0; i >>= 1) {
|
||||
pref[i] = pref[i * 2] + pref[i * 2 + 1];
|
||||
}
|
||||
};
|
||||
|
||||
function<ll(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) {
|
||||
if (l > tr || r < tl) {
|
||||
return -oo;
|
||||
}
|
||||
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return seg[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
return max(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr));
|
||||
};
|
||||
|
||||
function<ll(int, int, int, int, int)> prefquery = [&](int i, int l, int r, int tl, int tr) {
|
||||
if (l > tr || r < tl) {
|
||||
return 0LL;
|
||||
}
|
||||
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return pref[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
return prefquery(i * 2, l, mid, tl, tr) + prefquery(i * 2 + 1, mid + 1, r, tl, tr);
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
update(1, 0, n - 1, i, n - 1, fds[i]);
|
||||
pref[i + n] = fds[i];
|
||||
}
|
||||
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
pref[i] = pref[i * 2] + pref[i * 2 + 1];
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int op;
|
||||
cin >> op;
|
||||
|
||||
if (op == 1) {
|
||||
int k, u;
|
||||
cin >> k >> u;
|
||||
k--;
|
||||
|
||||
update(1, 0, n - 1, k, n - 1, u);
|
||||
update(1, 0, n - 1, k, n - 1, -fds[k]);
|
||||
updatepref(k, u);
|
||||
fds[k] = u;
|
||||
continue;
|
||||
}
|
||||
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
cout << max(query(1, 0, n - 1, a, b) - prefquery(1, 0, n - 1, 0, a - 1), 0LL) << '\n';
|
||||
}
|
||||
}
|
||||
132
CSES Problem Set/Prime Multiples.cpp
Normal file
132
CSES Problem Set/Prime Multiples.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/2185 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n;
|
||||
int k;
|
||||
cin >> n >> k;
|
||||
|
||||
vl a(k);
|
||||
cin >> a;
|
||||
|
||||
__int128 ans = 0;
|
||||
nrep(i, 1, 1 << k) {
|
||||
long double cur = n;
|
||||
ll cur2 = 1;
|
||||
rep(j, k) {
|
||||
if ((i >> j) & 1) {
|
||||
cur /= a[j];
|
||||
cur2 *= a[j];
|
||||
}
|
||||
}
|
||||
|
||||
if (~__builtin_popcount(i) & 1) {
|
||||
cur = -cur;
|
||||
cur2 = -cur2;
|
||||
}
|
||||
|
||||
ans += (ll)cur;
|
||||
}
|
||||
|
||||
cout << (ll)ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1140 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
@@ -69,52 +67,6 @@ auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
return is;
|
||||
}
|
||||
|
||||
class segtree {
|
||||
vl seg;
|
||||
|
||||
ll _query(int i, int l, int r, int ql, int qr) {
|
||||
if (l > qr || r < ql) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (l >= ql && r <= qr) {
|
||||
return seg[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) / 2;
|
||||
return max(_query(i * 2, l, mid, ql, qr), _query(i * 2 + 1, mid + 1, r, ql, qr));
|
||||
}
|
||||
|
||||
public:
|
||||
segtree() = delete;
|
||||
segtree(vl &items): seg(items.size() * 2) {
|
||||
size_t n = items.size();
|
||||
for (size_t i = n; i < n * 2; i++) {
|
||||
seg[i] = items[i - n];
|
||||
}
|
||||
|
||||
for (size_t i = n - 1; i > 0; i--) {
|
||||
seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
void update(ll v, int i) {
|
||||
i += seg.size() / 2;
|
||||
if (seg[i] >= v) {
|
||||
return;
|
||||
}
|
||||
seg[i] = v;
|
||||
i /= 2;
|
||||
for (; i > 0; i /= 2) {
|
||||
seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
ll query(int i) {
|
||||
return _query(1, 0, seg.size() / 2 - 1, 0, i);
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
@@ -122,39 +74,33 @@ int main()
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
V<tuple<ll, ll, ll>> fds(n);
|
||||
set<ll> lol;
|
||||
for (auto &[i, e, v] : fds) {
|
||||
cin >> i >> e >> v;
|
||||
lol.insert(e);
|
||||
V<tuple<ll, ll, ll>> q(n);
|
||||
|
||||
map<ll, int> var;
|
||||
for (auto &[x, y, z] : q) {
|
||||
cin >> x >> y >> z;
|
||||
var[x] = 0;
|
||||
var[y] = 0;
|
||||
}
|
||||
|
||||
pair<vl, vl> ends(vl(lol.size(), 0), vl(lol.size()));
|
||||
auto itr = lol.begin();
|
||||
for (size_t i = 0; itr != lol.end(); i++, itr++) {
|
||||
ends.second[i] = *itr;
|
||||
int v = 0;
|
||||
for (auto &i : var) {
|
||||
i.second = v;
|
||||
v++;
|
||||
}
|
||||
|
||||
sort(fds.begin(), fds.end());
|
||||
|
||||
ll neutral = 1e9 + 1;
|
||||
while (__builtin_popcount(ends.second.size()) != 1) {
|
||||
ends.first.push_back(-1);
|
||||
ends.second.push_back(neutral++);
|
||||
V<V<pair<int, ll>>> proj(var.size());
|
||||
for (auto [s, e, v] : q) {
|
||||
proj[var[e]].emplace_back(var[s], v);
|
||||
}
|
||||
|
||||
segtree seg(ends.first);
|
||||
|
||||
for (auto [i, f, v] : fds) {
|
||||
auto itr = lower_bound(ends.second.begin(), ends.second.end(), i);
|
||||
if (itr == ends.second.begin()) {
|
||||
seg.update(v, lower_bound(ends.second.begin(), ends.second.end(), f) - ends.second.begin());
|
||||
continue;
|
||||
vl dp(var.size() + 1);
|
||||
nrep(i, 1, dp.size()) {
|
||||
dp[i] += dp[i - 1];
|
||||
for (auto [s, v] : proj[i - 1]) {
|
||||
rmax(dp[i], dp[s] + v);
|
||||
}
|
||||
|
||||
ll up = seg.query(itr - ends.second.begin() - 1) + v;
|
||||
seg.update(up, lower_bound(ends.second.begin(), ends.second.end(), f) - ends.second.begin());
|
||||
}
|
||||
|
||||
cout << seg.query(ends.first.size() - 1) << '\n';
|
||||
cout << dp.back() << '\n';
|
||||
}
|
||||
|
||||
116
CSES Problem Set/Raab Game I.cpp
Normal file
116
CSES Problem Set/Raab Game I.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3399 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n, a, b;
|
||||
cin >> n >> a >> b;
|
||||
|
||||
vi ans1(n);
|
||||
rep(i, n) {
|
||||
ans1[i] = i + 1;
|
||||
}
|
||||
|
||||
if (a == 0 && b == 0) {
|
||||
cout << "YES\n" << ans1 << ans1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (a + b == 1 || a == 0 || b == 0 || a + b > n) {
|
||||
cout << "NO\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
vi ans2(n);
|
||||
|
||||
int i = 0;
|
||||
for (; i < b; i++) {
|
||||
ans2[i] = i + a + 1;
|
||||
}
|
||||
|
||||
for (; i < b + a; i++) {
|
||||
ans2[i] = i - b + 1;
|
||||
}
|
||||
|
||||
for (; i < n; i++) {
|
||||
ans2[i] = ans1[i];
|
||||
}
|
||||
|
||||
cout << "YES\n";
|
||||
cout << ans1 << ans2;
|
||||
}
|
||||
}
|
||||
121
CSES Problem Set/Range Interval Queries.cpp
Normal file
121
CSES Problem Set/Range Interval Queries.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/3163 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
vi fds(n);
|
||||
map<int, int> var;
|
||||
repv(i, fds) {
|
||||
cin >> i;
|
||||
var[i] = 0;
|
||||
}
|
||||
|
||||
int v = 0;
|
||||
repv(i, var) {
|
||||
i.second = v;
|
||||
v++;
|
||||
}
|
||||
|
||||
vi bit1(var.size() + 1);
|
||||
vi bit2(var.size() + 1);
|
||||
|
||||
auto add = [&](vi &bit, int i, int v) {
|
||||
for (; i <= var.size(); i += i&(-i)) {
|
||||
bit[i] += v;
|
||||
}
|
||||
};
|
||||
|
||||
auto get = [&](vi &bit, int i) {
|
||||
int ans = 0;
|
||||
for (; i > 0; i -= i&(-i)) {
|
||||
ans += bit[i];
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int l, r, a, b;
|
||||
cin >> l >> r >> a >> b;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
244
CSES Problem Set/Range Updates and Sums.cpp
Normal file
244
CSES Problem Set/Range Updates and Sums.cpp
Normal file
@@ -0,0 +1,244 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1735 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
while (__builtin_popcount(n) != 1) {
|
||||
n++;
|
||||
fds.push_back(0);
|
||||
}
|
||||
|
||||
vl seg(n * 2);
|
||||
vl lazyadd(n * 2);
|
||||
vl lazyset(n * 2);
|
||||
V<bool> updadd(n * 2);
|
||||
V<bool> updset(n * 2);
|
||||
|
||||
nrep(i, n, n * 2) {
|
||||
seg[i] = fds[i - n];
|
||||
}
|
||||
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
seg[i] = seg[i * 2] + seg[i * 2 + 1];
|
||||
}
|
||||
|
||||
auto propagateset = [&](int i, int l, int r) {
|
||||
if (!updset[i]) {
|
||||
return;
|
||||
}
|
||||
|
||||
updset[i] = false;
|
||||
|
||||
if (i >= n) {
|
||||
seg[i] = lazyset[i];
|
||||
return;
|
||||
}
|
||||
|
||||
seg[i] = lazyset[i] * (r - l + 1);
|
||||
lazyset[i * 2] = lazyset[i];
|
||||
lazyset[i * 2 + 1] = lazyset[i];
|
||||
updset[i * 2] = true;
|
||||
updset[i * 2 + 1] = true;
|
||||
|
||||
lazyadd[i * 2] = 0;
|
||||
lazyadd[i * 2 + 1] = 0;
|
||||
updadd[i * 2] = false;
|
||||
updadd[i * 2 + 1] = false;
|
||||
};
|
||||
|
||||
auto propagateadd = [&](int i, int l, int r) {
|
||||
if (!updadd[i]) {
|
||||
return;
|
||||
}
|
||||
|
||||
updadd[i] = false;
|
||||
|
||||
if (i >= n) {
|
||||
seg[i] += lazyadd[i];
|
||||
lazyadd[i] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
seg[i] += lazyadd[i] * (r - l + 1);
|
||||
lazyadd[i * 2] += lazyadd[i];
|
||||
lazyadd[i * 2 + 1] += lazyadd[i];
|
||||
updadd[i * 2] = true;
|
||||
updadd[i * 2 + 1] = true;
|
||||
lazyadd[i] = 0;
|
||||
};
|
||||
|
||||
auto propagate = [&](int i, int l, int r) {
|
||||
propagateset(i, l, r);
|
||||
propagateadd(i, l, r);
|
||||
};
|
||||
|
||||
function<void(int, int, int, int, int, ll v)> updateset = [&](int i, int l, int r, int tl, int tr, ll v) {
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l > tr || r < tl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
lazyset[i] = v;
|
||||
updset[i] = true;
|
||||
updadd[i] = false;
|
||||
lazyadd[i] = 0;
|
||||
propagate(i, l, r);
|
||||
return;
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
updateset(i * 2, l, mid, tl, tr, v);
|
||||
updateset(i * 2 + 1, mid + 1, r, tl, tr, v);
|
||||
|
||||
seg[i] = seg[i * 2] + seg[i * 2 + 1];
|
||||
};
|
||||
|
||||
function<void(int, int, int, int, int, ll v)> updateadd = [&](int i, int l, int r, int tl, int tr, ll v) {
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l > tr || r < tl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
lazyadd[i] += v;
|
||||
updadd[i] = true;
|
||||
propagate(i, l, r);
|
||||
return;
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
updateadd(i * 2, l, mid, tl, tr, v);
|
||||
updateadd(i * 2 + 1, mid + 1, r, tl, tr, v);
|
||||
|
||||
seg[i] = seg[i * 2] + seg[i * 2 + 1];
|
||||
};
|
||||
|
||||
function<ll(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) {
|
||||
if (l > tr || r < tl) {
|
||||
return 0LL;
|
||||
}
|
||||
|
||||
propagate(i, l, r);
|
||||
|
||||
if (l >= tl && r <= tr) {
|
||||
return seg[i];
|
||||
}
|
||||
|
||||
int mid = (l + r) >> 1;
|
||||
|
||||
return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr);
|
||||
};
|
||||
|
||||
while (m--) {
|
||||
int op;
|
||||
cin >> op;
|
||||
|
||||
if (op == 1) {
|
||||
int l, r;
|
||||
ll v;
|
||||
cin >> l >> r >> v;
|
||||
updateadd(1, 0, n - 1, l - 1, r - 1, v);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (op == 2) {
|
||||
int l, r;
|
||||
ll v;
|
||||
cin >> l >> r >> v;
|
||||
updateset(1, 0, n - 1, l - 1, r - 1, v);
|
||||
continue;
|
||||
}
|
||||
|
||||
int l, r;
|
||||
cin >> l >> r;
|
||||
cout << query(1, 0, n - 1, l - 1, r - 1) << '\n';
|
||||
}
|
||||
}
|
||||
93
CSES Problem Set/Reading Books.cpp
Normal file
93
CSES Problem Set/Reading Books.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1631 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
sortv(fds);
|
||||
|
||||
ll sum = 0;
|
||||
|
||||
vl pref(n + 1);
|
||||
rep(i, n) {
|
||||
pref[i + 1] = pref[i] + fds[i];
|
||||
}
|
||||
|
||||
cout << (fds.back() >= pref.back() - fds.back() ? fds.back() * 2 : pref.back()) << '\n';
|
||||
}
|
||||
109
CSES Problem Set/Restaurant Customers.cpp
Normal file
109
CSES Problem Set/Restaurant Customers.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1619 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<pair<int, int>> query(n);
|
||||
map<int, int> var;
|
||||
for (auto &[a, b] : query) {
|
||||
cin >> a >> b;
|
||||
var[a] = 0;
|
||||
var[b] = 0;
|
||||
}
|
||||
|
||||
int v = 0;
|
||||
for (auto &i : var) {
|
||||
i.second = v;
|
||||
v++;
|
||||
}
|
||||
|
||||
vi ans(var.size() + 1, 0);
|
||||
|
||||
for (auto [a, b] : query) {
|
||||
ans[var[a]]++;
|
||||
ans[var[b] + 1]--;
|
||||
}
|
||||
|
||||
int maximus = 0;
|
||||
int count = 0;
|
||||
repv(i, ans) {
|
||||
count += i;
|
||||
rmax(maximus, count);
|
||||
}
|
||||
|
||||
cout << maximus << '\n';
|
||||
}
|
||||
126
CSES Problem Set/Room Allocation.cpp
Normal file
126
CSES Problem Set/Room Allocation.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1164 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<tuple<int, ll, ll>> cus(n);
|
||||
map<ll, int> var;
|
||||
size_t ind = 0;
|
||||
for (auto &[i, a, b] : cus) {
|
||||
cin >> a >> b;
|
||||
var[a] = 0;
|
||||
var[b] = 0;
|
||||
i = ind;
|
||||
ind++;
|
||||
}
|
||||
|
||||
int v = 0;
|
||||
for (auto &i : var) {
|
||||
i.second = v;
|
||||
v++;
|
||||
}
|
||||
|
||||
V<tuple<ll, int, int>> act;
|
||||
|
||||
for (auto &[i, a, b] : cus) {
|
||||
a = var[a];
|
||||
b = var[b];
|
||||
act.emplace_back(a, 0, i);
|
||||
act.emplace_back(b, 1, i);
|
||||
}
|
||||
|
||||
int k = 1;
|
||||
priority_queue<int, vi, greater<>> pos;
|
||||
for (size_t i = 1; i <= n; i++) {
|
||||
pos.push(i);
|
||||
}
|
||||
vi ans(n);
|
||||
|
||||
sortv(act);
|
||||
|
||||
for (auto [a, yes, i] : act) {
|
||||
if (!yes) {
|
||||
ans[i] = pos.top();
|
||||
pos.pop();
|
||||
rmax(k, ans[i]);
|
||||
} else {
|
||||
pos.push(ans[i]);
|
||||
}
|
||||
}
|
||||
|
||||
cout << k << '\n';
|
||||
cout << ans;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user