提交时间:2026-02-09 20:16:50

运行 ID: 39944

#pragma GCC optimize("Ofast,no-stack-protector,inline,fast-math,unroll-loops,omit-frame-pointer") #define NDEBUG #include <algorithm> #include <cctype> #include <cstdio> #include <utility> #include <vector> using namespace std; using i64 = long long; constexpr int INF = 0x3f3f3f3f; struct IO { static constexpr int MAX_LEN = 1 << 20; char buf[MAX_LEN], *p1, *p2; char pbuf[MAX_LEN], *pp; IO() : p1(buf) , p2(buf) , pp(pbuf) { } ~IO() { fwrite_unlocked(pbuf, 1, pp - pbuf, stdout); } inline char getch() { if (p1 == p2) p2 = (p1 = buf) + fread_unlocked(buf, 1, MAX_LEN, stdin); return p1 == p2 ? ' ' : *p1++; } IO& read(int& x) { int sgn = 1; char ch = getch(); x = 0; for (; !isdigit(ch); ch = getch()) ; for (; isdigit(ch); ch = getch()) x = x * 10 + (ch - '0'); x *= sgn; return *this; } inline void push(const char& c) { if (pp - pbuf == MAX_LEN) { fwrite_unlocked(pbuf, 1, MAX_LEN, stdout); pp = pbuf; } *pp++ = c; } IO& write(int x) { if (x < 0) { x = -x; push('-'); } else if (x == 0) { push('0'); return *this; } int top = 0, ch[40]; do { ch[top++] = x % 10; x /= 10; } while (x > 0); while (top > 0) push('0' + ch[--top]); return *this; } inline IO& write(int x, char lst) { write(x); push(lst); return *this; } } io; vector<vector<pair<int, int>>> idx; vector<vector<int>> tree; vector<int> a; void prepare(int u, int fa = -1) { auto& id = idx[u]; for (auto v : tree[u]) { id.emplace_back(v, a[v]); if (v != fa) prepare(v, u); } } inline int query(int u, int l, int r) { const auto& id = idx[u]; return lower_bound(id.begin(), id.end(), make_pair(l, -INF))->second - lower_bound(id.begin(), id.end(), make_pair(r, INF))->second; } int main() { int n, q; io.read(n); io.read(q); a.resize(n + 1); for (int i = 1; i <= n; ++i) io.read(a[i]); tree.resize(n + 1); for (int i = 1; i < n; ++i) { int u, v; io.read(u); io.read(v); tree[u].push_back(v); tree[v].push_back(u); } for (int i = 1; i <= n; ++i) sort(tree[i].begin(), tree[i].end()); idx.resize(n + 1); prepare(1); for (int i = 1; i <= n; ++i) { idx[i].emplace_back(n + 1, 0); for (int j = idx[i].size() - 3; j >= 0; --j) idx[i][j].second += idx[i][j + 1].second; } while (q-- > 0) { int u, l, r; io.read(u); io.read(l); io.read(r); io.write(query(u, l, r), '\n'); } return 0; }