• BZOJ #4771. 七彩树 [主席树,链并,差分]


    乱搞即可。
    不强制在线就可以dsu on tree+bit,强制在线只能主席树了(摊手)。

    // 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;
    using ull = unsigned long long;
    
    using pii = pair<int, int>;
    
    #define fir first
    #define sec second
    
    template <class T>
    
    void cmax(T& x, const T& y) {
      if (x < y) x = y;
    }
    
    template <class T>
    
    void cmin(T& x, const T& y) {
      if (x > y) x = y;
    }
    
    #define all(v) v.begin(), v.end()
    #define sz(v) ((int)v.size())
    #define pb emplace_back
    
    template <class T>
    
    void sort(vector<T>& v) {
      sort(all(v));
    }
    
    template <class T>
    
    void reverse(vector<T>& v) {
      reverse(all(v));
    }
    
    template <class T>
    
    void unique(vector<T>& v) {
      sort(all(v)), v.erase(unique(all(v)), v.end());
    }
    
    void reverse(string& s) { reverse(s.begin(), s.end()); }
    
    const int io_size = 1 << 23 | 233;
    const int io_limit = 1 << 22;
    struct io_in {
      char ch;
    #ifndef __WIN64
      char getchar() {
        static char buf[io_size], *p1 = buf, *p2 = buf;
    
        return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, io_size, stdin), p1 == p2) ? EOF : *p1++;
      }
    #endif
      io_in& operator>>(char& c) {
        for (c = getchar(); isspace(c); c = getchar())
          ;
    
        return *this;
      }
      io_in& operator>>(string& s) {
        for (s.clear(); isspace(ch = getchar());)
          ;
    
        if (!~ch) return *this;
    
        for (s = ch; !isspace(ch = getchar()) && ~ch; s += ch)
          ;
    
        return *this;
      }
    
      io_in& operator>>(char* str) {
        char* cur = str;
        while (*cur) *cur++ = 0;
    
        for (cur = str; isspace(ch = getchar());)
          ;
        if (!~ch) return *this;
    
        for (*cur = ch; !isspace(ch = getchar()) && ~ch; *++cur = ch)
          ;
    
        return *++cur = 0, *this;
      }
    
      template <class T>
    
      void read(T& x) {
        bool f = 0;
        while ((ch = getchar()) < 48 && ~ch) f ^= (ch == 45);
    
        x = ~ch ? (ch ^ 48) : 0;
        while ((ch = getchar()) > 47) x = x * 10 + (ch ^ 48);
        x = f ? -x : x;
      }
    
      io_in& operator>>(int& x) { return read(x), *this; }
    
      io_in& operator>>(ll& x) { return read(x), *this; }
    
      io_in& operator>>(uint& x) { return read(x), *this; }
    
      io_in& operator>>(ull& x) { return read(x), *this; }
    
      io_in& operator>>(db& x) {
        read(x);
        bool f = x < 0;
        x = f ? -x : x;
        if (ch ^ '.') return *this;
    
        double d = 0.1;
        while ((ch = getchar()) > 47) x += d * (ch ^ 48), d *= .1;
        return x = f ? -x : x, *this;
      }
    } in;
    
    struct io_out {
      char buf[io_size], *s = buf;
      int pw[233], st[233];
    
      io_out() {
        set(7);
        rep(i, pw[0] = 1, 9) pw[i] = pw[i - 1] * 10;
      }
    
      ~io_out() { flush(); }
    
      void io_chk() {
        if (s - buf > io_limit) flush();
      }
    
      void flush() { fwrite(buf, 1, s - buf, stdout), fflush(stdout), s = buf; }
    
      io_out& operator<<(char c) { return *s++ = c, *this; }
    
      io_out& operator<<(string str) {
        for (char c : str) *s++ = c;
        return io_chk(), *this;
      }
    
      io_out& operator<<(char* str) {
        char* cur = str;
        while (*cur) *s++ = *cur++;
        return io_chk(), *this;
      }
    
      template <class T>
    
      void write(T x) {
        if (x < 0) *s++ = '-', x = -x;
    
        do {
          st[++st[0]] = x % 10, x /= 10;
        } while (x);
    
        while (st[0]) *s++ = st[st[0]--] ^ 48;
      }
    
      io_out& operator<<(int x) { return write(x), io_chk(), *this; }
    
      io_out& operator<<(ll x) { return write(x), io_chk(), *this; }
    
      io_out& operator<<(uint x) { return write(x), io_chk(), *this; }
    
      io_out& operator<<(ull x) { return write(x), io_chk(), *this; }
    
      int len, lft, rig;
    
      void set(int _length) { len = _length; }
    
      io_out& operator<<(db x) {
        bool f = x < 0;
        x = f ? -x : x, lft = x, rig = 1. * (x - lft) * pw[len];
        return write(f ? -lft : lft), *s++ = '.', write(rig), io_chk(), *this;
      }
    } out;
    #define int long long
    
    template <int sz, int mod>
    
    struct math_t {
      math_t() {
        fac.resize(sz + 1), ifac.resize(sz + 1);
        rep(i, fac[0] = 1, sz) fac[i] = fac[i - 1] * i % mod;
    
        ifac[sz] = inv(fac[sz]);
        Rep(i, sz - 1, 0) ifac[i] = ifac[i + 1] * (i + 1) % mod;
      }
    
      vector<int> fac, ifac;
    
      int qpow(int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = x * x % mod)
          if (y & 1) ans = ans * x % mod;
        return ans;
      }
    
      int inv(int x) { return qpow(x, mod - 2); }
    
      int C(int n, int m) {
        if (n < 0 || m < 0 || n < m) return 0;
        return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
      }
    };
    
    int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
    int lcm(int x, int y) { return x * y / gcd(x, y); }
    
    const int maxn = 5e5 + 55;
    vector<int> g[maxn];
    int dfn[maxn], idx = 0, dfo[maxn];
    int dep[maxn];
    vector<int> d[maxn];
    
    struct qwqwq {
      int n;
      int f[maxn][22];
      
      void init(int _n) {
        n = _n;
        idx = 0;
        for (int i = 1; i <= n; i++) dfn[i] = dfo[i] = dep[i] = 0;
        for (int i = 1; i <= n; i++)
          for (int j = 0; j < 22; j++) f[i][j] = 0;
      }
    
      void dfs(int u) {
      	dep[u] = dep[f[u][0]] + 1;
        dfn[u] = ++idx;
        d[dep[u]].pb(u);
        for (int v : g[u]) {
          if (v ^ f[u][0]) {
            f[v][0] = u;
            dfs(v);
          }
        }
        dfo[u] = idx;
      }
    
      void solve() {
        dfs(1);
        for (int j = 1; j < 22; j++)
          for (int i = 1; i <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
      }
    
      int lca(int x, int y) {
        if (dep[x] < dep[y]) x ^= y ^= x ^= y;
        for (int i = 20; ~i; i--)
          if (dep[f[x][i]] >= dep[y]) x = f[x][i];
        if (x == y) return x;
        for (int i = 20; ~i; i--)
          if (f[x][i] ^ f[y][i]) x = f[x][i], y = f[y][i];
        return f[x][0];
      }
    } mt;
    
    struct set_cmp {
      int x;
      bool operator()(const int x, const int y) { return dfn[x] < dfn[y]; }
    };
    
    set<int, set_cmp> s[maxn];
    int c[maxn];
    
    struct smt {
      int cnt;
      smt() { cnt = 0; }
      int rt[maxn];
      int val[maxn << 6];
      int ls[maxn << 6];
      int rs[maxn << 6];
    	
    	void init(int _n) {
    		for(int i = 1 ; i <= _n ; i ++)
    			rt[i] = 0;
    	}
    	
      int newnode() {
        ++cnt;
        val[cnt] = 0;
        ls[cnt] = 0;
        rs[cnt] = 0;
        return cnt;
      }
    
      void upd(int& p, int l, int r, int x, int v) {
        if (!p) p = newnode();
        val[p] += v;
        if (l == r) return;
        int mid = l + r >> 1;
        if (x <= mid)
          upd(ls[p], l, mid, x, v);
        else
          upd(rs[p], mid + 1, r, x, v);
      }
    
      int merge(int x, int y) {
        if (!x || !y) return x | y;
        val[x] += val[y];
        ls[x] = merge(ls[x], ls[y]);
        rs[x] = merge(rs[x], rs[y]);
        return x;
      }
    
      int qry(int p, int a, int b, int l, int r) {
        if (a <= l && r <= b) return val[p];
        int mid = l + r >> 1, ans = 0;
        if (a <= mid) ans += qry(ls[p], a, b, l, mid);
        if (b > mid) ans += qry(rs[p], a, b, mid + 1, r);
        return ans;
      }
    } smt;
    
    int n, m;
    signed main() {
      // code begin.
      int _;
      in >> _;
      while (_--) {
        in >> n >> m;
        mt.init(n);
        smt.init(n);
        smt.cnt = 0;
        for (int i = 1; i <= n; i++) g[i].clear(), s[i].clear(), d[i].clear();
        for (int i = 1; i <= n; i++) in >> c[i];
        for (int i = 2; i <= n; i++) {
          int x;
          in >> x;
          g[x].pb(i);
        }
        mt.solve();
        int mx = 0;
        for (int i = 1; i <= n; i++) {
          if (sz(d[i])) mx = i;
          for (int x : d[i]) {
            auto lft = s[c[x]].lower_bound(x);
            auto rig = s[c[x]].lower_bound(x);
            int l, r;
    
            if (lft == s[c[x]].begin())
              l = -1;
            else
              --lft, l = *lft;
    
            if (rig == s[c[x]].end())
              r = -1;
            else
              r = *rig;
    
            smt.upd(smt.rt[i], 1, n, dfn[x], 1);
            if (~l) smt.upd(smt.rt[i], 1, n, dfn[mt.lca(l, x)], -1);
            if (~r) smt.upd(smt.rt[i], 1, n, dfn[mt.lca(x, r)], -1);
            if (~l && ~r) smt.upd(smt.rt[i], 1, n, dfn[mt.lca(l, r)], 1);
            s[c[x]].insert(x);
          }
          smt.rt[i] = smt.merge(smt.rt[i], smt.rt[i - 1]);
        }
        
        int ans = 0;
        while (m--) {
          int x, d;
          in >> x >> d;
          x ^= ans;
          d ^= ans;
          d = min(mx, dep[x] + d);
          ans = smt.qry(smt.rt[d], dfn[x], dfo[x], 1, n);
          out << ans << '
    ';
        }
      }
      return 0;
      // code end.
    }
    
  • 相关阅读:
    Android组件界面设计工具 DroidDraw
    ExtAspNet官方示例网站和官方论坛速度归来!
    [原创]FineUI秘密花园(四) — 页面级别的配置
    [翻译]OAuth入门指南 2. 协议工作流
    ExtAspNet v2.3.4 发布了
    [翻译]JavaScript秘密花园 Type Casting,undefined,eval,setTimeout,Auto Semicolon Insertion 全部完成PDF打包下载
    [原创]祝贺《JavaScript 秘密花园》中文翻译被官方正式采纳
    [原创]使用 Google Chart 在线服务实现软件版本发布时间线图
    [原创]ExtAspNet新春贺岁版 文件上传控件、复选框列表控件、完善布局管理、表格的行扩展列、完整的100多个中文示例、30多处BUG修正与功能增强、《ExtAspNet秘密花园》系列文章
    ExtAspNet v2.3.5 发布了
  • 原文地址:https://www.cnblogs.com/Isaunoya/p/12654815.html
Copyright © 2020-2023  润新知