• [SDOI2013]森林


    嘟嘟嘟


    如果不是森林,很自然想到树上主席树。
    但现在他是个森林,那就lct+主席树?
    不,怎么会那么毒瘤。
    只要启发式合并就好啦。


    不用垃圾回收(主席树启发式合并垃圾回收似乎很麻烦)。需要注意的是,一定要处理好较小树节点的新的元素,包括深度,父亲……最重要的是求lca的倍增数组,一定要都清空,因为合并到新的树后他的深度可能没有原来那么深,所以更远的祖先就不存在了。但是如果不清空的话,倍增的时候就会跳到那里。毕竟此时fa[u][x] ^ fa[i][y]是成立的。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 8e4 + 5;
    const int N = 18;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    char s[2];
    int ID, _n, n, m, q;
    int a[maxn], b[maxn];
    struct Edge
    {
      int nxt, to;
    }e[maxn << 1];
    int head[maxn], ecnt = -1;
    In void addEdge(int x, int y)
    {
      e[++ecnt] = (Edge){head[x], y};
      head[x] = ecnt;
    }
    
    struct Tree
    {
      int ls, rs, sum;
    }t[maxn * 400];
    int root[maxn], tcnt = 0;
    In void insert(int old, int& now, int l, int r, int id)
    {
      t[now = ++tcnt] = t[old];
      ++t[now].sum;
      if(l == r) return;
      int mid = (l + r) >> 1;
      if(id <= mid) insert(t[old].ls, t[now].ls, l, mid, id);
      else insert(t[old].rs, t[now].rs, mid + 1, r, id);
    }
    In int query(int pre, int Lca, int x, int y, int l, int r, int k)
    {
      if(l == r) return l;
      int mid = (l + r) >> 1, Sum = t[t[x].ls].sum + t[t[y].ls].sum - t[t[pre].ls].sum - t[t[Lca].ls].sum;
      if(k <= Sum) return query(t[pre].ls, t[Lca].ls, t[x].ls, t[y].ls, l, mid, k);
      else return query(t[pre].rs, t[Lca].rs, t[x].rs, t[y].rs, mid + 1, r, k - Sum);
    }
    
    int siz[maxn], bel[maxn];
    int dep[maxn], fa[N + 2][maxn];
    In int lca(int x, int y)
    {
      if(dep[x] < dep[y]) swap(x, y);
      for(int i = N; i >= 0; --i)
        if(dep[x] - (1 << i) >= dep[y]) x = fa[i][x];
      if(x == y) return x;
      for(int i = N; i >= 0; --i)
        if(fa[i][x] ^ fa[i][y]) x = fa[i][x], y = fa[i][y];
      return fa[0][x];
    }
    In void dfs(int now, int _f, int rot)
    {
      siz[now] = 1; bel[now] = rot;
      for(int i = 1; i <= N; ++i) fa[i][now] = 0;
      for(int i = 1; (1 << i) <= dep[now]; ++i)
        fa[i][now] = fa[i - 1][fa[i - 1][now]];
      insert(root[_f], root[now], 1, _n, a[now]);
      for(int i = head[now], v; ~i; i = e[i].nxt)
        {
          if((v = e[i].to) == _f) continue;
          fa[0][v] = now, dep[v] = dep[now] + 1;
          dfs(v, now, rot);
          siz[now] += siz[v];
        }
    }
    
    int main()
    {
      Mem(head, -1);
      ID = read();
      n = read(), m = read(), q = read();
      for(int i = 1; i <= n; ++i) a[i] = b[i] = read();
      sort(b + 1, b + n + 1);
      _n = unique(b + 1, b + n + 1) - b - 1;
      for(int i = 1; i <= n; ++i)
        a[i] = lower_bound(b + 1, b + _n + 1, a[i]) - b;
      for(int i = 1; i <= m; ++i)
        {
          int x = read(), y = read();
          addEdge(x, y), addEdge(y, x);
        }
      for(int i = 1; i <= n; ++i) if(!siz[i]) dfs(i, 0, i);
      for(int i = 1, ans = 0; i <= q; ++i)
        {
          scanf("%s", s); int x = read() ^ ans, y = read() ^ ans;
          if(s[0] == 'Q')
    	{
    	  int K = read() ^ ans, z = lca(x, y);
    	  ans = b[query(root[fa[0][z]], root[z], root[x], root[y], 1, _n, K)];
    	  write(ans), enter;
    	}
          else
    	{
    	  addEdge(x, y), addEdge(y, x);
    	  if(siz[bel[x]] > siz[bel[y]]) swap(x, y);
    	  fa[0][x] = y, dep[x] = dep[y] + 1, siz[bel[y]] += siz[bel[x]]; 
    	  dfs(x, y, bel[y]);
    	}
        }
      return 0;
    }
    
  • 相关阅读:
    忘记 mysql 数据库连接密码(解决方案)
    CVE-2020-14882&CVE-2020-14883 Weblogic未授权远程命令执行漏洞
    社会工程学之信息收集之信息收集
    8种src常用越权测试小技巧
    《数据中台-让数据用起来》思维导图(更新中)
    idea使用zsh代替系统的terminal
    mac安装oh my zsh
    mac安装homebrew
    navicat破解(亲测可用)
    docker搭建typecho博客系统,并启用https
  • 原文地址:https://www.cnblogs.com/mrclr/p/10645063.html
Copyright © 2020-2023  润新知