Advertisement
cjchirag7

Beautiful Tree

Aug 17th, 2020 (edited)
1,985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3.  
  4. using namespace std;
  5.  
  6. const int MAX=500000;
  7. const int MOD=998244353;
  8.  
  9. vector<int> beautiful;
  10. set<int> st;
  11. int cnt;
  12. int n,m,x;
  13.  
  14. vector<vector<int> > graph;  // adjacency list of graph
  15. vector<bool> vis;  // visited array
  16. vector<int> in;  // discover times
  17. vector<int> out;  // finish times
  18. vector<int> nodes; // nodes in order of their discovery time
  19. int tot;  // total nodes visited yet
  20.  
  21. void dfs(int i)
  22. {
  23.    vis[i]=true;
  24.    nodes.push_back(i);
  25.    in[i]=tot++;
  26.    for(auto it: graph[i])
  27.     {
  28.         if(!vis[it])
  29.             dfs(it);
  30.     }
  31.    out[i]=tot-1;
  32. }
  33.  
  34.  class BIT
  35. {
  36.     // 1-based indexing    
  37.     vector<int> bit;
  38.     int n;
  39.     public:
  40.     BIT(int sz)
  41.     {
  42.         n=sz;
  43.         bit.assign(n+1,0);
  44.     }
  45.     void update(int x, int del)
  46.     {
  47.         // queries are 1-based
  48.         while(x<=n)
  49.         {
  50.             bit[x]=(((bit[x]+(del%MOD))%MOD)+MOD)%MOD;
  51.             x+=x&(-x);
  52.         }
  53.     }
  54.     int query(int x)
  55.     {
  56.         int ans=0;
  57.         while(x>0)
  58.         {
  59.             ans=(ans+bit[x])%MOD;
  60.             ans=(ans+MOD)%MOD;
  61.             x-=x&(-x);
  62.         }
  63.         return ans;
  64.     }    
  65.     int query(int l, int r)
  66.     {
  67.         return (((query(r)-query(l-1))%MOD)+MOD)%MOD;
  68.     }
  69. };
  70.  
  71. int32_t main()
  72. {
  73.   cin>>n>>m>>x;
  74.   cnt=0;
  75.   queue<pair<int,int> > q;
  76.  
  77.   for(int i=1; i<=9; i++)
  78.   {
  79.     if(i*i<=x)
  80.     {
  81.         if(cnt+1>MAX)
  82.             break;     
  83.         cnt++;
  84.         beautiful.push_back(i);            
  85.         q.push({i,i*i});
  86.     }  
  87.     else
  88.         break;
  89.   }
  90.  
  91.   while(!q.empty())
  92.   {
  93.      if(cnt+1>MAX)
  94.         break;
  95.     int  num=q.front().first;
  96.     int sum=q.front().second;  
  97.       q.pop();
  98.       for(int i=0; i<=9; i++)
  99.       {
  100.         if(sum+i*i<=x)
  101.         {
  102.             int newNum=(((num*10)%MOD)+i)%MOD;             
  103.             if(cnt+1>MAX)
  104.                 break; 
  105.             cnt++;     
  106.             beautiful.push_back(newNum);               
  107.             q.push({newNum,sum+i*i});
  108.         }  
  109.         else
  110.             break;
  111.       }
  112.   }
  113.   graph.resize(n+1);  
  114.   for(int i=0; i<n-1; i++)
  115.   {
  116.       int u,v;
  117.       cin>>u>>v;     
  118.       graph[u].push_back(v);
  119.       graph[v].push_back(u);     
  120.   }
  121.    
  122.   vis.assign(n+1,false);
  123.   in.resize(n+1);
  124.   out.resize(n+1);
  125.   tot=1;
  126.   nodes.push_back(0); //dummy element, since i kept 1-based indexing
  127.   dfs(1);
  128.   vector<int> val(n+1);
  129.   BIT b(n);
  130.   for(int i=1; i<=n; ++i)
  131.   {
  132.       cin>>val[i];
  133.       int ind=val[i]-1;
  134.       b.update(in[i],beautiful[ind]);
  135.   }    
  136.   while(m--)
  137.   {
  138.       int type;
  139.       cin>>type;
  140.       if(type==1)
  141.       {
  142.           int i,y;
  143.           cin>>i>>y;
  144.           int prev=beautiful[val[i]-1];
  145.           int curr=beautiful[y-1];
  146.           int del=curr-prev;
  147.           b.update(in[i],del);
  148.           val[i]=y;
  149.       }
  150.       else
  151.       {
  152.           int i;
  153.           cin>>i;
  154.           int l=in[i];
  155.           int r=out[i];
  156.           cout<<b.query(l,r)<<'\n';
  157.       }
  158.   }
  159.   return 0;
  160. }
  161.  
  162. /*  Sample 1:
  163. 3 3 5
  164. 1 2
  165. 1 3
  166. 1 2 3
  167. 2 1
  168. 1 2 5
  169. 2 1
  170. * */
  171.  
  172. /*  Sample 2:
  173. 3 3 5
  174. 1 2
  175. 1 3
  176. 1 2 3
  177. 2 1
  178. 1 2 98
  179. 2 1
  180. * */
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement