#include <bits/stdc++.h>
#include "hieroglyphs.h"
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const int II = 2 * 1000 * 1000 * 1000;
const int N = 300 * 1000 + 7;
int tab[N][2], nxt[N][2], rev[N];
map<int, int> scl;
vector<int> pos[N][2];
set<int>::iterator it;
int Il(int a, int i, int r)
{
//cout << "Il: " << a << " " << i << " " << r << " " << (pos[a][r].size()) << "\n";
return (pos[a][r].end() - lower_bound(pos[a][r].begin(), pos[a][r].end(), i));
}
int Nxt(int a, int i, int r)
{
int j = (upper_bound(pos[a][r].begin(), pos[a][r].end(), i) - pos[a][r].begin());
if(j == (int)pos[a][r].size())
return N - 3;
return pos[a][r][j];
}
void Scl(int n, int m)
{
vector<int> v;
for(int i = 1; i <= n; ++i)
v.pb(tab[i][0]);
for(int i = 1; i <= m; ++i)
v.pb(tab[i][1]);
sort(v.begin(), v.end());
int l = -1;
for(int i = 0; i < (int)v.size(); ++i)
{
if(i == 0 || v[i] != v[i - 1]) ++l;
scl[v[i]] = l; rev[l] = v[i];
}
for(int i = 1; i <= n; ++i)
{
tab[i][0] = scl[tab[i][0]];
pos[tab[i][0]][0].pb(i);
}
for(int i = 1; i <= m; ++i)
{
tab[i][1] = scl[tab[i][1]];
pos[tab[i][1]][1].pb(i);
}
}
void Gen(int n, int m)
{
nxt[n + 1][0] = n + 1;
nxt[m + 1][1] = m + 1;
for(int i = n; i >= 1; --i)
{
nxt[i][0] = nxt[i + 1][0];
if((int)pos[tab[i][0]][0].size() <= (int)pos[tab[i][0]][1].size())
nxt[i][0] = i;
}
for(int i = m; i >= 1; --i)
{
nxt[i][1] = nxt[i + 1][1];
if((int)pos[tab[i][1]][1].size() <= (int)pos[tab[i][1]][0].size())
nxt[i][1] = i;
}
nxt[0][0] = nxt[1][0];
nxt[0][1] = nxt[1][1];
}
vector<int> ucs(vector<int> A, vector<int> B)
{
int n = A.size(), m = B.size();
vector<int> answer, fans = {-1};
for(int i = 0; i < n; ++i)
tab[i + 1][0] = A[i];
for(int i = 0; i < m; ++i)
tab[i + 1][1] = B[i];
Scl(n, m);
Gen(n, m);
tab[n + 1][0] = N - 6; tab[m + 1][1] = N - 5;
int i = 0, j = 0;
while(i < n && j < m)
{
int a = tab[nxt[i + 1][0]][0], b = tab[nxt[j + 1][1]][1];
if(nxt[i + 1][0] > n && nxt[j + 1][1] > m) break;
if(a == b)
{
answer.pb(rev[a]);
i = nxt[i + 1][0]; j = nxt[j + 1][1];
continue;
}
int il1 = Il(a, i + 1, 0), il2 = Il(b, j + 1, 1);
//int n1 = Nxt(a, 1, nxt[j + 1][1]), n2 = Nxt(b, 0, nxt[i + 1][0]);
bool cz1 = (Nxt(a, j, 1) < nxt[j + 1][1] && Il(b, nxt[i + 1][0], 0) >= il2);
bool cz2 = (Nxt(b, i, 0) < nxt[i + 1][0] && Il(a, nxt[j + 1][1], 1) >= il1);
//cout << a << " " << b << "\n";
//cout << i << " " << j << " " << nxt[i + 1][0] << " " << nxt[j + 1][1] << "\n";
//cout << Il(b, nxt[i + 1][0], 0) << " " << Il(a, nxt[j + 1][1], 1) << "\n";
//cout << il1 << " " << il2 << "\n";
//cout << cz1 << " " << cz2 << "\n";
if(!(cz1 ^ cz2))
return fans;
if(cz1)
{
i = nxt[i + 1][0];
j = Nxt(a, j, 1);
}else
{
i = Nxt(b, i, 0);
j = nxt[j + 1][1];
}
answer.pb(rev[tab[i][0]]);
}
int anss = 0;
for(int i = 0; i <= n + m; ++i)
anss += (int)min(pos[i][0].size(), pos[i][1].size());
if(anss != (int)answer.size())
return fans;
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
4 ms |
19024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
4 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19024 KB |
Output is correct |
5 |
Correct |
155 ms |
33448 KB |
Output is correct |
6 |
Correct |
133 ms |
32684 KB |
Output is correct |
7 |
Correct |
129 ms |
32748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
4 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19024 KB |
Output is correct |
5 |
Correct |
155 ms |
33448 KB |
Output is correct |
6 |
Correct |
133 ms |
32684 KB |
Output is correct |
7 |
Correct |
129 ms |
32748 KB |
Output is correct |
8 |
Correct |
106 ms |
28488 KB |
Output is correct |
9 |
Correct |
110 ms |
28668 KB |
Output is correct |
10 |
Correct |
105 ms |
28668 KB |
Output is correct |
11 |
Correct |
106 ms |
28600 KB |
Output is correct |
12 |
Correct |
114 ms |
28660 KB |
Output is correct |
13 |
Correct |
115 ms |
28672 KB |
Output is correct |
14 |
Correct |
108 ms |
28668 KB |
Output is correct |
15 |
Correct |
106 ms |
28640 KB |
Output is correct |
16 |
Correct |
106 ms |
28680 KB |
Output is correct |
17 |
Correct |
105 ms |
28672 KB |
Output is correct |
18 |
Correct |
4 ms |
19040 KB |
Output is correct |
19 |
Correct |
4 ms |
19024 KB |
Output is correct |
20 |
Correct |
4 ms |
19024 KB |
Output is correct |
21 |
Correct |
6 ms |
19280 KB |
Output is correct |
22 |
Correct |
69 ms |
26256 KB |
Output is correct |
23 |
Correct |
117 ms |
31216 KB |
Output is correct |
24 |
Correct |
123 ms |
31408 KB |
Output is correct |
25 |
Correct |
116 ms |
31212 KB |
Output is correct |
26 |
Correct |
100 ms |
27980 KB |
Output is correct |
27 |
Correct |
104 ms |
28668 KB |
Output is correct |
28 |
Correct |
114 ms |
28668 KB |
Output is correct |
29 |
Incorrect |
99 ms |
28668 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
22708 KB |
Output is correct |
2 |
Correct |
37 ms |
22964 KB |
Output is correct |
3 |
Correct |
32 ms |
22260 KB |
Output is correct |
4 |
Correct |
32 ms |
22248 KB |
Output is correct |
5 |
Correct |
34 ms |
22624 KB |
Output is correct |
6 |
Correct |
15 ms |
20760 KB |
Output is correct |
7 |
Correct |
3 ms |
19024 KB |
Output is correct |
8 |
Correct |
3 ms |
19024 KB |
Output is correct |
9 |
Correct |
3 ms |
19052 KB |
Output is correct |
10 |
Correct |
3 ms |
19024 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
3 ms |
19244 KB |
Output is correct |
13 |
Correct |
3 ms |
19024 KB |
Output is correct |
14 |
Incorrect |
3 ms |
19024 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
28488 KB |
Output is correct |
2 |
Correct |
110 ms |
28668 KB |
Output is correct |
3 |
Correct |
105 ms |
28668 KB |
Output is correct |
4 |
Correct |
106 ms |
28600 KB |
Output is correct |
5 |
Correct |
114 ms |
28660 KB |
Output is correct |
6 |
Correct |
115 ms |
28672 KB |
Output is correct |
7 |
Correct |
108 ms |
28668 KB |
Output is correct |
8 |
Correct |
106 ms |
28640 KB |
Output is correct |
9 |
Correct |
106 ms |
28680 KB |
Output is correct |
10 |
Correct |
105 ms |
28672 KB |
Output is correct |
11 |
Correct |
4 ms |
19040 KB |
Output is correct |
12 |
Correct |
4 ms |
19024 KB |
Output is correct |
13 |
Correct |
4 ms |
19024 KB |
Output is correct |
14 |
Correct |
6 ms |
19280 KB |
Output is correct |
15 |
Correct |
69 ms |
26256 KB |
Output is correct |
16 |
Correct |
117 ms |
31216 KB |
Output is correct |
17 |
Correct |
123 ms |
31408 KB |
Output is correct |
18 |
Correct |
116 ms |
31212 KB |
Output is correct |
19 |
Correct |
100 ms |
27980 KB |
Output is correct |
20 |
Correct |
30 ms |
22708 KB |
Output is correct |
21 |
Correct |
37 ms |
22964 KB |
Output is correct |
22 |
Correct |
32 ms |
22260 KB |
Output is correct |
23 |
Correct |
32 ms |
22248 KB |
Output is correct |
24 |
Correct |
34 ms |
22624 KB |
Output is correct |
25 |
Correct |
15 ms |
20760 KB |
Output is correct |
26 |
Correct |
4 ms |
19024 KB |
Output is correct |
27 |
Correct |
4 ms |
19024 KB |
Output is correct |
28 |
Correct |
4 ms |
19024 KB |
Output is correct |
29 |
Correct |
4 ms |
19024 KB |
Output is correct |
30 |
Correct |
4 ms |
19276 KB |
Output is correct |
31 |
Correct |
4 ms |
19248 KB |
Output is correct |
32 |
Correct |
5 ms |
19024 KB |
Output is correct |
33 |
Correct |
5 ms |
19056 KB |
Output is correct |
34 |
Correct |
11 ms |
20244 KB |
Output is correct |
35 |
Correct |
36 ms |
23472 KB |
Output is correct |
36 |
Correct |
50 ms |
24256 KB |
Output is correct |
37 |
Correct |
38 ms |
23980 KB |
Output is correct |
38 |
Correct |
38 ms |
23732 KB |
Output is correct |
39 |
Correct |
39 ms |
23724 KB |
Output is correct |
40 |
Correct |
95 ms |
27644 KB |
Output is correct |
41 |
Correct |
78 ms |
25864 KB |
Output is correct |
42 |
Correct |
28 ms |
23144 KB |
Output is correct |
43 |
Correct |
28 ms |
22964 KB |
Output is correct |
44 |
Correct |
30 ms |
23220 KB |
Output is correct |
45 |
Correct |
33 ms |
23140 KB |
Output is correct |
46 |
Correct |
86 ms |
25928 KB |
Output is correct |
47 |
Correct |
83 ms |
26252 KB |
Output is correct |
48 |
Correct |
81 ms |
26036 KB |
Output is correct |
49 |
Correct |
87 ms |
25620 KB |
Output is correct |
50 |
Correct |
82 ms |
25616 KB |
Output is correct |
51 |
Correct |
76 ms |
25520 KB |
Output is correct |
52 |
Correct |
86 ms |
25596 KB |
Output is correct |
53 |
Correct |
81 ms |
25596 KB |
Output is correct |
54 |
Correct |
79 ms |
25596 KB |
Output is correct |
55 |
Correct |
18 ms |
20800 KB |
Output is correct |
56 |
Correct |
48 ms |
23740 KB |
Output is correct |
57 |
Correct |
48 ms |
23540 KB |
Output is correct |
58 |
Correct |
56 ms |
23980 KB |
Output is correct |
59 |
Correct |
69 ms |
24268 KB |
Output is correct |
60 |
Correct |
85 ms |
24868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
3 ms |
19052 KB |
Output is correct |
4 |
Correct |
3 ms |
19024 KB |
Output is correct |
5 |
Correct |
3 ms |
19024 KB |
Output is correct |
6 |
Correct |
3 ms |
19244 KB |
Output is correct |
7 |
Correct |
3 ms |
19024 KB |
Output is correct |
8 |
Incorrect |
3 ms |
19024 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
4 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19024 KB |
Output is correct |
5 |
Correct |
3 ms |
19024 KB |
Output is correct |
6 |
Correct |
4 ms |
19024 KB |
Output is correct |
7 |
Correct |
4 ms |
19024 KB |
Output is correct |
8 |
Correct |
155 ms |
33448 KB |
Output is correct |
9 |
Correct |
133 ms |
32684 KB |
Output is correct |
10 |
Correct |
129 ms |
32748 KB |
Output is correct |
11 |
Correct |
106 ms |
28488 KB |
Output is correct |
12 |
Correct |
110 ms |
28668 KB |
Output is correct |
13 |
Correct |
105 ms |
28668 KB |
Output is correct |
14 |
Correct |
106 ms |
28600 KB |
Output is correct |
15 |
Correct |
114 ms |
28660 KB |
Output is correct |
16 |
Correct |
115 ms |
28672 KB |
Output is correct |
17 |
Correct |
108 ms |
28668 KB |
Output is correct |
18 |
Correct |
106 ms |
28640 KB |
Output is correct |
19 |
Correct |
106 ms |
28680 KB |
Output is correct |
20 |
Correct |
105 ms |
28672 KB |
Output is correct |
21 |
Correct |
4 ms |
19040 KB |
Output is correct |
22 |
Correct |
4 ms |
19024 KB |
Output is correct |
23 |
Correct |
4 ms |
19024 KB |
Output is correct |
24 |
Correct |
6 ms |
19280 KB |
Output is correct |
25 |
Correct |
69 ms |
26256 KB |
Output is correct |
26 |
Correct |
117 ms |
31216 KB |
Output is correct |
27 |
Correct |
123 ms |
31408 KB |
Output is correct |
28 |
Correct |
116 ms |
31212 KB |
Output is correct |
29 |
Correct |
100 ms |
27980 KB |
Output is correct |
30 |
Correct |
104 ms |
28668 KB |
Output is correct |
31 |
Correct |
114 ms |
28668 KB |
Output is correct |
32 |
Incorrect |
99 ms |
28668 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |