Submission #1130793

#TimeUsernameProblemLanguageResultExecution timeMemory
1130793patgraRoad Service 2 (JOI24_ho_t5)C++20
48 / 100
481 ms120808 KiB
#include <bits/stdc++.h>

#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto& a : (b))
#define repIn2(a, b, c) for(auto& [a, b] : c)

#define ll long long

constexpr bool dbg = 0;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl

using namespace std;

constexpr int maxn = 1e6 + 7, maxLog = 21;
int h, w, q, n;
vector<int> graf[maxn];
int component[maxn], top[maxn], bottom[maxn];
int jp[maxn][maxLog];
int rowCosts[maxn];

void input() {
    scanf("%d%d%d", &h, &w, &q);
    char c;
    rep(i, 0, h) rep(j, 0, w - 1) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + 1), graf[i * w + j + 1].push_back(i * w + j), 0 : 0);
    rep(i, 0, h - 1) rep(j, 0, w) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + w), graf[i * w + j + w].push_back(i * w + j), 0 : 0);
    rep(i, 0, h) scanf("%d", rowCosts + i);
}

void dfsComponents(int v, int c) {
    component[v] = c;
    top[c] = min(top[c], v / w);
    bottom[c] = max(bottom[c], v / w);
    repIn(u, graf[v]) if (!component[u]) dfsComponents(u, c);
}
void calcComponents() {
    top[0] = bottom[0] = -1;
    rep(i, 0, h) rep(j, 0, w) if (!component[i * w + j]) {
        n++;
        top[n] = bottom[n] = i;
        dfsComponents(i * w + j, n);
        DC << "Component " << n << " from " << i << ' ' << j << "  ->  top " << top[n] << " bottom " << bottom[n] << eol;
    }
}

int lowestSon[maxn];
void calcLowestSon() {
    // TODO: 1 or 2
    priority_queue<pair<int, int>> Q;
    repD(i, h - 1, -1) {
        while (!Q.empty() && top[Q.top().second] > i) Q.pop();
        if (Q.empty()) {DC << i << " empty " << eol;}
        else DC << i << ' ' << Q.top().first << ' ' << Q.top().second << eol;
        if (!Q.empty()) rep(j, 0, w) {
            bool xd = false;
            if (Q.top().second == component[i * w + j]) Q.pop(), xd = true;
            if (!Q.empty()) DC << " at " << i << ' ' << j << " Q.top = " << Q.top().first << ' ' << Q.top().second << eol;
            if (!Q.empty() && bottom[lowestSon[component[i * w + j]]] < Q.top().first && bottom[component[i * w + j]] < Q.top().first) lowestSon[component[i * w + j]] = Q.top().second;
            if (xd) Q.push({bottom[component[i * w + j]], component[i * w + j]});
        }
        rep(j, 0, w) if ((j == 0 || component[i * w + j] != component[i * w + j - 1]) && bottom[component[i * w + j]] == i) Q.push({i, component[i * w + j]});
    }
    DEBUG rep(i, 1, n + 1) DC << "LowestSon[" << i << "] = " << lowestSon[i] << eol;
}
void calcJP() {
    rep(i, 1, n + 1) jp[i][0] = lowestSon[i];
    rep(k, 1, maxLog) rep(i, 1, n + 1) jp[i][k] = jp[jp[i][k - 1]][k - 1];
}

pair<int, int> jump(int v, int u) {
    DC << "jump " << v << ' ' << u << eol << ' ';
    int ans = 0;
    repD(k, maxLog - 1, -1) if (jp[v][k] && bottom[jp[v][k]] < top[u]) {
        v = jp[v][k], ans += 1 << k;
        DC << v << "(" << (1 << k) << ") ";
    }
    DC << eol;
    return {v, ans};
}

bool cmpBottom(int a, int b) {
    return bottom[a] < bottom[b];
}
int query(vector<int>& V) {
    int ans = 0;
    ranges::sort(V, cmpBottom);
    int last = -1;
    int v = V[0];
    rep(i, 1, V.size()) {
        const auto& u = V[i];
        DC << "Jumping from " << v << " to " << u << "  ;  last = " << last << eol;
        if (v == u) DC << " skip" << eol;
        if (v == u) continue;
        if (last >= top[u] && last <= bottom[u]) DC << " skip" << eol;
        if (last >= top[u] && last <= bottom[u]) continue;
        if (v == 0) return -1;
        if (bottom[v] >= top[u]) {
            ans++;
            if (bottom[u] >= bottom[v]) {
                last = bottom[v];
                v = lowestSon[v];
            }
            else {
                last = bottom[u];
                v = lowestSon[u];
            }
            DC << " zachodza" << eol;
            continue;
        }
        auto [vv, cost] = jump(v, u);
        v = lowestSon[vv], ans += cost;
        if (top[u] > bottom[v]) return -1;
        ans += 2;
        if (bottom[u] >= bottom[v]) {
            last = bottom[v];
            v = lowestSon[v];
        }
        else {
            last = bottom[u];
            v = lowestSon[u];
        }
    }
    return ans;
}

void processQueries() {
    int t, a, b;
    vector<int> V;
    rep(i, 0, q) {
        scanf("%d", &t);
        V.resize(t);
        rep(j, 0, t) scanf("%d%d", &a, &b), V[j] = component[(a - 1) * w + b - 1];
        printf("%d\n", query(V));
    }
}

int main() {
    input();
    calcComponents();
    calcLowestSon();
    calcJP();
    processQueries();
    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'void input()':
Main.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     scanf("%d%d%d", &h, &w, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:27:40: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     rep(i, 0, h) rep(j, 0, w - 1) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + 1), graf[i * w + j + 1].push_back(i * w + j), 0 : 0);
      |                                   ~~~~~^~~~~~~~~~~
Main.cpp:28:40: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     rep(i, 0, h - 1) rep(j, 0, w) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + w), graf[i * w + j + w].push_back(i * w + j), 0 : 0);
      |                                   ~~~~~^~~~~~~~~~~
Main.cpp:29:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |     rep(i, 0, h) scanf("%d", rowCosts + i);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void processQueries()':
Main.cpp:132:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         scanf("%d", &t);
      |         ~~~~~^~~~~~~~~~
Main.cpp:134:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         rep(j, 0, t) scanf("%d%d", &a, &b), V[j] = component[(a - 1) * w + b - 1];
      |                      ~~~~~^~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...