diff --git a/Codeforces Gym/104077A.cpp b/Codeforces Gym/104077A.cpp new file mode 100644 index 0000000..2e43fc7 --- /dev/null +++ b/Codeforces Gym/104077A.cpp @@ -0,0 +1,76 @@ +/** + * @file 104077A.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-10-27 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +#define maxn 300005 + +typedef tuple tiii; + +int lef[maxn], rig[maxn], jmp[maxn], S[maxn], T[maxn]; +map fnd; +tiii ques[maxn]; +vector pos[maxn]; +int len; + +int jump(int p) { + while (jmp[p] != p) p = jmp[p]; + return p; +} +void rebuild(int p) { + int q = p; + for (int i = 1; i <= len; i++) q = lef[q]; + jmp[q] = q; + for (int i = 1; i <= len; i++) jmp[q] = rig[jmp[q]]; + while (p != q) jmp[rig[q]] = rig[jmp[q]], q = rig[q]; + return; +} + +void solve(void) { + int n, m, q; + cin >> n >> m >> q, len = sqrt(m); + + for (int i = 1; i <= n; i++) pos[i].push_back(m + 1); + for (int i = 1; i <= q; i++) { + int op, a, b = 0; + cin >> op >> a; + if (op == 1) cin >> b; + ques[i] = {op, a, b}; + if (op == 1) pos[a].push_back(b), pos[a + 1].push_back(b); + } + for (int i = 1; i <= n; i++) { + S[i] = T[i - 1] + 1, T[i] = T[i - 1] + pos[i].size(), fnd[T[i]] = i; + sort(pos[i].begin(), pos[i].end()); + for (int j = S[i]; j <= T[i]; j++) lef[j] = max(j - 1, S[i]), rig[j] = min(j + 1, T[i]), jmp[j] = min(j + len, T[i]); + } + for (int i = 1; i <= q; i++) { + auto [op, a, b] = ques[i]; + if (op == 2) { + cout << fnd[jump(S[a])] << endl; + continue; + } + int p = lower_bound(pos[a].begin(), pos[a].end(), b) - pos[a].begin() + S[a], + q = lower_bound(pos[a + 1].begin(), pos[a + 1].end(), b) - pos[a + 1].begin() + S[a + 1]; + swap(lef[rig[p]], lef[rig[q]]), swap(rig[p], rig[q]); + rebuild(p), rebuild(q); + } + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _ = 1; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/104077E.cpp b/Codeforces Gym/104077E.cpp new file mode 100644 index 0000000..a52a2c8 --- /dev/null +++ b/Codeforces Gym/104077E.cpp @@ -0,0 +1,44 @@ +/** + * @file 104077E.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-10-27 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +int64_t pow3[39]; + +int64_t solve(int dep, int64_t l, int64_t r, int64_t ql, int64_t qr, bool pre = false) { + if (ql <= l && r <= qr) return (dep + 1) * 3; + int64_t m1 = r - 2 * pow3[dep], m2 = r - pow3[dep], ans = 0; + if (ql <= m1 && qr >= l) ans = max(ans, solve(dep - 1, l, m1, ql, qr, pre) + pre); + if (ql <= m2 && qr >= m1 + 1) ans = max(ans, solve(dep - 1, m1 + 1, m2, ql, qr, true) + 2); + if (ql <= r && qr >= m2 + 1) ans = max(ans, solve(dep - 1, m2 + 1, r, ql, qr, true) + 3); + return ans; +} + +void solve(void) { + int64_t l, r; + cin >> l >> r; + cout << solve(37, 0, pow3[38] - 1, l, r) << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + pow3[0] = 1; + for (int i = 1; i < 39; i++) pow3[i] = pow3[i - 1] * 3; + + int _ = 1; + cin >> _; + while (_--) solve(); + + return 0; +} \ No newline at end of file