Snapper_001

Untitled

Mar 19th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<ext/pb_ds/assoc_container.hpp>
  3. #include<ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6. #define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  7. #define pb push_back
  8. #define all(x) (x).begin(),(x).end()
  9. #define ll long long
  10. #define ull unsigned long long
  11. #define ld long double
  12. #define eps 1e-9
  13. #define sz(a) (ll)(a).size()
  14. #define ppc __builtin_popcount
  15. #define ppcll __builtin_popcountll
  16. #define mem1(a) memset(a,-1,sizeof(a))
  17. #define mem0(a) memset(a,0,sizeof(a))
  18. #define endl "\n"
  19. #define lb lower_bound
  20. #define ub upper_bound
  21. template<class T> using ordered_set = tree<T, null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
  22. template<class T> using ordered_multiset = tree<T, null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
  23. const ld PI = acos(-1.0);
  24. const int MOD = 1e9 +7;
  25. const ll INF = 1e18;
  26. // if(abs(a-b)<eps) --> if(a==b)
  27. // fixed << setprecision(n) -->printing decimal till n
  28. // hypot(a ,b) --> sqrt(a^2 + b^2)
  29.  
  30. void solve(){
  31. ll n;
  32. cin>>n;
  33. vector<ll>C(n);
  34. for(int i=0;i<n;i++){
  35. cin>>C[i];
  36. }
  37.  
  38. vector<ll>adj[n];
  39. for(int i=0;i<n-1;i++){
  40. ll a, b;
  41. cin>>a>>b;
  42. a--;
  43. b--;
  44. adj[a].pb(b);
  45. adj[b].pb(a);
  46. }
  47.  
  48. ll time= 0;
  49. vector<ll>st(n) , en(n);
  50. function<void(ll ,ll)> dfs=[&](ll root , ll parent){
  51. st[root] = time;
  52. for(auto it : adj[root]){
  53. if(it==parent) continue;
  54. time++;
  55. dfs(it ,root);
  56. }
  57. en[root] = time;
  58. };
  59. dfs(0, -1);
  60.  
  61. vector<ll>A(n);
  62. for(int i=0;i<n;i++){
  63. A[st[i]] = C[i];
  64. }
  65.  
  66. //Sqrt Decomposition
  67. //two methods Online Query , Offline query
  68. //offline : we sort the queries and run in blocks
  69. //online : we precompute something nad combine blocks
  70.  
  71. //Offline : --> (Sorting the queries ,MOs)
  72. //Coordinate compression so that we can apply add and remove function
  73. unordered_map<ll ,ll>mp1;
  74. ll ind =0;
  75.  
  76. auto G = A;
  77. sort(all(G));
  78. G.resize(unique(all(G)) - G.begin());
  79. for(int i=0;i<sz(G);i++){
  80. mp1[G[i]] = ind++;
  81. }
  82. for(int i=0;i<n;i++){
  83. A[i] = mp1[A[i]];
  84. }
  85.  
  86. vector<pair<pair<ll,ll> ,ll>>Queries;
  87. for(int i=0;i<n;i++){
  88. Queries.pb({{st[i] , en[i]} , i});
  89. }
  90.  
  91. ll len = ceil(sqrt(n));
  92. auto comp=[&](pair<pair<ll, ll>, ll>a , pair<pair<ll, ll>, ll>b){
  93. ll s11 = (a.first.first/len);
  94. ll s12 = (b.first.first/len);
  95. ll s21 = (a.first.second);
  96. ll s22 = (b.first.second);
  97. if(s11==s12){
  98. return s11&1?s21>s22:s21<s22; //slight modification
  99. }
  100. return s11<s12;
  101. };
  102.  
  103. sort(all(Queries) , comp);
  104.  
  105. vector<ll>ans(n, 0);
  106. vector<ll>Cnt(2e5+1 , 0); //we remove and add by this
  107. ll l =0;
  108. ll r =-1;
  109. //l to r is the current range
  110. //We try to expand and shrink acc to new query
  111. ll distinct = 0;
  112. for(auto it : Queries){
  113. while(r < it.first.second){
  114. r++;
  115. Cnt[A[r]]++;
  116. if(Cnt[A[r]]==1) distinct++;
  117. }
  118. while(l > it.first.first){
  119. l--;
  120. Cnt[A[l]]++;
  121. if(Cnt[A[l]]==1) distinct++;
  122. }
  123.  
  124. while(r > it.first.second){
  125. Cnt[A[r]]--;
  126. if(Cnt[A[r]]==0) distinct--;
  127. r--;
  128. }
  129. while(l < it.first.first){
  130. Cnt[A[l]]--;
  131. if(Cnt[A[l]]==0) distinct--;
  132. l++;
  133. }
  134. ans[it.second] = distinct;
  135. }
  136. for(auto it: ans){
  137. cout<<it<<" ";
  138. }
  139. cout<<endl;
  140.  
  141. }
  142.  
  143. int main(){
  144. IO;
  145. ll t =1;
  146. // cin>>t;
  147. for(int i=1;i<=t;i++){
  148. // cout<<"Case #"<<i<<": ";
  149. solve();
  150. }
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment