Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void dbg_out() { cerr << endl; }
- template<typename Head, typename... Tail>
- void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
- #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
- #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
- #define all(x) (x).begin(), (x).end()
- #define sz(x) (int) (x).size()
- #define int long long
- const int MXN = 3e5 + 5, INF = 1e9 + 5;
- struct edge {
- int v, w, s, e;
- edge(int v, int w, int s, int e) : v(v), w(w), s(s), e(e) {}
- };
- int N, Q;
- vector<edge> g[MXN];
- pair<int, int> timings[MXN]; // allowed range of starting times for which this road i is traversable
- void dfs(int u, int p, int dist) {
- for (const auto &[v, w, s, e] : g[u]) {
- if (v == p) continue;
- // Left side of range always increases, right side always decreases
- timings[v] = make_pair(max(s - dist, timings[u].first), min(e - dist, timings[u].second));
- dfs(v, u, dist + w);
- }
- }
- void solve() {
- cin >> N >> Q;
- for (int i = 0; i < N - 1; i++) {
- int u, v, w, s, e;
- cin >> u >> v >> w >> s >> e;
- g[u].emplace_back(v, w, s, e);
- g[v].emplace_back(u, w, s, e);
- }
- timings[0] = make_pair(0, INF);
- dfs(0, -1, 0);
- for (int i = 0; i < Q; i++) {
- int m, t;
- cin >> m >> t;
- cout << (timings[m].first <= t && t <= timings[m].second ? "YES" : "NO") << "\n";
- }
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- #ifdef local
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int TC = 1;
- // cin >> TC;
- while (TC--) solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement