题目链接:传送门

Description

给出两个长度为n的数组A,B。求一个子序列,使得子序列中A数组单调不降,B数组单调不升

Solution

本质就是个三维偏序 可以用CDQ分治+树状数组过掉, 但是由于我对Splay情有独钟想练一下Splay,就写的树状数组套Splay 开始写完之后调好久以为是处理权值相同的情况的时候有问题,于是一顿魔改(即读入数据之后的那十几行代码), 把原数组乱搞成了一个元素互不相同的数组, 后来发现是Splay里面 写错了, 稍微改了下A了之后就懒得把魔改部分改回去了

Code

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
#include <bits/stdc++.h>
#define lowbit(x) x & (-x)

using namespace std;

const int Maxn = 5e4 + 10;

int N;
int A[Maxn], B[Maxn], len1, len2;
int Next[Maxn];
int Dp[Maxn], Root[Maxn];

struct node
{
int val, id;
}X[Maxn], Y[Maxn];
int tmp1[Maxn], tmp2[Maxn];

inline int cmp1 (node a, node b)
{
if (a.val == b.val) return a.id > b.id;
return a.val < b.val;
}

inline int cmp2 (node a, node b)
{
return a.id < b.id;
}

inline int cmp3 (node a, node b)
{
if (a.val == b.val) return a.id < b.id;
return a.val < b.val;
}


struct tree
{
int maxv, maxid, id, val, ch[2], dp, fa;
};

namespace Splay
{
tree Tree[Maxn * 16];
int cnt;

inline int judge_dir (int x)
{
return Tree[Tree[x].fa].ch[1] == x;
}

inline void chkmax (tree &x, int DP, int ID)
{
if (DP > x.maxv) x.maxv = DP, x.maxid = ID;
else if (DP == x.maxv && ID < x.maxid) x.maxid = ID;
}

inline void update (int x)
{
Tree[x].maxv = Tree[x].dp;
Tree[x].maxid = Tree[x].id;
if (Tree[x].ch[0]) chkmax (Tree[x], Tree[Tree[x].ch[0]].maxv, Tree[Tree[x].ch[0]].maxid);
if (Tree[x].ch[1]) chkmax (Tree[x], Tree[Tree[x].ch[1]].maxv, Tree[Tree[x].ch[1]].maxid);
}

inline void connect (int x, int f, int dir)
{
Tree[x].fa = f;
Tree[f].ch[dir] = x;
}

inline void rotate (int x)
{
int f = Tree[x].fa;
int dirx = judge_dir(x);
int anc = Tree[f].fa;
int dirf = judge_dir(f);
connect(Tree[x].ch[dirx ^ 1], f, dirx);
connect(f, x, (dirx ^ 1));
connect(x, anc, dirf);
update(f), update(x);
}

inline void splay (int x, int y, int &root)
{
while (Tree[x].fa != y)
{
int f = Tree[x].fa;
int dirx = judge_dir(x), dirf = judge_dir(f);
if (Tree[f].fa == y) rotate(x);
else if (dirx == dirf) rotate(f), rotate(x);
else rotate(x), rotate(x);
}
if (!y) root = x;
}

inline void Newnode (int val, int DP, int ID, int f)
{
Tree[++cnt].val = val;
Tree[cnt].maxv = Tree[cnt].dp = DP;
Tree[cnt].maxid = Tree[cnt].id = ID;
Tree[cnt].fa = f;
}

inline void insert (int &root, int val, int DP, int ID)
{
if (!root)
{
Newnode(val, DP, ID, 0);
splay (cnt, 0, root);
return ;
}
int now = root;
while (1)
{
if (Tree[now].val == val)
{
/*
tree tmp;
tmp.maxv = Tree[now].dp;
tmp.maxid = Tree[now].id;
chkmax (tmp, DP, ID);
Tree[now].dp = tmp.maxv;
Tree[now].id = tmp.maxid;
splay (now, 0, root);
*/
/*
int tmp = Tree[now].ch[1];
while (Tree[tmp].ch[0]) tmp = Tree[tmp].ch[0];
Newnode(val, DP, ID, tmp);
Tree[tmp].ch[0] = cnt;
splay (cnt, 0, root);
*/
return ;
}
int dir = (val > Tree[now].val);
if (!Tree[now].ch[dir])
{
Newnode(val, DP, ID, now);
Tree[now].ch[dir] = cnt;
splay (cnt, 0, root);
return ;
}
now = Tree[now].ch[dir];
}
}

inline void query (int root, int val, tree &save)
{
int now = root;
while (1)
{
//cout<<root<<" "<<val<<" "<<now<<" "<<Tree[now].val<<endl;
if (!now) return ;
if (Tree[now].val < val)
{
now = Tree[now].ch[1];
continue;
}
chkmax (save, Tree[now].dp, Tree[now].id);
if (Tree[now].ch[1]) chkmax (save, Tree[Tree[now].ch[1]].maxv, Tree[Tree[now].ch[1]].maxid);
now = Tree[now].ch[0];
}
}

}

namespace BIT
{
inline void add (int x, int y, int DP, int ID)
{
while (x <= N)
{
Splay :: insert (Root[x], y, DP, ID);
x += lowbit(x);
}
}

inline void query (int x, int y, int &DP, int &ID)
{
tree newnode;
newnode.maxv = DP;
newnode.maxid = ID;
while (x)
{
Splay :: query (Root[x], y, newnode);
x -= lowbit(x);
}
DP = newnode.maxv;
ID = newnode.maxid;
}
}

inline void Init ()
{
memset(Dp, 0, sizeof Dp);
memset(Root, 0, sizeof Root);
memset(Splay :: Tree, 0, sizeof Splay :: Tree);
Splay :: cnt = 0;
}

int main()
{
#ifdef hk_cnyali
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
while (~scanf("%d", &N))
{
Init();
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]), X[i].val = A[i], X[i].id = i;
for (int i = 1; i <= N; ++i) scanf("%d", &B[i]), Y[i].val = B[i], Y[i].id = i;
sort(X + 1, X + N + 1, cmp1);
sort(Y + 1, Y + N + 1, cmp3);
for (int i = 2; i <= N; ++i)
if (X[i].val <= X[i - 1].val)
X[i].val = X[i - 1].val + 1;
for (int i = 2; i <= N; ++i)
if (Y[i].val <= Y[i - 1].val)
Y[i].val = Y[i - 1].val + 1;
sort(X + 1, X + N + 1, cmp2);
sort(Y + 1, Y + N + 1, cmp2);
for (int i = 1; i <= N; ++i) A[i] = X[i].val;
for (int i = 1; i <= N; ++i) B[i] = Y[i].val;
sort(X + 1, X + N + 1, cmp1);
sort(Y + 1, Y + N + 1, cmp3);
for (int i = 1; i <= N; ++i) tmp1[i] = X[i].val;
for (int i = 1; i <= N; ++i) tmp2[i] = Y[i].val;
for (int i = 1; i <= N; ++i) A[i] = lower_bound(tmp1 + 1, tmp1 + N + 1, A[i]) - tmp1;
for (int i = 1; i <= N; ++i) B[i] = lower_bound(tmp2 + 1, tmp2 + N + 1, B[i]) - tmp2;
//for (int i = 1; i <= N; ++i) cout<<A[i]<<" ";
//cout<<endl;
//for (int i = 1; i <= N; ++i) cout<<B[i]<<" ";
//cout<<endl;
int Ans = 0, now = INT_MAX;
for (int i = N; i >= 1; --i)
{
int ID = INT_MAX, DP = 0;
BIT :: query (A[i], B[i], DP, ID);
//cout<<A[i]<<" "<<B[i]<<endl;
//cout<<DP<<" "<<ID<<endl;
Dp[i] = DP + 1;
Next[i] = ID;
BIT :: add (A[i], B[i], Dp[i], i);
if (Dp[i] >= Ans)
{
now = i;
Ans = Dp[i];
}
}
//for (int i = 1; i <= N; ++i) cout<<Dp[i]<<" ";cout<<endl;
//for (int i = 1; i <= N; ++i) cout<<Next[i]<<" ";cout<<endl;
cout<<Ans<<endl;
while (now != INT_MAX)
{
cout<<now;
now = Next[now];
if (now != INT_MAX) cout<<" ";
}
cout<<endl;
}
return 0;
}