1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| #include<cstdio> using namespace std; long long f(-1); inline char GetCharacter() { static char buf[2000000], *p1 = buf, *p2 = buf; return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 2000000, stdin), p1 == p2) ? EOF : *p1++; } #define IS_DIGIT(c) (c >= '0' && c <= '9') inline void Read(long long &x) { f = 1, x = 0; static char c = GetCharacter(); while (!IS_DIGIT(c)) { if (c == '-') f = -1; c = GetCharacter(); } while (IS_DIGIT(c)) x = x * 10 + c - '0', c = GetCharacter(); x *= f; } #undef IS_DIGIT struct node { long long l, r, size; node *left, *right, *father; node(const long long &le, const long long &ri) { l = le, r = ri, size = r - l + 1; left = right = father = NULL; } }; class splay_tree { public: node *root; private: inline void update(register node *x) { if (x) x->size = (x->left ? x->left->size : 0) + (x->right ? x->right->size : 0) + x->r - x->l + 1ll; } inline void left_rotate(register node *x) { register node *y = x->father; y->right = x->left; if (x->left) x->left->father = y; x->father = y->father; if (y->father) { if (y == y->father->left) y->father->left = x; else y->father->right = x; } x->left = y; y->father = x; update(y), update(x); } inline void right_rotate(register node *x) { register node *y = x->father; y->left = x->right; if (x->right) x->right->father = y; x->father = y->father; if (y->father) { if (y == y->father->right) y->father->right = x; else y->father->left = x; } x->right = y; y->father = x; update(y), update(x); } inline void splay(register node *x, const node *target=NULL) { register node *y; while (x->father != target) { y = x->father; if (x == y->left) { if (y->father != target && y == y->father->left) right_rotate(y); right_rotate(x); } else { if (y->father != target && y == y->father->right) left_rotate(y); left_rotate(x); } } if (!target) root = x; } inline node *split(register node *x,const long long &rnk) { if (1ll < rnk && x->r - x->l + 1ll > rnk) { register node *y = new node(x->l + rnk - 1ll, x->l + rnk - 1ll), *z = new node(x->l + rnk, x->r); x->r = x->l + rnk - 2ll, x->size = x->r - x->l + 1ll; z->right = x->right; if (x->right) x->right->father = z; x->right = NULL; y->father = x->father; if (x->father) { if (x == x->father->right) x->father->right = y; else x->father->left = y; } else root = y; y->left = x, y->right = z; x->father = z->father = y; update(x), update(z), update(y); return y; } else if (x->l == x->r) { return x; } else if (rnk == 1ll) { register node *y = new node(x->l, x->l); ++x->l, --x->size; y->father = x->father; if (x->father) { if (x == x->father->left) x->father->left = y; else x->father->right = y; } else root = y; y->left = x->left; if (x->left) x->left->father = y; x->left = NULL; x->father = y, y->right = x; update(x), update(y); return y; } else { register node *y = new node(x->r, x->r); --x->r, --x->size; y->father = x->father; if (x->father) { if (x == x->father->right) x->father->right = y; else x->father->left = y; } else root = y; y->right = x->right; if (x->right) x->right->father = y; x->right = NULL; x->father = y, y->left = x; update(x), update(y); return y; } } inline node *erase(register node *x) { splay(x); if (!x->left && !x->right) { root = NULL; return x; } else if (!x->right) { root = x->left; root->father = NULL; x->left = NULL; x->size = x->r - x->l + 1; return x; } else if (!x->left) { root = x->right; root->father = NULL; x->right = NULL; x->size = x->r - x->l + 1; return x; } else { register node *y = x->right; while (y->left) y = y->left; splay(y, x); y->left = x->left; x->left->father = y; root = y, y->father = x->left = x->right = NULL; update(root); x->size = x->r - x->l + 1; return x; } } public: splay_tree() { root = NULL; } inline void push_back(const long long &l, const long long &r) { register node *x = root; if (!x) { root = new node(l,r); } else { while (x->right) x = x->right; splay(x); x->right = new node(l, r); x->right->father = x; update(x); } } inline void push_back(register node *x) { register node *y = root, *z = NULL; while (y) { z = y; y = y->right; } if (z) { splay(z); z->right = x, x->father = z; update(z); } else { root = x; } } inline node *pop(long long rnk) { register node *x = root; while (1) { if (x->left) { if (x->left->size >= rnk) x = x->left; else { rnk -= x->left->size; if (x->r - x->l + 1 >= rnk) break; else rnk -= x->r - x->l + 1, x = x->right; } } else { if (x->r - x->l + 1 >= rnk) break; else rnk -= x->r - x->l + 1, x = x->right; } } return erase(split(x, rnk)); } }; long long n, m, q, t1, t2; splay_tree S[300010]; int main(int argc, char **argv) { Read(n), Read(m), Read(q); for (register int i(1); i <= n; ++i) S[i].push_back(1ll + (i - 1ll) * m, i * m - 1); for (register int i(1); i <= n; ++i) S[0].push_back(i * m, i * m); while (q--) { Read(t1), Read(t2); if (t2 == m) { register node *x = S[0].pop(t1); printf("%lld\n", x->l); S[0].push_back(x); } else { register node *x = S[t1].pop(t2); printf("%lld\n", x->l); S[t1].push_back(S[0].pop(t1)), S[0].push_back(x); } } return 0; }
|