fork download
  1. /* AUTHOR: TUAN ANH - BUI */
  2. // ~~ icebear ~~
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define int long long
  6. typedef long long ll;
  7. typedef pair<int, int> ii;
  8. typedef pair<int, ii> iii;
  9.  
  10. template<class X, class Y>
  11. bool minimize(X &x, const Y &y) {
  12. if (x > y) return x = y, true;
  13. return false;
  14. }
  15.  
  16. template<class X, class Y>
  17. bool maximize(X &x, const Y &y) {
  18. if (x < y) return x = y, true;
  19. return false;
  20. }
  21.  
  22. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  23. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  24. #define REP(i, n) for(int i=0; i<(n); ++i)
  25. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  26. #define MASK(i) (1LL << (i))
  27. #define BIT(S, i) (((S) >> (i)) & 1)
  28. #define mp make_pair
  29. #define pb push_back
  30. #define fi first
  31. #define se second
  32. #define all(x) x.begin(), x.end()
  33. #define task "icebear"
  34. /*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */
  35.  
  36. const int MOD = 1e9 + 7;
  37. const int inf = (int)1e9 + 27092008;
  38. const ll INF = (ll)1e18 + 27092008;
  39. const int N = 2e5 + 5;
  40. int n, a[N];
  41. vector<int> G[N], compress, nodes[N];
  42. int ans[N], ft[N], tin[N], tout[N], timer, P[N][20], h[N];
  43. vector<array<int, 3>> Q[N];
  44.  
  45. void update(int x, int val) {
  46. for(; x <= n; x += x & -x) ft[x] += val;
  47. }
  48.  
  49. int get(int x) {
  50. int ans = 0;
  51. for(; x; x -= x & -x) ans += ft[x];
  52. return ans;
  53. }
  54.  
  55. void dfs(int u, int par) {
  56. P[u][0] = par;
  57. FOR(j, 1, 19) P[u][j] = P[P[u][j - 1]][j - 1];
  58. tin[u] = ++timer;
  59. for(int v : G[u]) if (v != par) {
  60. h[v] = h[u] + 1;
  61. dfs(v, u);
  62. }
  63. tout[u] = timer;
  64. }
  65.  
  66. int LCA(int u, int v) {
  67. if (h[u] < h[v]) swap(u, v);
  68. int s = h[u] - h[v];
  69. RED(j, 20) if (BIT(s, j))
  70. u = P[u][j];
  71. if (u == v) return u;
  72. RED(j, 20) if (P[u][j] != P[v][j]) {
  73. u = P[u][j];
  74. v = P[v][j];
  75. }
  76. return P[u][0];
  77. }
  78.  
  79. int q;
  80. void init(void) {
  81. cin >> n >> q;
  82. FOR(i, 1, n) cin >> a[i], compress.pb(a[i]);
  83. FOR(i, 2, n) {
  84. int u, v;
  85. cin >> u >> v;
  86. G[u].pb(v);
  87. G[v].pb(u);
  88. }
  89. sort(all(compress));
  90. compress.resize(unique(all(compress)) - compress.begin());
  91. FOR(i, 1, n) a[i] = upper_bound(all(compress), a[i]) - compress.begin();
  92. FOR(i, 1, q) {
  93. int x, y, z;
  94. cin >> x >> y >> z;
  95. int p = upper_bound(all(compress), z) - compress.begin();
  96. if (p == 0 || compress[p - 1] != z) continue;
  97. Q[p].pb({x, y, i});
  98. }
  99.  
  100. FOR(i, 1, n) nodes[a[i]].pb(i);
  101. }
  102.  
  103. void process(void) {
  104. dfs(1, 0);
  105. FOR(i, 1, n) {
  106. for(int &x : nodes[i]) update(tin[x], +1), update(tout[x] + 1, -1);
  107. for(auto &x : Q[i]) {
  108. ans[x[2]] = get(tin[x[1]]) + get(tin[x[0]]) - 2 * get(tin[LCA(x[0], x[1])]) + (a[LCA(x[0], x[1])] == i);
  109. }
  110. for(int &x : nodes[i]) update(tin[x], -1), update(tout[x] + 1, +1);
  111. }
  112. FOR(i, 1, q) cout << (ans[i] > 0);
  113. }
  114.  
  115. signed main() {
  116. ios_base::sync_with_stdio(0);
  117. cin.tie(0); cout.tie(0);
  118. if (fopen(task".inp", "r")) {
  119. freopen(task".inp", "r", stdin);
  120. freopen(task".out", "w", stdout);
  121. }
  122. int tc = 1;
  123. // cin >> tc;
  124. while(tc--) {
  125. init();
  126. process();
  127. }
  128. return 0;
  129. }
  130.  
Success #stdin #stdout 0.01s 20452KB
stdin
Standard input is empty
stdout
Standard output is empty