제출 #1120322

#제출 시각아이디문제언어결과실행 시간메모리
1120322vjudge1Event Hopping (BOI22_events)C++17
0 / 100
396 ms12536 KiB
#include <bits/stdc++.h> using namespace std; int n, q; int s[100100]; int e[100100]; int p[100100]; int par[100100]; int nxt[100100][20]; int cmp(int i, int j) { return e[i] < e[j]; } int get_par(int v) { if(par[v] == v) { return v; } return par[v] = get_par(par[v]); } int calc(int x, int y) { int ans = 0; for(int i = 17; i >= 0; i--) { int v = nxt[x][i]; if(v > 0 && e[v] < e[y]) { ans += 1<<i; x = v; } } if(e[x] >= s[y]) { return ans; } else { return -1; } } int go(int x, int k) { for(int i = 17; i >= 0; i--) { int v = nxt[x][i]; if(k >= (1<<i)) { k -= 1<<i; if(v == -1) return -1; x = v; } } return x; } int can(int x, int y, int k) { int v = get_par(x); int cnt = 0; int c = nxt[v][0]; if(v == x || s[c] <= e[x]) { cnt++; x = c; } if(k - cnt - 1 <= 0) { return 0; } int t = go(x, k - cnt - 1); if(t == -1) { return 0; } if(e[t] >= s[y]) return 1; return 0; } void solve() { cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> s[i] >> e[i]; p[i] = i; } sort(p+1, p + n + 1, cmp); vector<int> st; int pr = -1; for(int i = 1; i <= n; i++) { int j = p[i]; while(st.size() > 1 && s[j] <= e[st[st.size()-2]]) { par[st.back()] = j; st.pop_back(); } par[j] = j; st.push_back(j); } for(int i = 0; i < st.size(); i++) { if(i + 1 == st.size()) { nxt[st[i]][0] = -1; } else { if(e[st[i]] >= s[st[i+1]]) { nxt[st[i]][0] = st[i + 1]; } else { nxt[st[i]][0] = -1; } } } for(int i = 1; i <= n; i++) { if(par[i] != i) { nxt[i][0] = get_par(i); } } for(int k = 1; k < 18; k++) { for(int i = 1; i <= n; i++) { int v = nxt[i][k-1]; if(v != -1) { nxt[i][k] = nxt[v][k-1]; } else { nxt[i][k] = -1; } } } for(int i = 1; i <= q; i++) { int x, y; cin >> x >> y; if(e[y] < e[x]) { cout << "impossible\n"; } else if(e[x] >= s[y]) { cout << 0 << "\n"; } else { int k = calc(get_par(x), get_par(y)); if(k == -1) { cout << "impossible\n"; } else { int k = calc(get_par(x), get_par(y)); if(k == -1) { cout << "impossible\n"; } else { if(can(x, y, k)) { cout << k << "\n"; } else if(can(x, y, k+1)) { cout << k + 1 << "\n"; } else { cout << k + 2 << "\n"; } } } } } } int main() { solve(); }

컴파일 시 표준 에러 (stderr) 메시지

events.cpp: In function 'void solve()':
events.cpp:80:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for(int i = 0; i < st.size(); i++) {
      |                    ~~^~~~~~~~~~~
events.cpp:81:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         if(i + 1 == st.size()) {
      |            ~~~~~~^~~~~~~~~~~~
events.cpp:70:9: warning: unused variable 'pr' [-Wunused-variable]
   70 |     int pr = -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...