#54507: C++解答


kita197 (KK)


#include <cstdio>
#ifdef _WIN32
    #define putchar_fast _putchar_nolock
#else
    #define putchar_fast putchar_unlocked
#endif
const int MAXN = 2000005;
int L[MAXN], R[MAXN];
bool isDead[MAXN];
inline char get_char() {
    static char buf[1 << 20], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++;
}
inline int read() {
    int x = 0;
    char c = get_char();
    while (c != EOF && (c < '0' || c > '9')) c = get_char();
    if (c == EOF) return -1;
    while (c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c - '0');
        c = get_char();
    }
    return x;
}
inline void writeInt(int x) {
    if (x == 0) { putchar_fast('0'); return; }
    static char s[12];
    int len = 0;
    while (x) {
        s[len++] = (x % 10) + '0';
        x /= 10;
    }
    while (len--) putchar_fast(s[len]);
}
int main() {
    int N = read();
    int M = read();
    if (N == -1) return 0;
    for (int i = 0; i <= N + 1; i++) {
        L[i] = i - 1;
        R[i] = i + 1;
        isDead[i] = false;
    }
    L[0] = 0;
    R[0] = 1;
    R[N] = 0;
    L[N+1] = N;
    R[N+1] = 0;

    while (M--) {
        int k = read();
        if (k == -1) break;
        if (isDead[k]) {
            static const char* msg1 = "我大意了啊~沒有閃\n";
            for(int i=0; msg1[i]; ++i) putchar_fast(msg1[i]);
            continue;
        }
        int t1 = R[k];
        if (t1 == 0 || R[t1] == 0) {
            static const char* msg2 = "來~ 騙\n";
            for(int i=0; msg2[i]; ++i) putchar_fast(msg2[i]);
        } else {
            int t2 = R[t1];
            writeInt(t2);
            putchar_fast('\n');
            isDead[t2] = true;
            int prv = L[t2];
            int nxt = R[t2];
            if (prv != 0) R[prv] = nxt;
            else R[0] = nxt;
            if (nxt != 0) L[nxt] = prv;
        }
    }
    return 0;
}