# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|
1199667 | | InvMOD | 버스 (JOI14_bus) | C++17 | | 136 ms | 13996 KiB |
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = numeric_limits<ll>::max() / 8;
struct Edge{
int u,v,x,y;
Edge(int u = 0, int v = 0, int x = 0, int y = 0): u(u), v(v), x(x), y(y) {}
bool operator < (const Edge& q) const{
return x > q.x;
}
// greedy x:
/*
if x[u[1]] > y[v[1]] -> break and wait for a better
if x[u[1]] < y[v[1]] -> we can sure that this is the optimal answer
because if x[u[2]] < y[v[1]] too, it is not optimal as taking x[u[1]] (u[1] has bigger x than u[2])
*/
};
struct Node{
int x, time, from;
Node(int x = 0, int time = 0, int from = 0): x(x), time(time), from(from) {}
bool operator < (const Node& q) const{
return time > q.time;
}
};
void Main()
{
int n,m; cin >> n >> m;
vector<Edge> E; vector<vi> adj(n + 1, vi());
for(int i = 0; i < m; i++){
int u,v,x,y; cin >> u >> v >> x >> y;
E.eb(u, v, x, y);
adj[u].eb(i);
}
vector<int> ptr(n + 1, 0);
for(int i = 1; i <= n; i++){
sort(all(adj[i]), [&](int x, int y){return E[x] < E[y];});
}
vector<pair<int,int>> best;
priority_queue<Node> pq;
for(int id : adj[1]){
int curMin = E[id].x, curMax = 2e9;
pq.push(Node(E[id].v, E[id].y, id));
while(sz(pq)){
Node e = pq.top(); pq.pop();
int x = e.x, ctime = e.time;
if(x != n){
while(ptr[x] < sz(adj[x])){
Edge ce = E[adj[x][ptr[x]]];
if(ctime <= ce.x){
pq.push(Node(ce.v, ce.y, adj[x][ptr[x]]));
++ptr[x];
}
else break;
}
}
else{
curMax = min(curMax, ctime);
}
}
best.eb(curMax, curMin);
}
sort(all(best)); // basic
for(int i = 1; i < sz(best); i++){
ckmx(best[i].se, best[i - 1].se);
}
int q; cin >> q;
while(q--){
int last; cin >> last;
int p = uprbound(best, pi(last, 2e9)) - 1;
if(p < 0){
cout << "-1\n";
}
else{
cout << best[p].se << el;
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
bus.cpp: In function 'int32_t main()':
bus.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bus.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |