-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrow.cpp
51 lines (48 loc) · 1.08 KB
/
row.cpp
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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6;
int a[N], b[N], id[N];
int bef[N];
bool cmpA(int x, int y) {
return (a[x] < a[y]);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &x), a[x-1] = i;
for (int i = 0; i < n; i++)
scanf("%d", &x), b[x-1] = i, id[i] = i;
sort(id, id+n, cmpA);
set< pair< int, int > > st;
memset(bef, -1, sizeof bef);
for (int i = 0; i < n; i++) {
int now = id[i];
auto it = st.lower_bound(make_pair(-b[now], now));
if (it != st.end()) {
bef[now] = it->second;
st.erase(it);
}
st.insert(make_pair(-b[now], now));
}
printf("%d\n", st.size());
for (auto it : st) {
stack < int > stak;
int now = it.second;
while (now >= 0) {
stak.push(now);
now = bef[now];
}
printf("%d", stak.size());
while (!stak.empty()) {
printf(" %d", stak.top() + 1);
stak.pop();
}
printf("\n");
}
}
return 0;
}