Advertisement
Guest User

Balancing Tree USACO

a guest
Apr 25th, 2022
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <map>
  7. #include <unordered_map>
  8. #include <set>
  9. #include <cstring>
  10. #include <chrono>
  11. #include <cassert>
  12. #include <bitset>
  13. #include <stack>
  14. #include <queue>
  15. #include <iomanip>
  16. #include <random>
  17.  
  18. #ifdef _MSC_VER
  19. #  include <intrin.h>
  20. #  define __builtin_popcount __popcnt
  21. #  define __builtin_popcountll __popcnt64
  22. #endif
  23.  
  24. //#pragma GCC optimize("Ofast")
  25. //#pragma GCC optimize("Ofast,unroll-loops")
  26. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  27. #define x first
  28. #define y second
  29. #define ld long double
  30. #define ll long long
  31. #define ull unsigned long long
  32. #define us unsigned short
  33. #define lsb(x) ((x) & (-(x)))
  34. #define pii pair <int, int>
  35.  
  36. using namespace std;
  37.  
  38. const int MOD = 998244353;
  39.  
  40. int lgput(int n, int p, int mod = MOD) {
  41.     int ans = 1, x = n;
  42.  
  43.     while (p) {
  44.         if (p & 1)
  45.             ans = 1LL * ans * x % mod;
  46.         x = 1LL * x * x % mod;
  47.         p >>= 1;
  48.     }
  49.  
  50.     return ans;
  51. }
  52.  
  53. int T, b;
  54.  
  55. int n, x;
  56. int sol;
  57.  
  58. pair <ll, ll> v[100005], seg[100005];
  59. vector <int> g[100005];
  60. ll ans[100005], mx[100005];
  61.  
  62. void dfs(int nod, int d) {
  63.  
  64.     ll mx = max(0LL, v[nod].x - d), mn = min((ll)1e9, v[nod].y + d);
  65.  
  66.     for (auto& fiu : g[nod]) {
  67.         dfs(fiu, d);
  68.  
  69.         mx = max(mx, seg[fiu].x);
  70.         mn = min(mn, seg[fiu].y);
  71.     }
  72.  
  73.     seg[nod] = { mx, mn };
  74.  
  75.     //cout << nod << " : " << mx << ", " << mn << "\n";
  76. }
  77.  
  78. void dfs2(int nod, int tata = 0) {
  79.     ans[nod] = max(v[nod].x, seg[nod].x);
  80.     ans[nod] = max(ans[nod], mx[tata]);
  81.     mx[nod] = max(mx[tata], ans[nod] - sol);
  82.  
  83.     for (auto& fiu : g[nod]) {
  84.         dfs2(fiu, nod);
  85.     }
  86.  
  87.     //cout << nod << " : " << mx << ", " << mn << "\n";
  88. }
  89.  
  90. pii dfs3(int nod) {
  91.     int mn = (int)1e9, mx = 0;
  92.  
  93.     for (auto& fiu : g[nod]) {
  94.         pii x = dfs3(fiu);
  95.  
  96.         mn = min(mn, x.x);
  97.         mx = max(mx, x.y);
  98.     }
  99.  
  100.     assert(ans[nod] - mn <= sol && mx - ans[nod] <= sol);
  101.  
  102.     return { mn, mx };
  103. }
  104.  
  105. bool check(int val) {
  106.     dfs(1, val);
  107.  
  108.     for (int i = 1; i <= n; i++) {
  109.         if (seg[i].x > seg[i].y || seg[i].y < v[i].x || v[i].y < seg[i].x)
  110.             return 0;
  111.     }
  112.  
  113.     return 1;
  114. }
  115.  
  116. void solve(int t) {
  117.     cin >> n;
  118.  
  119.     for (int i = 1; i <= n; i++)
  120.         g[i].clear(), mx[i] = 0;
  121.  
  122.     for (int i = 1; i < n; i++) {
  123.         cin >> x;
  124.         g[x].push_back(i + 1);
  125.     }
  126.  
  127.     for (int i = 1; i <= n; i++) {
  128.         cin >> v[i].x >> v[i].y;
  129.     }
  130.  
  131.     int st = 0, dr = (int)1e9, mid;
  132.  
  133.     while (st <= dr) {
  134.         mid = (st + dr) >> 1;
  135.  
  136.         if (check(mid))
  137.             dr = mid - 1;
  138.         else
  139.             st = mid + 1;
  140.     }
  141.  
  142.     sol = st;
  143.  
  144.     cout << sol << "\n";
  145.  
  146.     if (b == 1) {
  147.         check(sol);
  148.  
  149.         dfs2(1);
  150.         for (int i = 1; i <= n; i++)
  151.             cout << ans[i] << " ";
  152.         cout << "\n";
  153.  
  154.         dfs3(1);
  155.     }
  156. }
  157.  
  158. int main() {
  159.     //ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  160.     srand(time(0));
  161.  
  162.     int T = 1;
  163.    
  164.     cin >> T >> b;
  165.  
  166.     for (int t = 1; t <= T; t++) {
  167.         solve(t);
  168.     }
  169.  
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement