Advertisement
Abrar_Al_Samit

Distinct Colors (CSES)

Jul 15th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 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.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8.  
  9. #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
  10.  
  11. template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
  12.  
  13. const int block_size = 800;
  14.  
  15.  
  16. // 0 based indexing
  17. class MosAlgorithm {
  18. public:
  19.     struct Query {
  20.         int l, r, index;
  21.         bool operator< (Query o) const {
  22.             return make_pair(make_pair(l/block_size, r), index) < make_pair(make_pair(o.l/block_size, o.r), o.index);
  23.         };
  24.     };
  25.     int N, M; // N -> Query count, M -> Max element
  26.     vector<Query>queries;
  27.     vector<int>range, A;
  28.     MosAlgorithm(vector<pair<int,int>>&_queries) {
  29.         N = _queries.size();
  30.         for(int i=0; i<N; ++i) {
  31.             Query q = {_queries[i].first, _queries[i].second, i};
  32.             queries.push_back(q);
  33.         }
  34.         sort(queries.begin(), queries.end());
  35.     }
  36.     void Hash(vector<int>a) {
  37.         map<int,int>H;
  38.         A = a;
  39.         for(int i=0; i<A.size(); ++i) H[A[i]] = 0;
  40.         int cur = 0;
  41.         for(auto &it : H) it.second = cur++;
  42.         for(int i=0; i<A.size(); ++i) A[i] = H[A[i]];
  43.         M = cur;
  44.         range.resize(M+1, 0);
  45.     }
  46.     vector<int>process() {
  47.         int L = 0, R = -1, cur = 0;
  48.         vector<int>ret(N);
  49.         for(Query q : queries) {
  50.             while(L > q.l) {
  51.                 if(++range[A[--L]]==1) ++cur;
  52.             }
  53.             while(R < q.r) {
  54.                 if(++range[A[++R]]==1) ++cur;
  55.             }
  56.             while(L < q.l) {
  57.                 if(--range[A[L++]]==0) --cur;
  58.             }
  59.             while(R > q.r) {
  60.                 if(--range[A[R--]]==0) --cur;
  61.             }
  62.             ret[q.index] = cur;
  63.         }
  64.         return ret;
  65.     }
  66. };
  67. void PlayGround() {
  68.     int n; cin >> n;
  69.     vector<int>a(n), b(n);
  70.     vector<vector<int>>edges(n, vector<int>());
  71.     for(auto& it : a) cin >> it;
  72.     for(int i=1; i<n; ++i) {
  73.         int u, v; cin >> u >> v;
  74.         --u, --v;
  75.         edges[u].push_back(v);
  76.         edges[v].push_back(u);
  77.     }
  78.     vector<pair<int,int>>p(n);
  79.     int timer = 0;
  80.     function<void(int,int)> DFS = [&] (int node, int parent) {
  81.         p[node].first = timer;
  82.         b[timer++] = a[node];
  83.         for(auto it : edges[node]) if(it!=parent) {
  84.             DFS(it, node);
  85.         }
  86.         p[node].second = timer-1;
  87.     };
  88.     DFS(0,0);
  89.     MosAlgorithm Mo(p);
  90.     Mo.Hash(b);
  91.     for(int ans : Mo.process()) {
  92.         cout << ans << ' ';
  93.     }
  94.     cout << '\n';
  95.  
  96.     #ifndef ONLINE_JUDGE
  97.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  98.     #endif
  99. }
  100. int main() {
  101.     ios_base::sync_with_stdio(0);
  102.     cin.tie(0);
  103.     cout.tie(0);
  104.     #ifndef ONLINE_JUDGE
  105.         freopen("input.txt", "r", stdin);
  106.     #endif
  107.     PlayGround();
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement