# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
527714 |
2022-02-18T05:03:21 Z |
8e7 |
Railway Trip 2 (JOI22_ho_t4) |
C++17 |
|
2000 ms |
24928 KB |
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
pii ed[maxn];
int rig[maxn], lef[maxn];
vector<pii> el[maxn], er[maxn];
struct segtree{
int type = 0;
pii seg[4*maxn];
void pull(int cur) {
if (type) seg[cur] = max(seg[cur*2], seg[cur*2+1]);
else seg[cur] = min(seg[cur*2], seg[cur*2+1]);
}
void init(int cur, int l, int r){
if (r <= l) return;
if (r - l == 1) {
seg[cur] = {(type ? 0 : inf), 0};
return;
}
int m = (l + r) / 2;
init(cur*2, l, m), init(cur*2+1, m, r);
pull(cur);
}
void modify(int cur, int l, int r, int ind, int val) {
if (r <= l) return;
if (r - l == 1) {
seg[cur] = {val, l};
return;
}
int m = (l + r) / 2;
if (ind < m) modify(cur*2, l, m, ind, val);
else modify(cur*2+1, m, r, ind, val);
pull(cur);
}
pii query(int cur, int l, int r, int ql, int qr) {
if (r <= l || ql >= r || qr <= l) return type ? make_pair(0, 0) : make_pair(inf, 0);
if (ql <= l && qr >= r) return seg[cur];
int m = (l + r) / 2;
pii le = query(cur*2, l, m, ql, qr), ri = query(cur*2+1, m, r, ql, qr);
return type ? max(le, ri) : min(le, ri);
}
} seg;
int tol[17][maxn], tor[17][maxn];
vector<int> adj[maxn];
int dis[maxn];
int main() {
io
int n, k, m;
cin >> n >> k >> m;
for (int i = 0;i < m;i++) {
cin >> ed[i].ff >> ed[i].ss;
if (ed[i].ss > ed[i].ff) {
er[min(ed[i].ss-1, ed[i].ff + k - 1)].push_back({ed[i].ss, 1});
er[ed[i].ff - 1].push_back({ed[i].ss, -1});
} else {
el[max(ed[i].ss+1, ed[i].ff - k + 1)].push_back({ed[i].ss, 1});
el[ed[i].ff + 1].push_back({ed[i].ss, -1});
}
}
seg.init(1, 1, n+1);
multiset<int> se;
for (int i = 1;i <= n;i++) {
for (auto p:el[i]) {
if (p.ss == 1) se.insert(p.ff);
else se.erase(se.find(p.ff));
}
lef[i] = (se.size() ? *se.begin() : i);
pii nxt = seg.query(1, 1, n+1, lef[i], i);
if (nxt.ss == 0) tol[0][i] = i;
else tol[0][i] = nxt.ss;
seg.modify(1, 1, n+1, i, lef[i]);
}
seg.type = 1;
seg.init(1, 1, n+1);
se.clear();
for (int i = n;i >= 1;i--) {
for (auto p:er[i]) {
if (p.ss == 1) se.insert(p.ff);
else se.erase(se.find(p.ff));
}
rig[i] = (se.size() ? *prev(se.end()) : i);
pii nxt = seg.query(1, 1, n+1, i+1, rig[i]+1);
if (nxt.ss == 0) tor[0][i] = i;
else tor[0][i] = nxt.ss;
seg.modify(1, 1, n+1, i, rig[i]);
}
pary(lef + 1, lef + n + 1);
pary(rig + 1, rig + n + 1);
//brute force
for (int i = 1;i <= n;i++) {
adj[i].push_back(tol[0][i]);
adj[i].push_back(tor[0][i]);
}
int q;
cin >> q;
while (q--) {
int s, t;
cin >> s >> t;
for (int i = 1;i <= n;i++) dis[i] = inf;
dis[s] = 0;
queue<int> que;
que.push(s);
int ans = inf;
while (que.size()) {
int cur = que.front();que.pop();
if ((lef[cur] <= t && t <= cur) || (rig[cur] >= t && t > cur)) {
ans = dis[cur];
break;
}
for (int v:adj[cur]) {
if (dis[v] > dis[cur] + 1) {
dis[v] = dis[cur] + 1;
que.push(v);
}
}
}
cout << (ans == inf ? -1 : ans+1) << "\n";
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:14:19: warning: statement has no effect [-Wunused-value]
14 | #define pary(...) 0
| ^
Main.cpp:110:2: note: in expansion of macro 'pary'
110 | pary(lef + 1, lef + n + 1);
| ^~~~
Main.cpp:14:19: warning: statement has no effect [-Wunused-value]
14 | #define pary(...) 0
| ^
Main.cpp:111:2: note: in expansion of macro 'pary'
111 | pary(rig + 1, rig + n + 1);
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
18096 KB |
Output is correct |
2 |
Correct |
108 ms |
17976 KB |
Output is correct |
3 |
Correct |
114 ms |
18256 KB |
Output is correct |
4 |
Correct |
94 ms |
17892 KB |
Output is correct |
5 |
Runtime error |
47 ms |
24928 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2091 ms |
19348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
43 ms |
23480 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |