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, q;
  41. vector<int> G[N];
  42. int chainHead[N], idChain[N], pos[N], tour[N], sz[N], h[N], cur_chain = 1, cur_pos = 0, par[N];
  43.  
  44. struct Node {
  45. int best, sum, pref, suff, lazy;
  46. friend Node combine(const Node &L, const Node &R) {
  47. Node res;
  48. res.best = max({L.best, R.best, L.suff + R.pref});
  49. res.pref = max(L.pref, L.sum + R.pref);
  50. res.suff = max(R.suff, R.sum + L.suff);
  51. res.sum = L.sum + R.sum;
  52. res.lazy = inf;
  53. return res;
  54. }
  55. } node[N << 2];
  56.  
  57. void Assign(int id, int l, int r, int k) { // gan k cho node[id]
  58. node[id].sum = (r - l + 1) * k;
  59. node[id].best = node[id].pref = node[id].suff = max(0LL, node[id].sum); // neu k > 0 thi lay toan bo la toi uu, con k < 0 thi ko lay gi la toi uu
  60. node[id].lazy = k;
  61. }
  62.  
  63. void pushDown(int id, int l, int r) {
  64. if (node[id].lazy != inf) {
  65. int mid = (l + r) >> 1;
  66. Assign(id << 1, l, mid, node[id].lazy);
  67. Assign(id << 1 | 1, mid + 1, r, node[id].lazy);
  68. node[id].lazy = inf;
  69. }
  70. }
  71.  
  72. void update(int id, int l, int r, int u, int v, int k) {
  73. if (l > v || r < u || u > v) return;
  74. if (u <= l && r <= v) {
  75. Assign(id, l, r, k);
  76. return;
  77. }
  78. pushDown(id, l, r);
  79. int mid = (l + r) >> 1;
  80. update(id << 1, l, mid, u, v, k);
  81. update(id << 1 | 1, mid + 1, r, u, v, k);
  82. node[id] = combine(node[id << 1], node[id << 1 | 1]);
  83. }
  84.  
  85. Node get(int id, int l, int r, int u, int v) {
  86. if (l > v || r < u || u > v) return {-INF, 0, 0, 0, inf};
  87. if (u <= l && r <= v) return node[id];
  88. pushDown(id, l, r);
  89. int mid = (l + r) >> 1;
  90. return combine(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
  91. }
  92.  
  93. void dfs(int u) {
  94. sz[u] = 1;
  95. for(int v : G[u]) if (v != par[u]) {
  96. par[v] = u;
  97. h[v] = h[u] + 1;
  98. dfs(v);
  99. sz[u] += sz[v];
  100. }
  101. }
  102.  
  103. void hld(int u) {
  104. if (chainHead[cur_chain] == 0) {
  105. chainHead[cur_chain] = u;
  106. }
  107. idChain[u] = cur_chain;
  108. pos[u] = ++cur_pos;
  109. tour[cur_pos] = u;
  110.  
  111. int hvy = 0;
  112. for(int v : G[u]) if (v != par[u] && sz[v] > sz[hvy])
  113. hvy = v;
  114.  
  115. if (hvy > 0) hld(hvy);
  116.  
  117. for(int v : G[u]) if (v != par[u] && v != hvy) {
  118. cur_chain++;
  119. hld(v);
  120. }
  121. }
  122.  
  123. int LCA(int u, int v) {
  124. while(idChain[u] != idChain[v]) {
  125. if (idChain[u] < idChain[v]) swap(u, v);
  126. u = par[chainHead[idChain[u]]];
  127. }
  128. return (h[u] < h[v] ? u : v);
  129. }
  130.  
  131. void updatePath(int u, int v, int c) {
  132. int p = LCA(u, v);
  133. while(idChain[u] != idChain[p]) {
  134. update(1, 1, n, pos[chainHead[idChain[u]]], pos[u], c);
  135. u = par[chainHead[idChain[u]]];
  136. }
  137. while(idChain[v] != idChain[p]) {
  138. update(1, 1, n, pos[chainHead[idChain[v]]], pos[v], c);
  139. v = par[chainHead[idChain[v]]];
  140. }
  141. if (h[u] > h[v]) swap(u, v);
  142. update(1, 1, n, pos[u], pos[v], c);
  143. }
  144.  
  145. int getPath(int u, int v) {
  146. Node L = {-INF, 0, 0, 0, inf};
  147. Node R = L;
  148. int p = LCA(u, v);
  149. while(idChain[u] != idChain[p]) {
  150. Node cur = get(1, 1, n, pos[chainHead[idChain[u]]], pos[u]);
  151. swap(cur.pref, cur.suff);
  152. L = combine(L, cur);
  153. u = par[chainHead[idChain[u]]];
  154. }
  155. if (u != p) {
  156. Node cur = get(1, 1, n, pos[p] + 1, pos[u]);
  157. swap(cur.pref, cur.suff);
  158. L = combine(L, cur);
  159. }
  160. while(idChain[v] != idChain[p]) {
  161. R = combine(get(1, 1, n, pos[chainHead[idChain[v]]], pos[v]), R);
  162. v = par[chainHead[idChain[v]]];
  163. }
  164. if (v != p) {
  165. R = combine(get(1, 1, n, pos[p] + 1, pos[v]), R);
  166. }
  167. Node mid = get(1, 1, n, pos[p], pos[p]);
  168. return combine(L, combine(mid, R)).best;
  169. }
  170.  
  171. int a[N];
  172. void build(int id, int l, int r) {
  173. node[id].lazy = inf;
  174. if (l == r) {
  175. node[id].sum = a[tour[l]];
  176. node[id].best = node[id].pref = node[id].suff = max(0LL, node[id].sum);
  177. return;
  178. }
  179. int mid = (l + r) >> 1;
  180. build(id << 1, l, mid);
  181. build(id << 1 | 1, mid + 1, r);
  182. node[id] = combine(node[id << 1], node[id << 1 | 1]);
  183. }
  184.  
  185. void init(void) {
  186. cin >> n;
  187. FOR(i, 1, n) cin >> a[i];
  188. FOR(i, 2, n) {
  189. int u, v;
  190. cin >> u >> v;
  191. G[u].pb(v);
  192. G[v].pb(u);
  193. }
  194. }
  195.  
  196. void process(void) {
  197. dfs(1);
  198. hld(1);
  199. build(1, 1, n);
  200. cin >> q;
  201. while(q--) {
  202. int t; cin >> t;
  203. int u, v; cin >> u >> v;
  204. if (t == 2) {
  205. int c; cin >> c;
  206. updatePath(u, v, c);
  207. } else cout << getPath(u, v) << '\n';
  208. }
  209. }
  210.  
  211. signed main() {
  212. ios_base::sync_with_stdio(0);
  213. cin.tie(0); cout.tie(0);
  214. if (fopen(task".inp", "r")) {
  215. freopen(task".inp", "r", stdin);
  216. freopen(task".out", "w", stdout);
  217. }
  218. int tc = 1;
  219. // cin >> tc;
  220. while(tc--) {
  221. init();
  222. process();
  223. }
  224. return 0;
  225. }
  226.  
  227.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Standard output is empty