This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
const int N = 1e5 + 5;
const int M = 3e5 + 5;
const int inf = 1e9 + 7;
using namespace std;
vector <int> mv, adj[N], Min[N];
int n, m, q, num, a[M], b[M], x[M], y[M], dp[M];
bool cmp(int i, int j){
if (y[i] > y[j]) return false;
if (y[i] == y[j] && x[i] > x[j]) return false;
return true;
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> x[i] >> y[i];
mv.push_back(i);
adj[b[i]].push_back(i);
}
sort(mv.begin(), mv.end(), cmp);
for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end(), cmp);
for (int ed = 0; ed < m; ed++){
int i = mv[ed], u = a[i], v = b[i];
if (u == 1) dp[ed] = x[i];
else if (adj[u].size() == 0) dp[ed] = -inf;
else{
int l = 0, r = adj[u].size()-1;
while (l != r){
int mid = (l + r + 1) / 2;
if (y[adj[u][mid]] <= x[i]) l = mid;
else r = mid-1;
}
if (y[adj[u][l]] > x[i]) dp[ed] = -inf;
else dp[ed] = Min[u][l];
}
if (Min[v].size() == 0) Min[v].push_back(dp[ed]);
else Min[v].push_back(max(dp[ed], *Min[v].rbegin()));
}
cin >> q;
while (q--){
cin >> num;
if (adj[n].size() == 0) cout << "-1\n";
else{
int l = 0, r = adj[n].size()-1;
while (l != r){
int mid = (l + r + 1)/ 2;
if (y[adj[n][mid]] <= num) l = mid;
else r = mid-1;
}
if (y[adj[n][l]] <= num && Min[n][l] != -inf) cout << Min[n][l] << "\n";
else cout << "-1\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |