P5443 [APIO2019]桥梁 [分块+并查集]

分块+并查集,大板子,没了。
并查集不路径压缩,可撤销,然后暴力删除
这样对于每个块都是独立的,所以直接搞就行了。
然后块内修改操作搞掉,就是单独的了

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
// #define int long long
using pii = pair<int, int>;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp<class T> void cmax(T& x, const T& y) {
  if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
  if (x > y) x = y;
}
// sort , unique , reverse
Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
Tp<class T> void unique(ve<T>& v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
Tp<class T> void reverse(ve<T>& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
  ~FILEIN() {}
  char qwq[SZ], *S = qwq, *T = qwq, ch;
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
  FILEIN& operator>>(char& c) {
    while (isspace(c = GETC()))
      ;
    return *this;
  }
  FILEIN& operator>>(string& s) {
    while (isspace(ch = GETC()))
      ;
    s = ch;
    while (!isspace(ch = GETC())) s += ch;
    return *this;
  }
  Tp<class T> void read(T& x) {
    bool sign = 1;
    while ((ch = GETC()) < 0x30)
      if (ch == 0x2d) sign = 0;
    x = (ch ^ 0x30);
    while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
    x = sign ? x : -x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  // FILEIN& operator>>(signed& x) { return read(x), *this; }
  FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 0x114514;
  char quq[SZ], ST[0x114];
  signed sz, O;
  ~FILEOUT() { flush(); }
  void flush() {
    fwrite(quq, 1, O, stdout);
    fflush(stdout);
    O = 0;
  }
  FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
  FILEOUT& operator<<(string str) {
    if (O > LIMIT) flush();
    for (char c : str) quq[O++] = c;
    return *this;
  }
  Tp<class T> void write(T x) {
    if (O > LIMIT) flush();
    if (x < 0) {
      quq[O++] = 0x2d;
      x = -x;
    }
    do {
      ST[++sz] = x % 0xa ^ 0x30;
      x /= 0xa;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
    return;
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  // FILEOUT& operator<<(signed x) { return write(x), *this; }
  FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out;

const int maxn = 2e5 + 52;
int n, m, num = 0;
int q, len, ans[maxn];
bool vis[maxn], used[maxn];
vector<int> b;
struct Edge {
  int u, v, w;
} e[maxn];
struct Que {
  int op, x, y;
} Q[maxn];
struct Qry {
  int id, u, v, w;
  bool operator<(const Qry& other) const {
    if (w != other.w) return w > other.w;
    return id < other.id;
  }
};
vector<Qry> qr;
struct DSU {
  int n, fa[maxn], sz[maxn], top, sta[maxn], stb[maxn];
  inline void reset(int _n) {
    n = _n, top = 0;
    for (int i = 1; i <= n; i++) sz[fa[i] = i] = 1;
  }
  inline void reset() {
    while (top) {
      int u = sta[top], v = stb[top];
      sz[u] -= sz[v], fa[v] = v;
      top--;
    }
  }
  inline int find(int x) { return x == fa[x] ? x : find(fa[x]); }
  inline int getsize(int x) {
    int fx = find(x);
    return sz[fx];
  }
  inline void merge(int u, int v, int r) {
    if ((u = find(u)) == (v = find(v))) return;
    if (sz[u] < sz[v]) u ^= v ^= u ^= v;
    sz[u] += sz[v], fa[v] = u;
    if (r) sta[++top] = u, stb[top] = v;
  }
} dsu;
inline void solve(int l, int r) {
  dsu.reset(n);
  b.clear(), qr.clear();
  for (int i = 1; i <= m; i++) {
    used[i] = 0;
  }
  for (int i = l; i <= r; i++) {
    if (Q[i].op == 1) {
      used[Q[i].x] = 1;
    } else {
      qr.push_back({ ++q, Q[i].x, i, Q[i].y });
    }
  }
  for (int i = 1; i <= m; i++)
    if (!used[i])
      qr.push_back({ -1, e[i].u, e[i].v, e[i].w });
    else
      b.push_back(i);
  sort(qr.begin(), qr.end());
  for (auto x : qr) {
    if (x.id == -1) {
      dsu.merge(x.u, x.v, 0);
      continue;
    }
    for (int i = x.v; i >= l; i--) {
      if (Q[i].op == 1) {
        if (!vis[Q[i].x]) {
          vis[Q[i].x] = 1;
          if (Q[i].y >= x.w) {
            dsu.merge(e[Q[i].x].u, e[Q[i].x].v, 1);
          }
        }
      }
    }
    for (auto y : b) {
      if (!vis[y] && e[y].w >= x.w) {
        dsu.merge(e[y].u, e[y].v, 1);
      }
    }
    ans[x.id] = dsu.getsize(x.u);
    for (auto y : b) {
      vis[y] = 0;
    }
    dsu.reset();
  }
  for (int i = l; i <= r; i++) {
    if (Q[i].op == 1) {
      e[Q[i].x].w = Q[i].y;
    }
  }
}
signed main() {
#ifdef _WIN64
  freopen("testdata.in", "r", stdin);
#else
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
#endif
  // code begin.
  in >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    in >> u >> v >> w;
    e[i] = { u, v, w };
  }
  in >> num;
  len = 900;
  for (int i = 1; i <= num; i++) {
    in >> Q[i].op >> Q[i].x >> Q[i].y;
  }
  for (int i = 1; i <= num; i += len) solve(i, min(num, i + len - 1));
  for (int i = 1; i <= q; i++) out << ans[i] << '\n';
  return 0;
  // code end.
}
posted @ 2020-02-15 11:09  _Isaunoya  阅读(188)  评论(0编辑  收藏  举报