Submission #575931

#TimeUsernameProblemLanguageResultExecution timeMemory
575931SHZhangGame (APIO22_game)C++17
60 / 100
1293 ms3960 KiB
#include "game.h"
#include <queue>
#include <vector>

using namespace std;

vector<int> graph[30005];
vector<int> rgraph[30005];
int n, k;

int first_go[30005], last_come[30005];

int finish = 0;

queue<int> que;

bool inque[30005];

void push_queue(int x)
{
    if (!inque[x]) {
        que.push(x); inque[x] = true;
    }
}

int pop_queue()
{
    int res = que.front(); que.pop();
    inque[res] = false;
    return res;
}

void init(int N, int K) {
    n = N; k = K;
    for (int i = 0; i < n; i++) {
        first_go[i] = n;
        last_come[i] = -1;
    }
    for (int i = 0; i + 1 < k; i++) {
        graph[i].push_back(i+1);
        rgraph[i+1].push_back(i);
    }
    for (int i = 0; i < k; i++) {
        first_go[i] = last_come[i] = i;
    }
}

int add_teleporter(int u, int v) {
    if (v <= u && u < k) return 1;
    graph[u].push_back(v);
    rgraph[v].push_back(u);
    push_queue(u);
    push_queue(v);
    while (!que.empty()) {
        int node = pop_queue();
        int old_first_go = first_go[node];
        for (int i = 0; i < graph[node].size(); i++) {
            int nxt = graph[node][i];
            first_go[node] = min(first_go[node], first_go[nxt]);
        }
        if (old_first_go != first_go[node]) {
            for (int i = 0; i < rgraph[node].size(); i++) {
                int nxt = rgraph[node][i];
                push_queue(nxt);
            }
        }
        int old_last_come = last_come[node];
        for (int i = 0; i < rgraph[node].size(); i++) {
            int nxt = rgraph[node][i];
            last_come[node] = max(last_come[node], last_come[nxt]);
        }
        if (old_last_come != last_come[node]) {
            for (int i = 0; i < graph[node].size(); i++) {
                int nxt = graph[node][i];
                push_queue(nxt);
            }
        }
        if (node >= k && first_go[node] <= last_come[node]) finish = 1;
    }
    return finish;
}

Compilation message (stderr)

game.cpp: In function 'int add_teleporter(int, int)':
game.cpp:57:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for (int i = 0; i < graph[node].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~
game.cpp:62:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |             for (int i = 0; i < rgraph[node].size(); i++) {
      |                             ~~^~~~~~~~~~~~~~~~~~~~~
game.cpp:68:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for (int i = 0; i < rgraph[node].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~
game.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |             for (int i = 0; i < graph[node].size(); i++) {
      |                             ~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...