Advertisement
arujbansal

trucks

Jan 22nd, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void dbg_out() { cerr << endl; }
  6. template<typename Head, typename... Tail>
  7. void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  8. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  9.  
  10. #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
  11. #define all(x) (x).begin(), (x).end()
  12. #define sz(x) (int) (x).size()
  13. #define int long long
  14.  
  15. const int MXN = 3e5 + 5, INF = 1e9 + 5;
  16.  
  17. struct edge {
  18.     int v, w, s, e;
  19.  
  20.     edge(int v, int w, int s, int e) : v(v), w(w), s(s), e(e) {}
  21. };
  22.  
  23. int N, Q;
  24. vector<edge> g[MXN];
  25. pair<int, int> timings[MXN]; // allowed range of starting times for which this road i is traversable
  26.  
  27. void dfs(int u, int p, int dist) {
  28.     for (const auto &[v, w, s, e] : g[u]) {
  29.         if (v == p) continue;
  30.  
  31.         // Left side of range always increases, right side always decreases
  32.         timings[v] = make_pair(max(s - dist, timings[u].first), min(e - dist, timings[u].second));
  33.         dfs(v, u, dist + w);
  34.     }
  35. }
  36.  
  37. void solve() {
  38.     cin >> N >> Q;
  39.  
  40.     for (int i = 0; i < N - 1; i++) {
  41.         int u, v, w, s, e;
  42.         cin >> u >> v >> w >> s >> e;
  43.  
  44.         g[u].emplace_back(v, w, s, e);
  45.         g[v].emplace_back(u, w, s, e);
  46.     }
  47.  
  48.     timings[0] = make_pair(0, INF);
  49.     dfs(0, -1, 0);
  50.  
  51.     for (int i = 0; i < Q; i++) {
  52.         int m, t;
  53.         cin >> m >> t;
  54.  
  55.         cout << (timings[m].first <= t && t <= timings[m].second ? "YES" : "NO") << "\n";
  56.     }
  57. }
  58.  
  59. signed main() {
  60.     ios_base::sync_with_stdio(false);
  61.     cin.tie(nullptr);
  62.  
  63. #ifdef local
  64.     freopen("input.txt", "r", stdin);
  65.     freopen("output.txt", "w", stdout);
  66. #endif
  67.  
  68.     int TC = 1;
  69.     // cin >> TC;
  70.     while (TC--) solve();
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement