Submission #1279823

#TimeUsernameProblemLanguageResultExecution timeMemory
1279823icebearPassport (JOI23_passport)C++20
48 / 100
1117 ms413060 KiB
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template<class T>
    bool minimize(T &a, const T &b) {
        if (a > b) return a = b, true;
        return false;
    }

template<class T>
    bool maximize(T &a, const T &b) {
        if (a < b) return a = b, true;
        return false;
    }

#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "gen"
/*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */

const int MOD = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18 + 27092008;
const int N = 2500 + 5;
int n, dist[N*N], l[N], r[N];
int mx[N][N], mn[N][N];
vector<ii> G[N*N];
int id(int i, int j) {
    return (i - 1) * n + j;
}

void buildGraph() {
    FOR(i, 1, n) FOR(j, i, n) {
        G[id(l[i], r[i])].emplace_back(id(i, j), 1);
        G[id(l[j], r[j])].emplace_back(id(i, j), 1);
        if (i - 1 > 0) G[id(i,j)].emplace_back(id(i-1, j), 0);
        if (j + 1 <= n) G[id(i, j)].emplace_back(id(i, j+1), 0);
        G[id(mn[i][j], j)].emplace_back(id(i, j), 1);
        G[id(i, mx[i][j])].emplace_back(id(i, j), 1);
//            if (i - 1 > 0) cerr << "EDGE : " << i << ' ' << j << " -> " << i - 1 << ' ' << j << " - W = 0\n";
//            if (j + 1 <= n) cerr << "EDGE : " << i << ' ' << j << " -> " << i << ' ' << j + 1 << " - W = 0\n";
//            cerr << "EDGE : " << i << ' ' << j << " -> " << mn[i][j] << ' ' << j << " - W = 1\n";
//            cerr << "EDGE : " << i << ' ' << j << " -> " << i << ' ' << mx[i][j] << " - W = 1\n";
    }
}

void dijkstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[id(1,n)] = 0;
    priority_queue<ii, vector<ii>, greater<ii>> Q;
    Q.push({0, id(1,n)});
    while(!Q.empty()) {
        auto T = Q.top(); Q.pop();
        if (T.fi != dist[T.se]) continue;
        for(ii x : G[T.se]) {
            if (minimize(dist[x.fi], T.fi + x.se))
                Q.push(mp(dist[x.fi], x.fi));
        }
    }
}

void init(void) {
    cin >> n;
    FOR(i, 1, n) cin >> l[i] >> r[i];
}

void process(void) {
    memset(mn, 0x3f, sizeof mn);
    FOR(i, 1, n) FOR(j, i, n) {
      mx[i][j] = max(mx[i][j - 1], r[j]);
      mn[i][j] = min(mn[i][j - 1], l[j]);
    }

    buildGraph();
    dijkstra();

    int q;
    cin >> q;
    while(q--) {
        int x; cin >> x;
        int ans = dist[id(l[x],r[x])];
        cout << (ans == inf ? -1 : ans+1) << '\n';
        cerr << (ans == inf ? -1 : ans+1) << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int tc = 1;
//    cin >> tc;
    while(tc--) {
        init();
        process();
    }
    return 0;
}

Compilation message (stderr)

passport.cpp: In function 'int main()':
passport.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...