多校训练营8

G:Lexicographic Comparison

z3475

标签

Splay;平衡树;线段树

题意

最开始是排列组成的数列。

你需要执行q次操作,每次操作可能如下

  1. 交换
  2. 交换
  3. 字典序比较
思路
  1. 将所有看成图中的一条边,则是由多个环组成的图

证明略

对于任意的值,就是找到所属的环,向后走步的对应的

字段序比较的话,考虑相等的情况,考虑到上段,这相等当且仅当。满足这个条件下环的所有元素都应相等,而我们只需要考虑前缀相等即可,那就意味着可以只让环内最小的元素代表这个环。

现在问题变成了快速找到所有数字满足的数列前缀。而当且仅当,用线段树维护。

考虑维护这一堆环,对于以及2操作,我们要维护环的拆分和合并问题。环从任意一个地方断开那就是全序,可以用平衡树维护。(这里平衡树维护的key并不是某个特定的整数,这个key蕴含在平衡树的结构中)

参考资料

变成的操作可以让分割然后再让左边合并到右边末尾实现。(虽然我不是这么干的

代码
参考代码
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <bits/stdc++.h>
using namespace std;
struct FAST_IO {
  FAST_IO() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  }
} __fast_io;
#define DEF_NUM(num)           \
  using i##num = int##num##_t; \
  using u##num = uint##num##_t;
DEF_NUM(8) DEF_NUM(16) DEF_NUM(32) DEF_NUM(64) using i128 = __int128;
using u128 = unsigned __int128;

bool MULTIDATA = true;

int T;
#define N (1000000 + 10)
namespace splay {
struct node {
  node *ch[2], *fa;
  int val, minn, size;
  bool chi() { return fa->ch[1] == this; }
  node*& chf() { return fa->ch[chi()]; }
  void maintain() {
    minn = min({val, ch[0]->minn, ch[1]->minn});
    size = 1 + ch[0]->size + ch[1]->size;
  }
};
node nilnode{{&nilnode, &nilnode}, &nilnode, -1, 100000000, 0};
node* nil = &nilnode;
node d[N];
void rotate(node* a, bool d, node* b) {
  node* c = b->ch[!d];
  if (a->fa) {
    a->chf() = b;
  }
  tie(a->ch[d], b->ch[!d]) = make_tuple(c, a);
  tie(a->fa, b->fa, c->fa) = make_tuple(b, a->fa, a);
  a->maintain();
  b->maintain();
}
bool uprotate(node* b) {
  if (!b->fa)
    return false;
  rotate(b->fa, b->chi(), b);
  return true;
}
bool downrotate(node* a, bool d) {
  if (a->ch[!d] == nil)
    return false;
  rotate(a, !d, a->ch[!d]);
  return true;
}
void init(int n) {
  for (int i = 1; i <= n; i++) {
    d[i] = {{nil, nil}, NULL, i, i, 1};
  }
}

void splay(node* e) {
  while (e->fa) {
    uprotate(e);
  }
}

node* _kth(node* a, int i) {  // 1-indexed
  assert(a != nil);
  if (i <= a->ch[0]->size)
    return _kth(a->ch[0], i);
  if (i == a->ch[0]->size + 1)
    return a;
  return _kth(a->ch[1], i - a->ch[0]->size - 1);
}
node* kth(node* a, i64 i) {  // loop handler
                             //  dprint(a);
  return _kth(a, (i - 1) % a->size + 1);
}
node* spin(node* a, bool d) {
  node* b;
  while (true) {
    b = a->ch[!d];
    if (b == nil)
      break;
    rotate(a, !d, b);
    a = b;
  }
  return a;
}
pair<node*, bool> sspin(node* a, bool d1, bool d2) {
  // d1==d2 d1==0 insert_front 1 insert_back
  // d1!=d2 d1==0
  node* as = a->ch[d1];
  if (as == nil)
    return {a, d1};
  as = spin(as, d2);
  return {as, !d2};
}

void split(node* a, node* b) {
  // if (a == b) return;
  // splay(b);
  // splay(a);
  bool f = b->chi();
  if (f) {
    tie(a->ch[f], b->ch[f], b->ch[!f]) = make_tuple(b->ch[f], b->ch[!f], nil);
    a->ch[f]->fa = a;
    b->fa = NULL;
    b->ch[f]->fa = b;
  } else {
    std::swap(a->ch[1], b->ch[1]);
    a->ch[1]->fa = a;
    b->ch[1]->fa = b;
    a->ch[0] = nil;
    b->fa = NULL;
  }
  a->maintain();
  b->maintain();
}

void merge(node* a, node* b) {
  // if (a==b) return ;
  // splay(b);
  // splay(a);
  auto ass = sspin(a, 1, 1);
  node* bs = sspin(b, 1, 0).first;
  if (bs != b) {
    tie(bs->ch[1], b->ch[1]) = make_tuple(b, nil);
    tie(bs->fa, b->fa) = make_tuple((node*)NULL, bs);
    b->maintain();
    bs->maintain();
  }
  ass.first->ch[ass.second] = bs;
  bs->fa = ass.first;
  ass.first->maintain();
  if (ass.first != a)
    a->maintain();
}

bool swap(node* a, node* b) {
  // splay(b);
  // splay(a);
  if (b->fa == a) {
    split(a, b);
    return true;
  } else {
    merge(a, b);
    return false;
  }
}
}  // namespace splay

namespace sgtree {
template <class T>
T gcd(T a, T b) {
  return b == 0 ? a : gcd(b, a % b);
}
i64 lcm(i64 a, i64 b) {
  i128 ans = (i128)a * b / gcd(a, b);
  if (ans >= numeric_limits<i64>::max())
    return numeric_limits<i64>::max();
  return ans;
}
i64 nodes[8 * N];
void maintain(int i) {
  nodes[i] = lcm(nodes[i << 1], nodes[i << 1 | 1]);
}
int base;
void init(int n) {
  base = 1;
  while (base < n + 1)
    base <<= 1;
  for (int i = 1; i < base * 2; i++)
    nodes[i] = 1;
}
void set(int i, int a) {
  i += base;
  nodes[i] = a;
  while (i >>= 1) {
    maintain(i);
  }
}
int next(int i) {
  if (i == 1)
    return 4 * N;
  while (i & 1)
    i >>= 1;
  return i ^ 1;
}
int search(i64 a, i64 b) {
  int i = 1;
  while (1) {
    if (i >= base * 2)
      break;
    if (a % nodes[i] == b % nodes[i])
      i = next(i);
    else if (i >= base)
      break;
    else
      i <<= 1;
  }
  return i - base;
}
};  // namespace sgtree

namespace a {
int arr[N];
void init(int n) {
  for (int i = 1; i <= n; i++)
    arr[i] = i;
}
void swap(int i, int j) {
  std::swap(arr[i], arr[j]);
}
int get(int i) {
  return arr[i];
}
};  // namespace a
int main() {
  cin >> T;
  while (T--) {
    int n, q;
    cin >> n >> q;
    splay::init(n);
    sgtree::init(n);
    a::init(n);
    string str;
    i64 x, y;
    while (q--) {
      cin >> str >> x >> y;
      if (str == "cmp") {
        int lst = sgtree::search(x, y);
        if (lst > n) {
          cout << "=\n";
          continue;
        }
        splay::node* a = &splay::d[lst];
        splay::splay(a);
        auto f = [&](i64 i, int j) {
          if (i == 0)
            return j;
          return splay::kth(a, a->ch[0]->size + 1 + i)->val;
        };
        int ai = f(x - 1, lst), bi = f(y - 1, lst);
        int aa = a::get(ai), ba = a::get(bi);
        if (aa < ba)
          cout << "<\n";
        else
          cout << ">\n";
      } else if (str == "swap_a") {
        a::swap(x, y);
      } else if (str == "swap_p") {
        if (x == y)
          continue;
        splay::node *a = &splay::d[x], *b = &splay::d[y];
        splay::splay(b);
        splay::splay(a);
        sgtree::set(a->minn, 1);
        sgtree::set(b->minn, 1);
        if (splay::swap(a, b)) {
          sgtree::set(b->minn, b->size);
          sgtree::set(a->minn, a->size);
        } else {
          sgtree::set(a->minn, a->size);
        }
      }
    }
  }
}

评论