ZhenyaDudko

Тир

Jan 5th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <string>
  7. #include <set>
  8. #include <unordered_set>
  9. #include <unordered_map>
  10. #include <queue>
  11. #include <map>
  12. #include <stack>
  13. #include <bitset>
  14. typedef long long ll;
  15. using namespace std;
  16. struct qr {
  17.     int a, b, tp;
  18. };
  19. struct ad {
  20.     int l, r, a, b;
  21. };
  22. vector<int> vec;
  23. vector<pair<int,int>> tree[1600010];
  24. void change(int v, int l, int r, int askl, int askr, int a, int b) {
  25.     if (askr <= l || r <= askl)
  26.         return;
  27.     if (askl <= l && r <= askr) {
  28.         tree[v].push_back({ a,b });
  29.         return;
  30.     }
  31.     int m = (l + r) / 2;
  32.     change(v * 2 + 1, l, m, askl, askr, a, b);
  33.     change(v * 2 + 2, m, r, askl, askr, a, b);
  34. }
  35. vector<int> dsu, cntmish, rang;
  36. void build(int n, int cm) {
  37.     dsu.resize(n);
  38.     cntmish.resize(n);
  39.     rang.resize(n, 1);
  40.     for (int i = 0; i < n; i++)
  41.         dsu[i] = i;
  42.     for (int i = 1; i <= cm; i++)
  43.         cntmish[i] = 1;
  44. }
  45. int Find(int a) {
  46.     return (dsu[a] == a ? a : Find(dsu[a]));
  47. }
  48. struct ch {
  49.     int ind, to, lr, lc;
  50. };
  51. inline ch Union(int a, int b) {
  52.     a = Find(a);
  53.     b = Find(b);
  54.     if (rang[b] > rang[a])
  55.         swap(a, b);
  56.     dsu[b] = a;
  57.     int lr = rang[a], lc = cntmish[a];
  58.     cntmish[a] += cntmish[b];
  59.     if (rang[b] == rang[a])
  60.         rang[a]++;
  61.     return { b, a, lr, lc };
  62. }
  63. void dfs(int v, int l, int r) {
  64.     vector<ch> changes;
  65.     for (int i = 0; i < tree[v].size(); i++) {
  66.         int a = tree[v][i].first, b = tree[v][i].second;
  67.         a = Find(a), b = Find(b);
  68.         if (a != b) {
  69.             changes.push_back(Union(a, b));
  70.         }
  71.     }
  72.     if (r - l == 1) {
  73.         if (vec[l]) {
  74.             cout << cntmish[Find(0)] << endl;
  75.         }
  76.     }
  77.     else {
  78.         int m = (l + r) / 2;
  79.         dfs(v * 2 + 1, l, m);
  80.         dfs(v * 2 + 2, m, r);
  81.     }
  82.     for (int i = changes.size()-1; i >= 0; i--) {
  83.         cntmish[changes[i].to] = changes[i].lc;
  84.         rang[changes[i].to] = changes[i].lr;
  85.         dsu[changes[i].ind] = changes[i].ind;
  86.     }
  87. }
  88. int main() {
  89.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  90.     int n, m, q;
  91.     cin >> n >> m >> q;
  92.     int tim = 1+m;
  93.     vector<set<pair<int, int>>> rows(n+1), cols(m+1);
  94.     vector<qr> per, qrs;
  95.     for (int i = 1; i <= m; i++) {
  96.         qrs.push_back({ 0,i,0 });
  97.     }
  98.     map<pair<int, int>, int> ma;
  99.     while (q--) {
  100.         int tpp, x, y;
  101.         cin >> tpp >> x >> y;
  102.         if (tpp == 0) {
  103.             int nper = ma[{x, y}];
  104.             int a = per[nper].a, b = per[nper].b, type = per[nper].tp;
  105.             auto ind1 = rows[x].find({ y,nper });
  106.             if (ind1 != rows[x].begin()) {
  107.                 auto indl = prev(ind1);
  108.                 int fr = a, to = per[indl->second].b;
  109.                 qrs.push_back({ min(fr,to), max(fr,to),1 });
  110.             }
  111.             if (next(ind1) != rows[x].end()) {
  112.                 auto indr = next(ind1);
  113.                 int fr = b, to = per[indr->second].a;
  114.                 qrs.push_back({ min(fr, to), max(fr, to), 1 });
  115.             }
  116.             if (ind1 != rows[x].begin() && next(ind1) != rows[x].end()) {
  117.                 int fr = per[prev(ind1)->second].b;
  118.                 int to = per[next(ind1)->second].a;
  119.                 qrs.push_back({ min(fr, to), max(fr, to), 0 });
  120.             }
  121.             auto ind2 = cols[y].find({ x, nper });
  122.             int tp = 0;
  123.             if (ind2 != cols[y].begin()) {
  124.                 auto indt = prev(ind2);
  125.                 if (per[indt->second].tp == 1)
  126.                     tp = per[indt->second].a;
  127.                 else
  128.                     tp = per[indt->second].b;
  129.             }
  130.             int ourtp = b;
  131.             if (type == 2)
  132.                 ourtp = a;
  133.             qrs.push_back({ min(tp, ourtp), max(tp, ourtp), 1 });
  134.             int dn = y;
  135.             if (next(ind2) != cols[y].end()) {
  136.                 auto indd = next(ind2);
  137.                 if (per[indd->second].tp == 1)
  138.                     dn = per[indd->second].b;
  139.                 else
  140.                     dn = per[indd->second].a;
  141.             }
  142.             int ourdn = a;
  143.             if (type == 2)
  144.                 ourdn = b;
  145.             qrs.push_back({ min(ourdn, dn), max(ourdn, dn), 1 });
  146.             qrs.push_back({ min(tp, dn), max(tp, dn), 0 });
  147.             rows[x].erase({ y, nper });
  148.             cols[y].erase({ x, nper });
  149.         }
  150.         else {
  151.             per.push_back({ tim++, tim++, tpp });
  152.             int a = per.back().a, b = per.back().b;
  153.             rows[x].insert({ y, per.size() - 1 });
  154.             cols[y].insert({ x, per.size() - 1 });
  155.             ma[{x, y}] = per.size() - 1;
  156.             auto ind1 = rows[x].find({ y,per.size()-1 });
  157.             if (ind1 != rows[x].begin() && next(ind1) != rows[x].end()) {
  158.                 int fr = per[prev(ind1)->second].b;
  159.                 int to = per[next(ind1)->second].a;
  160.                 qrs.push_back({ min(fr, to), max(fr, to), 1 });
  161.             }
  162.             if (ind1 != rows[x].begin()) {
  163.                 auto indl = prev(ind1);
  164.                 int fr = a, to = per[indl->second].b;
  165.                 qrs.push_back({ min(fr,to), max(fr, to),0 });
  166.             }
  167.             if (next(ind1) != rows[x].end()) {
  168.                 auto indr = next(ind1);
  169.                 int fr = b, to = per[indr->second].a;
  170.                 qrs.push_back({ min(fr, to), max(fr, to), 0 });
  171.             }
  172.             auto ind2 = cols[y].find({ x, per.size()-1 });
  173.             int tp = 0;
  174.             if (ind2 != cols[y].begin()) {
  175.                 auto indt = prev(ind2);
  176.                 if (per[indt->second].tp == 1)
  177.                     tp = per[indt->second].a;
  178.                 else
  179.                     tp = per[indt->second].b;
  180.             }
  181.             int ourtp = b;
  182.             if (tpp == 2)
  183.                 ourtp = a;
  184.             qrs.push_back({ min(tp, ourtp), max(tp, ourtp), 0 });
  185.             int dn = y;
  186.             if (next(ind2) != cols[y].end()) {
  187.                 auto indd = next(ind2);
  188.                 if (per[indd->second].tp == 1)
  189.                     dn = per[indd->second].b;
  190.                 else
  191.                     dn = per[indd->second].a;
  192.             }
  193.             int ourdn = a;
  194.             if (tpp == 2)
  195.                 ourdn = b;
  196.             qrs.push_back({ min(ourdn, dn), max(ourdn, dn), 0 });
  197.             qrs.push_back({ min(tp, dn), max(tp, dn), 1 });
  198.         }
  199.         qrs.push_back({ -1,-1, 2 });
  200.     }
  201.     ma.clear();
  202.     vector<ad> edges;
  203.     int ct = 0;
  204.     for (int i = 0; i < qrs.size(); i++) {
  205.         if (qrs[i].tp == 2) {
  206.             if (i != 0)
  207.                 vec.push_back(0);
  208.             vec.push_back(1);
  209.             ct += 2;
  210.             continue;
  211.         }
  212.         if (qrs[i].tp == 0)
  213.             ma[{qrs[i].a, qrs[i].b}] = ct;
  214.         else {
  215.             edges.push_back({ ma[{qrs[i].a, qrs[i].b}] , ct, qrs[i].a, qrs[i].b });
  216.             ma.erase({ qrs[i].a, qrs[i].b });
  217.         }
  218.     }
  219.     if (ma.size() > 0)
  220.         vec.push_back(0);
  221.     for (auto pr : ma) {
  222.         edges.push_back({ pr.second, (int)vec.size() - 1, pr.first.first, pr.first.second });
  223.     }
  224.     for (int i = 0; i < edges.size(); i++) {
  225.         change(0, 0, vec.size(), edges[i].l, edges[i].r + 1, edges[i].a, edges[i].b);
  226.     }
  227.     build(tim, m);
  228.     dfs(0, 0, vec.size());
  229. }
Advertisement
Add Comment
Please, Sign In to add comment