Advertisement
arujbansal

Experience

Feb 1st, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
  4. #define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
  5. #define pb push_back
  6. #define eb emplace_back
  7. #define all(x) (x).begin(), (x).end()
  8. #define rall(x) (x).rbegin(), (x).rend()
  9. #define sz(x) (int) (x).size()
  10. #define lc(i) 2*i
  11. #define rc(i) 2*i+1
  12. //#define int long long
  13. using namespace std;
  14. using ll = long long;
  15. using ii = pair<int, int>;
  16. using vii = vector<ii>;
  17. using vi = vector<int>;
  18. using vvi = vector<vi>;
  19. using vb = vector<bool>;
  20. using vvb = vector<vb>;
  21.  
  22. const int N = 2e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 5;
  23.  
  24. struct DSU {
  25.     int n;
  26.     vector<int> par, s, delta, deltaRoot;
  27.     vector<int> g[N];
  28.  
  29.     void init(int x) {
  30.         n = x;
  31.         par.resize(n);
  32.         iota(all(par), 0);
  33.         s.assign(n, 1);
  34.         delta.assign(n, 0);
  35.         deltaRoot.assign(n, 0);
  36.     }
  37.  
  38.     void mark(int x, int newPar) {
  39.         delta[x] += deltaRoot[par[x]] - deltaRoot[newPar];
  40.  
  41.         par[x] = newPar;
  42.  
  43.         for (const auto &v : g[x])
  44.             if (par[v] != newPar)
  45.                 mark(v, newPar);
  46.     }
  47.  
  48.     void unite(int x, int y) {
  49.         x = par[x];
  50.         y = par[y];
  51.  
  52.         if (x == y) return;
  53.  
  54.         g[x].pb(y);
  55.         g[y].pb(x);
  56.  
  57.         if (s[x] > s[y]) swap(x, y);
  58.         mark(x, y);
  59.         s[y] += s[x];
  60.     }
  61.  
  62.     int get(int x) {
  63.         return delta[x] + deltaRoot[par[x]];
  64.     }
  65.  
  66.     void add(int x, int v) {
  67.         x = par[x];
  68.         deltaRoot[x] += v;
  69.     }
  70. };
  71.  
  72. signed main() {
  73.     FAST_IO;
  74. #ifdef arujbansal_local
  75.     setIO("input.txt", "output.txt");
  76. #endif
  77.  
  78.     int n, q;
  79.     cin >> n >> q;
  80.  
  81.     DSU players;
  82.     players.init(n);
  83.  
  84.     while (q--) {
  85.         string query;
  86.         cin >> query;
  87.  
  88.         if (query == "join")  {
  89.             int x, y;
  90.             cin >> x >> y;
  91.             x--, y--;
  92.  
  93.             players.unite(x, y);
  94.         } else if (query == "add") {
  95.             int x, v;
  96.             cin >> x >> v;
  97.             x--;
  98.  
  99.             players.add(x, v);
  100.         } else {
  101.             int x;
  102.             cin >> x;
  103.             x--;
  104.  
  105.             cout << players.get(x) << "\n";
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement