#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 3e5 + 5;
ll n, m;
struct node {
ll left, right, size;
node *child[2], *father;
void updata() {size = child[0]->size + child[1]->size + right - left + 1;}
} *nul = new node;
void init() {
nul->child[0] = nul->child[1] = nul->father = nul;
nul->left = 1; nul->right = nul->size = 0;
}
node *newNode(ll l, ll r, node *fa) {
node *ptr = new node;
ptr->father = fa; ptr->child[0] = nul; ptr->child[1] = nul;
ptr->left = l; ptr->right = r; ptr->size = r - l + 1;
return ptr;
}
void cect(node *x, node *y, ll p) {x->father = y; y->child[p] = x;}
bool getPos(node *x) {return x->father->child[1] == x;}
void rotate(node *x) {
ll p = getPos(x), fp = getPos(x->father);
node *gfa = x->father->father;
cect(x->child[p ^ 1], x->father, p);
cect(x->father, x, p ^ 1); cect(x, gfa, fp);
x->child[p ^ 1]->updata(); x->updata();
}
void split(node *x, ll k) {
node *tmpChild[2] = {x->child[0], x->child[1]};
if(x->left < k) {
x->child[0] = newNode(x->left, k - 1, x);
if(tmpChild[0] != nul) cect(tmpChild[0], x->child[0], 0);
x->child[0]->updata();
}
if(x->right > k) {
x->child[1] = newNode(k + 1, x->right, x);
if(tmpChild[1] != nul) cect(tmpChild[1], x->child[1], 1);
x->child[1]->updata();
}
x->left = x->right = k;
}
struct splayTree {
node *rt;
splayTree() {rt = newNode(1, 0, nul);}
void build(node *&cur, node *fa, ll l, ll r) {
if(l <= r) {
ll mid = (l + r) >> 1;
cur = newNode(mid * m, mid * m, fa);
build(cur->child[0], cur, l, mid - 1);
build(cur->child[1], cur, mid + 1, r);
cur->updata();
}
}
void splay(node *cur, node *goal) {
while(cur->father != goal) {
node *fa = cur->father, *gfa = cur->father->father;
if(gfa != goal) getPos(cur) == getPos(fa) ? rotate(fa) : rotate(cur);
rotate(cur);
}
}
node *findKth(ll k) {
node *cur = rt->child[1];
while(true) {
if(k <= cur->child[0]->size) cur = cur->child[0];
else if(k > cur->child[0]->size + cur->right - cur->left + 1) {
k = k - cur->child[0]->size - (cur->right - cur->left + 1);
cur = cur->child[1];
} else {
split(cur, cur->left + k - cur->child[0]->size - 1);
splay(cur, rt);
return cur;
}
}
}
ll earse(node *goal) {
node *rplNode = goal->child[0];
while(rplNode->child[1] != nul) rplNode = rplNode->child[1];
if(rplNode != nul) {
splay(rplNode, goal);
cect(rplNode, rt, 1);
cect(goal->child[1], rplNode, 1);
rplNode->updata();
} else cect(goal->child[1], rt, 1);
ll id = goal->left;
delete goal;
return id;
}
void insLast(ll k) {
node *cur = rt;
while(cur->child[1] != nul) cur = cur->child[1];
if(cur != rt) splay(cur, rt);
cur->child[1] = newNode(k, k, cur);
cur->size++;
}
} row[MAXN], lastLine;
ll leave(ll x, ll y) {
ll id;
if(y != m) {
lastLine.insLast(id = row[x].earse(row[x].findKth(y)));
row[x].insLast(lastLine.earse(lastLine.findKth(x)));
} else lastLine.insLast(id = lastLine.earse(lastLine.findKth(x)));
return id;
}
int main() {
init();
ll q, x, y;
scanf("%lld%lld%lld", &n, &m, &q);
lastLine.build(lastLine.rt->child[1], lastLine.rt, 1, n);
for(ll i = 1; i <= n; i++)
(row[i].rt)->child[1] = newNode((i - 1) * m + 1, i * m - 1, row[i].rt);
while(q--) {
scanf("%lld%lld", &x, &y);
printf("%lld\n", leave(x, y));
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 4.066 ms | 20 MB + 820 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 4.057 ms | 20 MB + 824 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 4.07 ms | 20 MB + 820 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 4.074 ms | 20 MB + 828 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 4.07 ms | 20 MB + 824 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 4.067 ms | 20 MB + 824 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 5.606 ms | 26 MB + 328 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 5.608 ms | 26 MB + 304 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 5.755 ms | 26 MB + 816 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 5.592 ms | 26 MB + 196 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 99.305 ms | 26 MB + 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 98.633 ms | 26 MB + 172 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 401.231 ms | 37 MB + 376 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 374.348 ms | 37 MB + 116 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 396.001 ms | 71 MB + 544 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 416.807 ms | 71 MB + 988 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 172.79 ms | 42 MB + 552 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 166.479 ms | 42 MB + 8 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 684.752 ms | 85 MB + 88 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 676.725 ms | 86 MB + 676 KB | Accepted | Score: 5 | 显示更多 |