-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Codeforces Gym: The 2022 ICPC Asia Xian Regional Contest
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @file 104077A.cpp | ||
* @author Macesuted ([email protected]) | ||
* @date 2024-10-27 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define endl '\n' | ||
|
||
#define maxn 300005 | ||
|
||
typedef tuple<int, int, int> tiii; | ||
|
||
int lef[maxn], rig[maxn], jmp[maxn], S[maxn], T[maxn]; | ||
map<int, int> fnd; | ||
tiii ques[maxn]; | ||
vector<int> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @file 104077E.cpp | ||
* @author Macesuted ([email protected]) | ||
* @date 2024-10-27 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
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; | ||
} |