编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#11713 #1001. A. 神秘谜题 Time Limit Exceeded 0 2045 ms 1144 K C++ / 2.2 K qushaoru202 2019-07-05 15:40:59
显示原始代码
#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

#define LETTER 2
struct Trie {
    int num;
    int next[LETTER];
} trie[500001];
int cnt;
int biase = 0;

void init() {
    cnt = 0;
    memset(trie, 0, sizeof(Trie));
}

void insert(queue<int> s) {
    int cur = 0;
    while (!s.empty()) {
        int &pos = trie[cur].next[(s.front() + biase) % 2];
        s.pop();
        if (!pos) {
            pos = ++cnt;
            memset(trie + cnt, 0, sizeof(Trie));
        }
        cur = pos;
    }
    trie[cur].num++;
}

int add(int num1 = 0) {
    int &cur = trie[num1].next[0], &cur1 = trie[num1].next[1];
    int result = 0;
    if (cur1 && cur) {
        result = (trie[cur].num % 2 + trie[cur1].num % 2) % 2;
        result += (add(cur) % 2 + add(cur1) % 2) % 2 * 2;
        result += (add(cur) / 2) * 2;
        result += (add(cur1) / 2) * 2;
        return result;
    } else if (cur1 && !cur) {
        result += trie[cur1].num % 2;
        result += add(cur1) * 2;
        return result;
    } else if (cur && !cur1) {
        result += trie[cur].num % 2;
        result += add(cur) * 2;
        return result;
    }
    return 0;
}

void change_end(int num1 = 0) {
    int &cur = trie[num1].next[(biase) % 2], &cur1 = trie[num1].next[(1 + biase) % 2];
    int &tmp_cur = trie[cur].next[biase % 2], &tmp_cur1 = trie[num1].next[(1 + biase) % 2];
    if (!tmp_cur && !tmp_cur1) {
        tmp_cur1 = cnt++;
        memset(trie + cnt, 0, sizeof(Trie));
    }
    if (cur)
        change_end(cur);
    if (cur1)
        change_end(cur1);
}

void plus_one(int num1 = 0) {
    biase++;
    change_end();
}

void bfs(int num1 = 0) {
    int &cur = trie[num1].next[biase % 2];
    int &cur1 = trie[num1].next[(1 + biase) % 2];
    if (cur) {
        cout << 0 << " ";
        bfs(cur);
    }
    cout << endl;
    if (cur1) {
        cout << 1 << " ";
        bfs(cur1);
    } else
        return;
}

int main() {
    int n, choice;
    int binary;
    init();
    queue<int> mine;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        while (!mine.empty()) mine.pop();
        std::cin >> choice;
        if (choice == 2) {
            std::cin >> choice;
            while (choice > 0) {
                binary = choice % 2;
                choice = choice / 2;
                mine.push(binary);
            }
            insert(mine);
        } else if (choice == 1)
            plus_one();
        cout << add() << endl;
    }
    return 0;
}
子任务 #1
Time Limit Exceeded
得分:0
测试点 #1
Time Limit Exceeded
得分:0
用时:1004 ms
内存:1128 KiB

输入文件(1.in

200000
2 526767110
2 724642759
2 567837900
2 104106873
2 357915481
2 33997211
2 444788944
2 
<1586974 bytes omitted>

答案文件(1.ans

526767110
877985729
361528077
330887284
116239149
82537142
510237286
843295274
453728745
55
<2188330 bytes omitted>

用户输出

268435456
402653184
469762048
352321536
377487360
314572800
281018368
281018368
312475648
329252864
329252864
329252864
33764147
<40012 bytes omitted>
测试点 #2
Time Limit Exceeded
得分:0
用时:1041 ms
内存:1144 KiB

输入文件(2.in

200000
2 515979308
2 512702340
2 684230440
2 488136957
2 598252313
2 283603971
2 349877373
2
<1586842 bytes omitted>

答案文件(2.ans

515979308
5115816
679905408
899606653
372667236
114362215
302756634
473674072
520218589
525
<2192841 bytes omitted>

用户输出

268435456
268435456
402653184
335544320
402653184
369098752
369098752
301989888
287309824
254017536
287571968
279183360
27918336
<41022 bytes omitted>