#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, q, x, y;
inline int read() {
int s = 0;
char ch = getchar();
while (ch < '0' || ch > '9')
ch = getchar();
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
return s;
}
void write1(ll x) {
if (x > 9) write1(x / 10);
putchar(x % 10 + '0');
}
namespace task1 {
ll a[505][50005], b[50005], ans; int f[50005], cnt;
inline void solve() {
for (int i=1; i<=n; i++)
b[i] = 1ll * i * m;
while (q--) {
x = read(), y = read();
if (y ^ m) {
if (! f[x]) {
f[x] = ++ cnt;
for (int i=1; i<=m; i++)
a[cnt][i] = (x - 1) * m + i;
} int _ = f[x];
ans = a[_][y];
for (int i=y; i<m-1; i++)
a[_][i] = a[_][i+1];
a[_][m-1] = b[x];
} else ans = b[x];
for (int i=x; i<n; i++)
b[i] = b[i+1];
b[n] = ans;
write1(ans), putchar('\n');
}
}
}
namespace task2 {
vector <ll> a; ll b[600005], ans; int r;
inline void solve() {
for (int i=1; i<=m; i++)
a. push_back(i);
for (int i=2; i<=n; i++)
b[i] = 1ll * i * m;
r = n;
while (q--) {
x = read(), y = read();
write1(ans = a[y-1]), putchar('\n');
a. erase(a. begin() + y - 1);
if (n >= 2)
a. push_back(b[r-n+2]), b[++r] = ans;
else
a. push_back(ans);
}
}
}
namespace task3 {
int cnt, r, lim, ans, tree[600005]; ll a[600005], b[600005];
inline void update(int s, int t) {
for (; s<=lim; s+=s&-s)
tree[s] += t;
}
inline int search(int x) {
int res = 0, sum = 0;
for (int i=19; ~i; i--)
if (res + (1 << i) <= lim && sum + tree[res + (1 << i)] < x)
res += 1 << i, sum += tree[res];
return res + 1;
}
inline void solve() {
cnt = m, lim = m + q;
for (int i=1; i<=m; i++)
a[i] = i, update(i, 1);
for (int i=2; i<=n; i++)
b[i] = 1ll * i * m;
r = n;
while (q--) {
x = read(), y = read(), ans = search(y);
write1(a[ans]), putchar('\n');
update(ans, -1);
if (n >= 2)
a[++cnt] = b[r-n+2], update(cnt, 1), b[++r] = a[ans];
else
a[++cnt] = a[ans], update(cnt, 1);
}
}
}
int main() {
// freopen ("phalanx.in", "r", stdin);
// freopen ("phalanx.out", "w", stdout);
cin >> n >> m >> q;
if (q <= 500) task1 :: solve();
else task3 :: solve();
return 0;
}
/*
2 2 3
1 1
2 2
1 2
3 5 10
1 1
1 4
1 5
1 3
1 2
1 5
1 2
1 4
1 1
1 3
2 3 2
1 2
1 3
*/
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 474.63 us | 1 MB + 444 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 412.74 us | 1 MB + 316 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 444.49 us | 1 MB + 124 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 457.52 us | 1 MB + 552 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 374.23 us | 1 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 435.05 us | 1 MB + 560 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 15.687 ms | 50 MB + 684 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #8 | 15.585 ms | 53 MB + 956 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 21.403 ms | 60 MB + 132 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #10 | 17.84 ms | 65 MB + 40 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #11 | 27.544 ms | 2 MB + 772 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 27.385 ms | 2 MB + 756 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 105.36 ms | 8 MB + 356 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 99.614 ms | 8 MB + 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 105.022 ms | 12 MB + 860 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 109.469 ms | 13 MB + 104 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 29.807 ms | 4 MB + 536 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #18 | 28.756 ms | 4 MB + 436 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #19 | 109.047 ms | 13 MB + 300 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #20 | 110.089 ms | 13 MB + 676 KB | Wrong Answer | Score: 0 | 显示更多 |