ash_sinha

620E codeorces

Jun 29th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. using namespace __gnu_pbds;
  4. using namespace std;
  5.  
  6. #define ff              first
  7. #define ss              second
  8. #define int             long long
  9. #define pb              push_back
  10. #define mp              make_pair
  11. #define pii             pair<int,int>
  12. #define vi              vector<int>
  13. #define mii             map<int,int>
  14. #define pqb             priority_queue<int>
  15. #define pqs             priority_queue<int,vi,greater<int> >
  16. #define setbits(x)      __builtin_popcountll(x)
  17. #define zrobits(x)      __builtin_ctzll(x)
  18. #define mod             1000000007
  19. #define inf             1e18
  20. #define ps(x,y)         fixed<<setprecision(y)<<x
  21. #define mk(arr,n,type)  type *arr=new type[n];
  22. #define w(x)            int x; cin>>x; while(x--)
  23. //mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
  24.  
  25. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
  26. vi c, tin, tout, t, lazy, vis, p2;  //p2 is used for storing powers of 2
  27. vector<vi> g;
  28. int timr = 0;
  29. /*void c_p_c()
  30. {
  31.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  32. #ifndef ONLINE_JUDGE
  33.     freopen("input.txt", "r", stdin);
  34.     freopen("output.txt", "w", stdout);
  35. #endif
  36. }*/
  37. // In tin vector entry time is stored for all the vector
  38. // In tout exit time is stoed
  39. // In vis vector vis[i] denotes the node visited at ith time
  40. void dfs(int cur, int par)
  41. {
  42.     tin[cur] = ++timr;
  43.     vis[timr] = cur;
  44.     //  for (auto x : g[cur])
  45.     for (int x = 0; x < g[cur].size(); x++)
  46.     {
  47.         if (g[cur][x] != par)
  48.         {
  49.             dfs(g[cur][x], cur);
  50.         }
  51.     }
  52.     tout[cur] = timr;
  53. }
  54. int expo(int a, int b)
  55. {
  56.     int res = 1;
  57.     while (b > 0)
  58.     {
  59.         if (b & 1)
  60.             res = res * a;
  61.         a = a * a;
  62.         b = b >> 1;
  63.     }
  64.     return res;
  65. }
  66. void build(int s, int e, int n)
  67. {
  68.     if (s == e)
  69.     {
  70.         t[n] = p2[c[vis[s]]];
  71.         return;
  72.     }
  73.     int mid = (s + e) / 2;
  74.     build(s, mid, 2 * n);
  75.     build(mid + 1, e, 2 * n + 1);
  76.     t[n] = (t[2 * n] | t[2 * n + 1]);
  77. }
  78. void update_range(int s, int e, int l, int r, int n, int val)
  79. {
  80.     if (lazy[n] != 0)
  81.     {
  82.         t[n] = lazy[n];
  83.         if (s != e)
  84.         {
  85.             lazy[2 * n] = lazy[n];
  86.             lazy[2 * n + 1] = lazy[n];
  87.         }
  88.         lazy[n] = 0;
  89.     }
  90.  
  91.     if (l > e || s > r)
  92.         return ;
  93.     if (s >= l && r >= e)
  94.     {
  95.         t[n] = p2[val];
  96.         if (s != e)
  97.         {
  98.             lazy[2 * n] = t[n];
  99.             lazy[2 * n + 1] = t[n];
  100.         }
  101.         return ;
  102.     }
  103.     int mid = (s + e) / 2;
  104.     update_range(s, mid, l, r, 2 * n, val);
  105.     update_range(mid + 1, e, l, r, 2 * n + 1, val);
  106.     t[n] = t[2 * n] | t[2 * n + 1];
  107.  
  108. }
  109. void query(int s, int e, int l, int r, int n, int &ans)
  110. {
  111.     if (lazy[n] != 0)
  112.     {
  113.         t[n] = lazy[n];
  114.         if (s != e)
  115.         {
  116.             lazy[2 * n] = lazy[n];
  117.             lazy[2 * n + 1] = lazy[n];
  118.         }
  119.         lazy[n] = 0;
  120.     }
  121.     if (l > e || s > r)
  122.         return ;
  123.     if (s >= l && r >= e)
  124.     {
  125.         ans = ans | t[n];
  126.         return;
  127.     }
  128.     int mid = (s + e) / 2;
  129.     query(s, mid, l, r, 2 * n, ans);
  130.     query(mid + 1, e, l, r, 2 * n + 1, ans);
  131.  
  132. }
  133. signed main()
  134. {
  135.     //c_p_c();
  136.     int n, m, i, a, b;
  137.     p2.assign(61, 1);
  138.     for (int i = 1; i <= 60; i++)
  139.         p2[i] = 2 * p2[i - 1];
  140.     cin >> n >> m;
  141.     c.resize(n + 1);
  142.     g.resize(n + 1); tin.resize(n + 10); tout.resize(n + 10); t.assign(4 * n + 10, 0); lazy.assign(4 * n  + 10, 0); vis.resize(n + 10);
  143.     for (i = 1; i <= n; i++)
  144.         cin >> c[i];
  145.     for (i = 1; i <= n - 1; i++)
  146.     {
  147.         cin >> a >> b;
  148.         g[a].pb(b);
  149.         g[b].pb(a);
  150.     }
  151.     dfs(1, 0);
  152.     build(1, n, 1);
  153.     //  for (i = 1; i <= 2 * n; i++)
  154.     //      cout << i << " " << t[i] <<"  "<< setbits(t[i])<<endl;
  155.  
  156.     while (m--)
  157.     {
  158.         int ch;
  159.         cin >> ch;
  160.         if (ch == 1)
  161.         {
  162.             int v, f;
  163.             cin >> v >> f;
  164.             //  cout << tin[v] << " " << tout[v] << " #" << endl;
  165.             update_range(1, n, tin[v], tout[v], 1, f);
  166.             //  for (i = 1; i <= 2 * n; i++)
  167.             //      cout << i << " " << t[i] << endl;
  168.         }
  169.         else
  170.         {
  171.             int v, ans = 0;
  172.             cin >> v;
  173.             query(1, n, tin[v], tout[v], 1, ans);
  174.             cout << setbits(ans)  << endl;
  175.         }
  176.     }
  177.  
  178. }
Add Comment
Please, Sign In to add comment