Advertisement
Just-a-programmer

Beautiful Tree

Aug 14th, 2020
1,742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. const int mod=998244353;
  5. const int N=5*1e5+7;
  6. vector<int> beauty;
  7. vector<vector<int>> e;
  8. vector<int> a,seg,val;
  9. vector<int> in,out;
  10. int n,q,Size;
  11. ll x;
  12. /*----------------------------------------------Debug start------------------------------------------------------*/
  13. void __print(int x) {cerr << x;}
  14. void __print(long x) {cerr << x;}
  15. void __print(long long x) {cerr << x;}
  16. void __print(unsigned x) {cerr << x;}
  17. void __print(unsigned long x) {cerr << x;}
  18. void __print(unsigned long long x) {cerr << x;}
  19. void __print(float x) {cerr << x;}
  20. void __print(double x) {cerr << x;}
  21. void __print(long double x) {cerr << x;}
  22. void __print(char x) {cerr << '\'' << x << '\'';}
  23. void __print(const char *x) {cerr << '\"' << x << '\"';}
  24. void __print(const string &x) {cerr << '\"' << x << '\"';}
  25. void __print(bool x) {cerr << (x ? "true" : "false");}
  26.  
  27. template<typename T, typename V>
  28. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  29. template<typename T>
  30. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  31. void _print() {cerr << "]\n";}
  32. template <typename T, typename... V>
  33. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  34. #ifndef ONLINE_JUDGE
  35. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  36. #else
  37. #define debug(x...)
  38. #endif
  39. /*------------------------------------------------------Debug End------------------------------------------------------*/
  40. // Fucntion to calculate ith beautiful value
  41. void preprocess()
  42. {
  43.     queue<pair<int,ll>> q;
  44.     q.push(make_pair(0,0ll));
  45.     while((int)beauty.size()<N)
  46.     {
  47.         auto t=q.front(); q.pop();
  48.         beauty.emplace_back(t.first);
  49.         for(int i=0;i<10;i++)
  50.         {
  51.             if(i==0 && t.first==0) continue;
  52.             if(t.second+i*i<=x)
  53.                 q.push({(t.first*10+i)%mod,t.second+i*i});
  54.         }
  55.     }
  56. }
  57. int Time;
  58. //conduct a euler tour using dfs
  59. void dfs(int s,int p=-1)
  60. {
  61.     in[s]=Time++;
  62.     a.emplace_back(s);
  63.     for(int i:e[s])
  64.     {
  65.         if(i!=p)
  66.             dfs(i,s);
  67.     }
  68.     a.emplace_back(s);
  69.     out[s]=Time++;
  70. }
  71. void build()
  72. {
  73.     Size=1;
  74.     while(Size<(int)a.size()) Size*=2;
  75.     Size=2*Size-1;
  76.     seg=vector<int>(Size,0);
  77.     for(size_t i=0;i<a.size();i++)
  78.         seg[i+Size/2]=beauty[val[a[i]]];
  79.     for(int i=Size/2-1;i>=0;i--)
  80.         seg[i]=(seg[2*i+1]+seg[2*i+2])%mod;
  81. }
  82. void Set(int index,int val)
  83. {
  84.     index+=Size/2;
  85.     seg[index]=beauty[val];
  86.     int pos=(index-1)/2;
  87.     while(pos>=0)
  88.     {
  89.         seg[pos]=(seg[2*pos+1]+seg[2*pos+2])%mod;
  90.         if(pos==0)pos=-1;
  91.         else pos=(pos-1)/2;
  92.     }
  93.     debug(seg);
  94. }
  95. int get(int left,int right)
  96. {
  97.     int sum=0;
  98.     left+=Size/2,right+=Size/2;
  99.     while(left<right)
  100.     {
  101.         debug(left,right);
  102.         if(right&1) sum=(sum+seg[right--])%mod;
  103.         right=(right-1)/2;
  104.         if(!(left&1)) sum=(sum+seg[left++])%mod;
  105.         left=(left-1)/2;
  106.     }
  107.     if(left==right) sum=(sum+seg[left])%mod;
  108.     return sum;
  109. }
  110. int sum(int root)
  111. {
  112.     int val=get(in[root],out[root])/2;
  113.     cerr<<"here\n";
  114.     return val;
  115. }
  116. void update(int pos,int ne)
  117. {
  118.     Set(in[pos],ne);
  119.     Set(out[pos],ne);
  120. }
  121. int main()
  122. {
  123.     /*ios_base::sync_with_stdio(false);
  124.     cin.tie(0); cout.tie(0);*/
  125.     cin>>n>>q>>x;
  126.     e=vector<vector<int>>(n);
  127.     in.resize(n); out.resize(n);
  128.     val.resize(n);
  129.     preprocess();
  130.     for(int i=0;i<n-1;i++)
  131.     {
  132.         int u,v; cin>>u>>v;
  133.         e[u-1].emplace_back(v-1);
  134.         e[v-1].emplace_back(u-1);
  135.     }
  136.     for(int i=0;i<n;i++)
  137.         cin>>val[i];
  138.     Time=0;
  139.     dfs(0);
  140.     build();
  141.     debug(seg);
  142.     while(q--)
  143.     {
  144.         int type;
  145.         cin>>type;
  146.         if(type==1)
  147.         {
  148.             int pos,ne;
  149.             cin>>pos>>ne;
  150.             update(pos-1,ne);
  151.         }
  152.         else
  153.         {
  154.             int root=0;
  155.             cin>>root;
  156.             int val=sum(root-1);
  157.             debug(val);
  158.             cout<<val<<"\n";
  159.         }
  160.     }
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement