結果

問題 No.1787 Do Use Dynamic Tree
ユーザー ei1333333ei1333333
提出日時 2021-12-15 17:02:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,485 ms / 10,000 ms
コード長 13,698 bytes
コンパイル時間 2,596 ms
コンパイル使用メモリ 219,696 KB
実行使用メモリ 44,712 KB
最終ジャッジ日時 2023-10-01 03:01:46
合計ジャッジ時間 24,840 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 5 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 1,102 ms
25,212 KB
testcase_23 AC 800 ms
34,904 KB
testcase_24 AC 903 ms
21,996 KB
testcase_25 AC 1,485 ms
37,192 KB
testcase_26 AC 1,463 ms
37,292 KB
testcase_27 AC 1,435 ms
37,176 KB
testcase_28 AC 809 ms
44,700 KB
testcase_29 AC 799 ms
44,660 KB
testcase_30 AC 804 ms
44,712 KB
testcase_31 AC 780 ms
42,196 KB
testcase_32 AC 997 ms
42,252 KB
testcase_33 AC 1,169 ms
42,616 KB
testcase_34 AC 455 ms
42,080 KB
testcase_35 AC 848 ms
42,216 KB
testcase_36 AC 1,119 ms
42,632 KB
testcase_37 AC 943 ms
41,656 KB
testcase_38 AC 961 ms
41,184 KB
testcase_39 AC 958 ms
42,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
//const int mod = 1e9 + 7;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in: v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e: t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

/**
 * @brief 何でもできるLCT
 */
template< typename LInfo >
struct SplayTree {
  struct Node {
    Node *l, *r, *p;
    LInfo info;

    explicit Node(const LInfo &info) : info(info), l(nullptr), r(nullptr),
                                       p(nullptr) {}
  };

  const LInfo e;

  SplayTree() : e(LInfo()) {}

  using NP = Node *;

  void rotr(NP t) {
    NP x = t->p, y = x->p;
    if((x->l = t->r)) t->r->p = x;
    t->r = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
    }
  }

  void rotl(NP t) {
    NP x = t->p, y = x->p;
    if((x->r = t->l)) t->l->p = x;
    t->l = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
    }
  }

  const LInfo &get_info(NP t) {
    return t ? t->info : e;
  }

  void update(NP t) {
    t->info.update(get_info(t->l), get_info(t->r));
  }

  NP get_right(NP t) {
    while(t->r) t = t->r;
    return t;
  }

  NP alloc(const LInfo &v) {
    auto t = new Node(v);
    update(t);
    return t;
  }

  void splay(NP t) {
    while(t->p) {
      NP q = t->p;
      if(!q->p) {
        if(q->l == t) rotr(t);
        else rotl(t);
      } else {
        NP r = q->p;
        if(r->l == q) {
          if(q->l == t) rotr(q), rotr(t);
          else rotl(t), rotr(t);
        } else {
          if(q->r == t) rotl(q), rotl(t);
          else rotr(t), rotl(t);
        }
      }
    }
  }

  NP insert(NP t, const LInfo &v) {
    if(not t) {
      t = alloc(v);
      return t;
    } else {
      NP cur = get_right(t), z = alloc(v);
      splay(cur);
      z->p = cur;
      cur->r = z;
      update(cur);
      splay(z);
      return z;
    }
  }

  NP erase(NP t) {
    splay(t);
    NP x = t->l, y = t->r;
    delete t;
    if(not x) {
      t = y;
      if(t) t->p = nullptr;
    } else if(not y) {
      t = x;
      t->p = nullptr;
    } else {
      x->p = nullptr;
      t = get_right(x);
      splay(t);
      t->r = y;
      y->p = t;
      update(t);
    }
    return t;
  }
};

template< template< typename > typename _Info, typename _LInfo >
struct SuperLinkCutTree {
  using LInfo = _LInfo;
  using Info = _Info< LInfo >;
private:
  struct Node {
    Node *l, *r, *p;
    Info info;
    typename SplayTree< LInfo >::Node *light, *belong;

    bool is_root() const {
      return not p or (p->l != this and p->r != this);
    }

    explicit Node(const Info &info)
        : info(info), l(nullptr), r(nullptr), p(nullptr),
          light(nullptr), belong(nullptr) {}
  };

public:
  using NP = Node *;
  SplayTree< LInfo > splay_tree;

private:
  const Info e;

  void rotr(NP t) {
    NP x = t->p, y = x->p;
    if((x->l = t->r)) t->r->p = x;
    t->r = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
      update(y);
    }
  }

  void rotl(NP t) {
    NP x = t->p, y = x->p;
    if((x->r = t->l)) t->l->p = x;
    t->l = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
      update(y);
    }
  }

public:
  SuperLinkCutTree() : e{Info()}, splay_tree{} {}

  const Info &get_info(NP t) {
    return t ? t->info : e;
  }

  void update(NP t) {
    t->info.update(get_info(t->l), get_info(t->r), splay_tree.get_info(t->light));
  }

  void splay(NP t) {
    {
      NP rot = t;
      while(not rot->is_root()) rot = rot->p;
      t->belong = rot->belong;
      if(t != rot) rot->belong = nullptr;
    }
    while(not t->is_root()) {
      NP q = t->p;
      if(q->is_root()) {
        if(q->l == t) rotr(t);
        else rotl(t);
      } else {
        NP r = q->p;
        if(r->l == q) {
          if(q->l == t) rotr(q), rotr(t);
          else rotl(t), rotr(t);
        } else {
          if(q->r == t) rotl(q), rotl(t);
          else rotr(t), rotl(t);
        }
      }
    }
  }

  NP expose(NP t) {
    NP rp = nullptr;
    for(NP cur = t; cur; cur = cur->p) {
      splay(cur);
      if(cur->r) {
        cur->light = splay_tree.insert(cur->light, cur->r->info.link());
        cur->r->belong = cur->light;
      }
      cur->r = rp;
      if(cur->r) {
        splay_tree.splay(cur->r->belong);
        cur->light = splay_tree.erase(cur->r->belong);
      }
      update(cur);
      rp = cur;
    }
    splay(t);
    return rp;
  }

  void link(NP child, NP parent) {
    expose(parent);
    expose(child);
    child->p = parent;
    parent->r = child;
    update(parent);
  }

  void cut(NP child) {
    expose(child);
    NP parent = child->l;
    child->l = nullptr;
    parent->p = nullptr;
    update(child);
  }

  NP alloc(const Info &info) {
    NP t = new Node(info);
    update(t);
    return t;
  }

  bool is_connected(NP u, NP v) {
    expose(u), expose(v);
    return u == v or u->p;
  }

  vector< NP > build(vector< Info > &vs) {
    vector< NP > nodes(vs.size());
    for(int i = 0; i < (int) vs.size(); i++) {
      nodes[i] = alloc(vs[i]);
    }
    return nodes;
  }

  NP lca(NP u, NP v) {
    if(not is_connected(u, v)) return nullptr;
    expose(u);
    return expose(v);
  }

  void set_key(NP t, const Info &v) {
    expose(t);
    t->info = move(v);
    update(t);
  }

  const Info &query(NP u) {
    expose(u);
    return get_info(u);
  }

  Info query_subtree(NP u) {
    expose(u);
    NP l = u->l;
    u->l = nullptr;
    update(u);
    auto ret = u->info;
    u->l = l;
    update(u);
    return ret;
  }
};


using T = int;

// Light-edge の情報
struct LInfo {
  T left, ans;

  T p_left, p_ans;

  // 単位元(キーの値はアクセスしないので未初期化でもよい
  LInfo() : p_left(-1), p_ans(-1) {}

  // 初期化
  LInfo(T left, T ans) : left(left), ans(ans) {}

  // l, r は Splay-tree の子 (原理上、各ノード区別はない)
  void update(const LInfo &l, const LInfo &r) {
    p_left = left, p_ans = ans;
    if(chmax(p_left, l.p_left)) p_ans = l.p_ans;
    if(chmax(p_left, r.p_left)) p_ans = r.p_ans;
  }
};

// Heavy-edge の情報
template< typename LInfo >
struct Info {
  T v, idx;

  T p_ans, p_light, p_left;

  T c_ans, c_light, c_left;

  // 単位元(キーの値はアクセスしないので未初期化でもよい
  Info() : p_ans(-1), p_light(-1), p_left(-1), c_ans(-1), c_light(-1), c_left(-1) {}

  // 初期化
  Info(T idx, T v) : v(v), idx(idx) {}

  // pが親, cがheavy-edgeで結ばれた子, lがそれ以外の子
  void update(const Info &p, const Info &c, const LInfo &l) {
    p_left = ~p.p_left ? p.p_left : v;
    if(p.p_light < v) {
      if(l.p_left < c.p_left) {
        p_light = c.p_light;
        p_ans = c.p_ans;
      } else if(~l.p_left) {
        p_light = ~c.p_left ? inf : l.p_left;
        p_ans = l.p_ans;
      } else {
        p_light = -1;
        p_ans = idx;
      }
    } else {
      p_light = inf;
      p_ans = p.p_ans;
    }

    c_left = ~c.c_left ? c.c_left : v;
    if(c.c_light < v) {
      if(l.p_left < p.c_left) {
        c_light = p.c_light;
        c_ans = p.c_ans;
      } else if(~l.p_left) {
        c_light = ~p.c_left ? inf : l.p_left;
        c_ans = l.p_ans;
      } else {
        c_light = -1;
        c_ans = idx;
      }
    } else {
      c_light = inf;
      c_ans = c.c_ans;
    }
  }

  // 親と light-edge で繋げる
  LInfo link() const { return LInfo(p_left, p_ans); }
};

using LCT = SuperLinkCutTree< Info, LInfo >;

/**
 * @brief Scanner(高速入力)
 */
struct Scanner {
public:

  explicit Scanner(FILE *fp) : fp(fp) {}

  template< typename T, typename... E >
  void read(T &t, E &... e) {
    read_single(t);
    read(e...);
  }

private:
  static constexpr size_t line_size = 1 << 16;
  static constexpr size_t int_digits = 20;
  char line[line_size + 1] = {};
  FILE *fp = nullptr;
  char *st = line;
  char *ed = line;

  void read() {}

  static inline bool is_space(char c) {
    return c <= ' ';
  }

  void reread() {
    ptrdiff_t len = ed - st;
    memmove(line, st, len);
    char *tmp = line + len;
    ed = tmp + fread(tmp, 1, line_size - len, fp);
    *ed = 0;
    st = line;
  }

  void skip_space() {
    while(true) {
      if(st == ed) reread();
      while(*st && is_space(*st)) ++st;
      if(st != ed) return;
    }
  }

  template< typename T, enable_if_t< is_integral< T >::value, int > = 0 >
  void read_single(T &s) {
    skip_space();
    if(st + int_digits >= ed) reread();
    bool neg = false;
    if(is_signed< T >::value && *st == '-') {
      neg = true;
      ++st;
    }
    typename make_unsigned< T >::type y = *st++ - '0';
    while(*st >= '0') {
      y = 10 * y + *st++ - '0';
    }
    s = (neg ? -y : y);
  }

  template< typename T, enable_if_t< is_same< T, string >::value, int > = 0 >
  void read_single(T &s) {
    s = "";
    skip_space();
    while(true) {
      char *base = st;
      while(*st && !is_space(*st)) ++st;
      s += string(base, st);
      if(st != ed) return;
      reread();
    }
  }

  template< typename T >
  void read_single(vector< T > &s) {
    for(auto &d: s) read(d);
  }
};

/**
 * @brief Printer(高速出力)
 */
struct Printer {
public:
  explicit Printer(FILE *fp) : fp(fp) {}

  ~Printer() { flush(); }

  template< bool f = false, typename T, typename... E >
  void write(const T &t, const E &... e) {
    if(f) write_single(' ');
    write_single(t);
    write< true >(e...);
  }

  template< typename... T >
  void writeln(const T &...t) {
    write(t...);
    write_single('\n');
  }

  void flush() {
    fwrite(line, 1, st - line, fp);
    st = line;
  }

private:
  FILE *fp = nullptr;
  static constexpr size_t line_size = 1 << 16;
  static constexpr size_t int_digits = 20;
  char line[line_size + 1] = {};
  char small[32] = {};
  char *st = line;

  template< bool f = false >
  void write() {}

  void write_single(const char &t) {
    if(st + 1 >= line + line_size) flush();
    *st++ = t;
  }

  template< typename T, enable_if_t< is_integral< T >::value, int > = 0 >
  void write_single(T s) {
    if(st + int_digits >= line + line_size) flush();
    if(s == 0) {
      write_single('0');
      return;
    }
    if(s < 0) {
      write_single('-');
      s = -s;
    }
    char *mp = small + sizeof(small);
    typename make_unsigned< T >::type y = s;
    size_t len = 0;
    while(y > 0) {
      *--mp = y % 10 + '0';
      y /= 10;
      ++len;
    }
    memmove(st, mp, len);
    st += len;
  }

  void write_single(const string &s) {
    for(auto &c: s) write_single(c);
  }

  void write_single(const char *s) {
    while(*s != 0) write_single(*s++);
  }

  template< typename T >
  void write_single(const vector< T > &s) {
    for(size_t i = 0; i < s.size(); i++) {
      if(i) write_single(' ');
      write_single(s[i]);
    }
  }
};

int main() {
  Scanner in(stdin);
  Printer out(stdout);
  int N, Q;
  in.read(N);
  vector< int > A(N);
  LCT lct;
  vector< LCT::NP > vs(N);
  for(int i = 0; i < N; i++) {
    A[i] = i;
    vs[i] = lct.alloc(Info< LInfo >(i + 1, A[i]));
  }
  vector< vector< int > > g(N);
  for(int i = 1; i < N; i++) {
    int a, b;
    in.read(a, b);
    --a, --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  MFP([&](auto dfs, int idx, int par) -> void {
    for(auto to: g[idx]) {
      if(to != par) {
        lct.link(vs[to], vs[idx]);
        dfs(to, idx);
      }
    }
  })(0, -1);
  in.read(Q);
  int x = 0;
  while(Q--) {
    int u, v;
    in.read(u, v);
    --u, --v;
    u += x;
    v += x;
    u %= N;
    v %= N;
    swap(A[u], A[v]);
    lct.set_key(vs[v], Info< LInfo >(v + 1, A[v]));
    lct.set_key(vs[u], Info< LInfo >(u + 1, A[u]));
    out.writeln(x = lct.query(vs[u]).c_ans);
  }
}
0