diff options
author | Anthony Wang | 2022-03-15 20:38:51 -0500 |
---|---|---|
committer | Anthony Wang | 2022-03-15 20:38:51 -0500 |
commit | 896c6ceeff605c935a5538e3d9eca40ea3c79c56 (patch) | |
tree | 69d97b50f5ced801f6043cbef0219a941239f892 /16.5 | |
parent | 4c488e4c73f2d0e2b96bf0c444c05d42e566dd11 (diff) |
Restructure directories AGAIN so contests from the same season are in the same directory
Diffstat (limited to '16.5')
-rw-r--r-- | 16.5/dec/plat/triangles.cpp | 113 | ||||
-rw-r--r-- | 16.5/feb/plat/friendcross.cpp | 68 | ||||
-rw-r--r-- | 16.5/feb/plat/mincross.cpp | 57 | ||||
-rw-r--r-- | 16.5/feb/plat/nocross.cpp | 57 | ||||
-rw-r--r-- | 16.5/open/plat/cowbasic.cpp | 103 |
5 files changed, 398 insertions, 0 deletions
diff --git a/16.5/dec/plat/triangles.cpp b/16.5/dec/plat/triangles.cpp new file mode 100644 index 0000000..1a2fdd9 --- /dev/null +++ b/16.5/dec/plat/triangles.cpp @@ -0,0 +1,113 @@ +#include <algorithm> +#include <iostream> +#include <fstream> +#include <cstring> +#include <vector> +#include <cmath> +using namespace std; +typedef long long ll; +typedef pair<int, int> ii; +typedef vector<int> vi; +typedef vector<ii> vii; + +struct point { + ll x, y; + point() { x = y = 0; } + point(ll _x, ll _y) : x(_x), y(_y) {} + bool operator < (point p) const { + return (x == p.x && y < p.y) || x < p.x; + } + bool operator == (point p) const { + return x == p.x && y == p.y; + } +} T[305]; + +struct vec { + ll x, y; + vec(ll _x, ll _y) : x(_x), y(_y) {} +}; + +vec to_vec(point a, point b) { + return vec(b.x - a.x, b.y - a.y); +} + +ll dot(vec a, vec b) { + return (a.x * b.x + a.y * b.y); +} + +ll norm_sq(vec v) { + return v.x * v.x + v.y * v.y; +} + +ll cross(vec a, vec b) { + return a.x * b.y - a.y * b.x; +} + +bool ccw(point p, point q, point r) { + return cross(to_vec(p, q), to_vec(p, r)) > 0; +} + +struct line { ll a, b, c; }; + +line to_line(point p1, point p2) { + return { p2.y - p1.y, p1.x - p2.x, p1.x * p2.y - p1.y * p2.x }; +} + +class fenwick_tree { +private: vector<int> FT; +public: + fenwick_tree(int N) { FT.assign(N + 1, 0); } + void update(int x, int val) { for (; x < FT.size(); x += x & -x) FT[x] += val; } + int query(int x) { int ret = 0; for (; x > 0; x -= x & -x) ret += FT[x]; return ret; } + int query(int x, int y) { return query(y) - (x == 1 ? 0 : query(x - 1)); } +}; + +int ans[305] = { 0 }; + +int main() { + ifstream cin("triangles.in"); + ofstream cout("triangles.out"); + + int N; + cin >> N; + for (int i = 0; i < N; i++) cin >> T[i].x >> T[i].y; + sort(T, T + N); + + for (int i = 0; i < N - 1; i++) { + for (int j = i + 1; j < N; j++) { + line L = to_line(T[i], T[j]); + + vector<pair<ii, point>> A, B; + for (int k = i + 1; k < N; k++) { + if (k != i && k != j) { + if (T[k].x * L.a + T[k].y * L.b < L.c) { + A.emplace_back(ii(k, k), T[k]); + } + } + } + + vector<ii> l, r; + int N = A.size(); + for (int i = 0; i < N; i++) { + l.emplace_back(A[i].first.first, i); + r.emplace_back(A[i].first.second, i); + } + sort(l.begin(), l.end(), [i](const ii& a, const ii& b) { return ccw(T[b.first], T[i], T[a.first]); }); + sort(r.begin(), r.end(), [j](const ii& a, const ii& b) { return ccw(T[a.first], T[j], T[b.first]); }); + for (int i = 0; i < N; i++) { + A[l[i].second].first.first = i + 1; + A[r[i].second].first.second = i + 1; + } + + sort(A.begin(), A.end()); + + fenwick_tree FT(N); + for (auto& p : A) { + ans[FT.query(p.first.second)]++; + FT.update(p.first.second, 1); + } + } + } + + for (int i = 0; i < N - 2; i++) cout << ans[i] << endl; +} diff --git a/16.5/feb/plat/friendcross.cpp b/16.5/feb/plat/friendcross.cpp new file mode 100644 index 0000000..de7c198 --- /dev/null +++ b/16.5/feb/plat/friendcross.cpp @@ -0,0 +1,68 @@ +#include <bits/stdc++.h> +#include <ext/pb_ds/tree_policy.hpp> +#include <ext/pb_ds/assoc_container.hpp> +#include <ext/rope> +#define init_io ios_base::sync_with_stdio(false); cin.tie(NULL) +#define FOR(i, a, b, in) for (int i = (a); i < (b); i += in) +#define REP(i, a, b) for (int i = (a); i < (b); i++) +#define RFOR(i, a, b, in) for (int i = (a) - 1; i >= (b); i -= in) +#define RREP(i, a, b) for (int i = (a) - 1; i >= (b); i--) +#define trav(a, x) for (auto& a : x) +#define mp make_pair +#define pb push_back +#define f first +#define s second +#define lb lower_bound +#define ub upper_bound +#define sz(x) (int)x.size() +#define all(x) begin(x), end(x) +#define rsz resize +#define mem(a, b) memset(a, (b), sizeof(a)) +using namespace std; +using namespace __gnu_pbds; +using namespace __gnu_cxx; +typedef string str; +typedef long long ll; +typedef long double ld; +typedef complex<ld> cd; +typedef pair<int, int> ii; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; +typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ld> vd; +typedef vector<ii> vii; typedef vector<pl> vpl; typedef vector<pd> vpd; +typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; +typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset; // WARNING: May be broken +constexpr int INF = 1e9; +constexpr ll LINF = 1e18; +constexpr ll MOD = 1e9 + 7; +const ld PI = 4 * atan((ld)1); + +int N, A[100005], B[100005]; +ordered_set S[100005]; + +void update(int x, int y) { for (; x <= N; x += x & -x) S[x].insert(y); } +int query(int x, int y) { int ret = 0; for (; x > 0; x -= x & -x) ret += S[x].order_of_key(y + 1); return ret; } + +int main() { + ifstream cin("friendcross.in"); + ofstream cout("friendcross.out"); + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int K; + cin >> N >> K; + for (int i = 0; i < N; ++i) { + int a; + cin >> a; + A[a] = i + 1; + } + for (int i = 0; i < N; ++i) { + int b; + cin >> b; + B[b] = i + 1; + } + ll ans = 0; + for (int i = N - K - 1; i > 0; --i) { + update(A[i + K + 1], B[i + K + 1]); + ans += query(N, B[i]) + query(A[i], N) - 2 * query(A[i], B[i]); + } + cout << ans << endl; +} diff --git a/16.5/feb/plat/mincross.cpp b/16.5/feb/plat/mincross.cpp new file mode 100644 index 0000000..a5befd0 --- /dev/null +++ b/16.5/feb/plat/mincross.cpp @@ -0,0 +1,57 @@ +#include <algorithm> +#include <iostream> +#include <fstream> +#include <string> +#include <vector> +#include <queue> +#include <set> +#include <map> +#include <unordered_set> +#include <unordered_map> +#include <cmath> +#include <cstring> +using namespace std; +typedef long long ll; +typedef pair<int, int> ii; +typedef vector<int> vi; +typedef vector<ii> vii; +constexpr auto INF = (ll)1e18; + +class fenwick_tree { +private: vector<int> FT; +public: + fenwick_tree(int N) { FT.assign(N + 1, 0); } + void update(int x, int val) { for (; x < FT.size(); x += x & -x) FT[x] += val; } + int query(int x) { int ret = 0; for (; x > 0; x -= x & -x) ret += FT[x]; return ret; } + int query(int x, int y) { return query(y) - (x == 1 ? 0 : query(x - 1)); } +}; + +int A[100005], B[100005], RA[100005], RB[100005]; + +int main() { + ifstream cin("mincross.in"); + ofstream cout("mincross.out"); + + int N; + cin >> N; + for (int i = 0; i < N; ++i) cin >> A[i]; + for (int i = 0; i < N; ++i) cin >> B[i]; + for (int i = 0; i < N; ++i) RA[A[i]] = i + 1; + for (int i = 0; i < N; ++i) RB[B[i]] = i + 1; + + fenwick_tree FT(N); + ll cnt = 0, ans = INF; + for (int i = 0; i < N; ++i) { + cnt += FT.query(RA[B[i]], N); + FT.update(RA[B[i]], 1); + } + for (int i = 0; i < N; ++i) { + cnt += N - 2 * RA[B[i]] + 1; + ans = min(cnt, ans); + } + for (int i = 0; i < N; ++i) { + cnt += N - 2 * RB[A[i]] + 1; + ans = min(cnt, ans); + } + cout << ans << endl; +} diff --git a/16.5/feb/plat/nocross.cpp b/16.5/feb/plat/nocross.cpp new file mode 100644 index 0000000..5ce1b7c --- /dev/null +++ b/16.5/feb/plat/nocross.cpp @@ -0,0 +1,57 @@ +#include <algorithm> +#include <iostream> +#include <fstream> +#include <string> +#include <vector> +#include <queue> +#include <set> +#include <map> +#include <unordered_set> +#include <unordered_map> +#include <cmath> +#include <cstring> +using namespace std; +typedef long long ll; +typedef pair<int, int> ii; +typedef vector<int> vi; +typedef vector<ii> vii; +constexpr auto INF = (int)1e9; +constexpr auto MAXN = 100005; + +int N, A[100005], B[100005], R[100005], seg[4 * MAXN] = { 0 }; + +void update(int x, int v, int l = 0, int r = -1, int n = 1) { + if (r == -1) r = N - 1; + if (l == r) seg[n] = max(v, seg[n]); + else { + int m = (l + r) >> 1; + x <= m ? update(x, v, l, m, n << 1) : update(x, v, m + 1, r, n << 1 | 1); + seg[n] = max(seg[n << 1], seg[n << 1 | 1]); + } +} + +int query(int a, int b, int l = 0, int r = -1, int n = 1) { + if (r == -1) r = N - 1; + if (l > b || r < a) return 0; + if (l >= a && r <= b) return seg[n]; + int m = (l + r) >> 1; + return max(query(a, b, l, m, n << 1), query(a, b, m + 1, r, n << 1 | 1)); +} + +int main() { + ifstream cin("nocross.in"); + ofstream cout("nocross.out"); + + cin >> N; + for (int i = 0; i < N; ++i) cin >> A[i], A[i]--; + for (int i = 0; i < N; ++i) cin >> B[i], B[i]--; + for (int i = 0; i < N; ++i) R[B[i]] = i; + + for (int i = 0; i < N; ++i) { + vii u; + for (int j = max(A[i] - 4, 0); j < min(A[i] + 5, N); ++j) u.emplace_back(R[j], query(0, R[j] - 1) + 1); + for (auto q : u) update(q.first, q.second); + } + + cout << query(0, N - 1) << endl; +} diff --git a/16.5/open/plat/cowbasic.cpp b/16.5/open/plat/cowbasic.cpp new file mode 100644 index 0000000..8ef44c9 --- /dev/null +++ b/16.5/open/plat/cowbasic.cpp @@ -0,0 +1,103 @@ +#include <bits/stdc++.h> +#define init_io(pname) ifstream cin((string)pname+".in"); ofstream cout((string)pname+".out"); ios_base::sync_with_stdio(false); cin.tie(NULL) +#define FOR(i, a, b, in) for (int i = (a); i < (b); i += in) +#define REP(i, a, b) for (int i = (a); i < (b); i++) +#define RFOR(i, a, b, in) for (int i = (a) - 1; i >= (b); i -= in) +#define RREP(i, a, b) for (int i = (a) - 1; i >= (b); i--) +#define trav(a, x) for (auto& a : x) +#define mp make_pair +#define pb push_back +#define f first +#define s second +#define lb lower_bound +#define ub upper_bound +#define sz(x) (int)x.size() +#define all(x) begin(x), end(x) +#define rsz resize +#define mem(a, b) memset(a, (b), sizeof(a)) +using namespace std; +typedef string str; +typedef long long ll; +typedef long double ld; +typedef pair<int, int> ii; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; +typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ld> vd; +typedef vector<ii> vii; typedef vector<pl> vpl; typedef vector<pd> vpd; +constexpr auto INF = (int)1e9; +constexpr auto LINF = (ll)1e18; +constexpr auto MOD = 1000000007; + +typedef vector<vi> matrix; + +int d = 0; +map<str, int> var; // Variables +vector<str> program; // Program +stack<int> loop_st; // Loop stack +vector<matrix> mat_st; // Matrix stack + +void mult(matrix& A, matrix& B) { // B = A * B; + matrix res(d + 1, vi(d + 1, 0)); + for (int i = 0; i <= d; i++) { + for (int j = 0; j <= d; j++) { + for (int k = 0; k <= d; k++) res[i][j] = ((ll)A[i][k] * (ll)B[k][j] + res[i][j]) % MOD; + } + } + B = res; +} + +void pow(matrix& A, int n) { // A = A ^ n + matrix res(d + 1, vi(d + 1, 0)); + for (int i = 0; i <= d; i++) res[i][i] = 1; + while (n) { + if (n % 2 == 1) mult(A, res); + mult(A, A); + n /= 2; + } + A = res; +} + +int main() { + init_io("cowbasic"); + + str input; + while (getline(cin, input)) program.push_back(input); + for (auto& line : program) { // Record variables + stringstream ss(line); + str n; ss >> n; + if (islower(n[0]) && var.find(n) == var.end()) var[n] = d++; + } + mat_st.push_back({}); + mat_st.back().resize(d + 1, vi(d + 1, 0)); + for (int i = 0; i <= d; i++) mat_st.back()[i][i] = 1; + for (auto& line : program) { // Process each line + stringstream ss(line); + str n; ss >> n; + if (islower(n[0])) { // Variable assignment + matrix M(d + 1, vi(d + 1, 0)); + for (int i = 0; i <= d; i++) { + if (i != var[n]) M[i][i] = 1; + } + str tmp; + while (ss >> tmp) { + if (islower(tmp[0])) M[var[n]][var[tmp]]++; + else if (isdigit(tmp[0])) M[var[n]][d] += stoi(tmp); + } + mult(M, mat_st.back()); + } + else if (isdigit(n[0])) { // Begin loop + mat_st.push_back({}); + mat_st.back().resize(d + 1, vi(d + 1, 0)); + for (int i = 0; i <= d; i++) mat_st.back()[i][i] = 1; + loop_st.push(stoi(n)); + } + else if (n[0] == '}') { // End loop + pow(mat_st.back(), loop_st.top()); + mult(mat_st.back(), mat_st[mat_st.size() - 2]); + mat_st.pop_back(); + loop_st.pop(); + } + else { // Return + ss >> n; + cout << mat_st.back()[var[n]][d] << endl; + } + } +} |