#include "hieroglyphs.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double; // or double if tight TL
using str = string;
using pi = pair<int,int>;
#define mp make_pair
#define f first
#define s second
#define tcT template<class T
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vpi = V<pi>;
#define sz(x) int((x).size())
#define all(x) begin(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define pb push_back
#define ft front()
#define bk back()
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = 1e9+7;
const db PI = acos((db)-1);
mt19937 rng(0); // or mt19937_64
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; } // set a = max(a,b)
tcT, class U> T fstTrue(T lo, T hi, U f) {
++hi; assert(lo <= hi); // assuming f is increasing
while (lo < hi) { // find first index such that f is true
T mid = lo+(hi-lo)/2;
f(mid) ? hi = mid : lo = mid+1;
}
return lo;
}
const int mx = 200005;
/**
* Description: 1D point update, range query where \texttt{cmb} is
* any associative operation. If $N=2^p$ then \texttt{seg[1]==query(0,N-1)}.
* Time: O(\log N)
* Source:
* http://codeforces.com/blog/entry/18051
* KACTL
* Verification: SPOJ Fenwick
*/
tcT> struct SegTree { // cmb(ID,b) = b
const T ID = -MOD; T cmb(T a, T b) { return max(a, b); }
int n; V<T> seg;
void init(int _n) { // upd, query also work if n = _n
for (n = 1; n < _n; ) n *= 2;
seg.assign(2*n,ID); }
void pull(int p) { seg[p] = cmb(seg[2*p],seg[2*p+1]); }
void upd(int p, T val) { // set val at position p
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(int l, int r) { // associative op on [l, r]
T ra = ID, rb = ID;
if(l > r) return ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = cmb(ra,seg[l++]);
if (r&1) rb = cmb(seg[--r],rb);
}
return cmb(ra,rb);
}
/// int first_at_least(int lo, int val, int ind, int l, int r) { // if seg stores max across range
/// if (r < lo || val > seg[ind]) return -1;
/// if (l == r) return l;
/// int m = (l+r)/2;
/// int res = first_at_least(lo,val,2*ind,l,m); if (res != -1) return res;
/// return first_at_least(lo,val,2*ind+1,m+1,r);
/// }
};
bool checkSubseq(vi subseq, vi A){
int subseq_ind = 0;
for(auto u: A){
if(subseq_ind >= sz(subseq)) break;
if(u == subseq[subseq_ind]){
subseq_ind++;
}
}
if(subseq_ind >= sz(subseq)) return true;
return false;
}
vi ucs(vi A, vi B) {
int N = sz(A);
int M = sz(B);
vector<pair<int, vi>> special_As, special_Bs;
{
map<int, array<vi, 2>> at_poses;
for(int i = 0; i < N; i++){
at_poses[A[i]][0].pb(i);
}
for(int i = 0; i < M; i++){
at_poses[B[i]][1].pb(i);
}
for(auto u: at_poses){
if(sz(u.s[0]) == 0 || sz(u.s[1]) == 0) continue; //irrelevant
assert(sz(u.s[0]) == 1 || sz(u.s[1]) == 1);
if(sz(u.s[0]) == 1){
special_As.pb(mp(u.s[0][0], u.s[1]));
}
else{
special_Bs.pb(mp(u.s[1][0], u.s[0]));
}
}
}
sort(all(special_As));
sort(all(special_Bs));
int A_ind = 0;
int B_ind = 0;
vector<pair<char, int>> ord;
while(A_ind < sz(special_As) || B_ind < sz(special_Bs)){
if(A_ind == sz(special_As)){
ord.pb(mp('B', B_ind)); B_ind++; continue;
}
if(B_ind == sz(special_Bs)){
ord.pb(mp('A', A_ind)); A_ind++; continue;
}
bool A_before = special_As[A_ind].f < special_Bs[B_ind].s.bk && special_As[A_ind].s[0] < special_Bs[B_ind].f;
bool B_before = special_Bs[B_ind].f < special_As[A_ind].s.bk && special_Bs[B_ind].s[0] < special_As[A_ind].f;
if(A_before && B_before){
return vi{-1};
}
if(!A_before && !B_before){
return vi{-1};
}
if(A_before){
ord.pb(mp('A', A_ind)); A_ind++; continue;
}
assert(B_before);
ord.pb(mp('B', B_ind)); B_ind++; continue;
}
SegTree<int> A_max_to, B_max_to;
A_max_to.init(N);
B_max_to.init(M);
// for(int i = 0; i < sz(special_As); i++){
// A_max_to.upd(special_As[i].f, special_As[i].s.bk);
// }
// for(int i = 0; i < sz(special_Bs); i++){
// B_max_to.upd(special_Bs[i].f, special_Bs[i].s.bk);
// }
for(auto [side, ind]: ord){
if(side == 'A'){
if(B_max_to.query(special_As[ind].s[0], special_As[ind].s.bk) > special_As[ind].f){
return vi{-1};
}
A_max_to.upd(special_As[ind].f, special_As[ind].s.bk);
}
else{
if(A_max_to.query(special_Bs[ind].s[0], special_Bs[ind].s.bk) > special_Bs[ind].f){
return vi{-1};
}
B_max_to.upd(special_Bs[ind].f, special_Bs[ind].s.bk);
}
}
vi ans;
for(auto [side, ind]: ord){
if(side == 'A'){
ans.pb(A[special_As[ind].f]);
}
else{
ans.pb(B[special_Bs[ind].f]);
}
}
if(!checkSubseq(ans, A) || !checkSubseq(ans, B)) return vi{-1};
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
592 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
161 ms |
26964 KB |
Output is correct |
6 |
Correct |
169 ms |
26808 KB |
Output is correct |
7 |
Correct |
160 ms |
26808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
161 ms |
26964 KB |
Output is correct |
6 |
Correct |
169 ms |
26808 KB |
Output is correct |
7 |
Correct |
160 ms |
26808 KB |
Output is correct |
8 |
Correct |
93 ms |
16048 KB |
Output is correct |
9 |
Correct |
97 ms |
16052 KB |
Output is correct |
10 |
Correct |
95 ms |
16048 KB |
Output is correct |
11 |
Correct |
98 ms |
15992 KB |
Output is correct |
12 |
Correct |
103 ms |
16052 KB |
Output is correct |
13 |
Correct |
93 ms |
16052 KB |
Output is correct |
14 |
Correct |
97 ms |
16004 KB |
Output is correct |
15 |
Correct |
94 ms |
16048 KB |
Output is correct |
16 |
Correct |
102 ms |
16052 KB |
Output is correct |
17 |
Correct |
117 ms |
16048 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
848 KB |
Output is correct |
22 |
Correct |
71 ms |
12224 KB |
Output is correct |
23 |
Correct |
116 ms |
19644 KB |
Output is correct |
24 |
Correct |
129 ms |
19644 KB |
Output is correct |
25 |
Correct |
147 ms |
19644 KB |
Output is correct |
26 |
Correct |
89 ms |
14472 KB |
Output is correct |
27 |
Correct |
117 ms |
16140 KB |
Output is correct |
28 |
Correct |
123 ms |
15880 KB |
Output is correct |
29 |
Correct |
108 ms |
15896 KB |
Output is correct |
30 |
Correct |
105 ms |
15956 KB |
Output is correct |
31 |
Correct |
109 ms |
16052 KB |
Output is correct |
32 |
Correct |
119 ms |
16048 KB |
Output is correct |
33 |
Correct |
152 ms |
15920 KB |
Output is correct |
34 |
Correct |
95 ms |
14632 KB |
Output is correct |
35 |
Correct |
123 ms |
15924 KB |
Output is correct |
36 |
Correct |
121 ms |
16096 KB |
Output is correct |
37 |
Correct |
126 ms |
15944 KB |
Output is correct |
38 |
Correct |
117 ms |
15948 KB |
Output is correct |
39 |
Correct |
94 ms |
15956 KB |
Output is correct |
40 |
Correct |
113 ms |
15944 KB |
Output is correct |
41 |
Correct |
136 ms |
16080 KB |
Output is correct |
42 |
Correct |
92 ms |
16048 KB |
Output is correct |
43 |
Correct |
121 ms |
16052 KB |
Output is correct |
44 |
Correct |
112 ms |
16084 KB |
Output is correct |
45 |
Correct |
138 ms |
16052 KB |
Output is correct |
46 |
Correct |
115 ms |
15900 KB |
Output is correct |
47 |
Correct |
103 ms |
15948 KB |
Output is correct |
48 |
Correct |
88 ms |
16044 KB |
Output is correct |
49 |
Correct |
109 ms |
16052 KB |
Output is correct |
50 |
Correct |
124 ms |
16048 KB |
Output is correct |
51 |
Correct |
127 ms |
16060 KB |
Output is correct |
52 |
Correct |
125 ms |
16052 KB |
Output is correct |
53 |
Correct |
120 ms |
15996 KB |
Output is correct |
54 |
Correct |
95 ms |
16080 KB |
Output is correct |
55 |
Correct |
86 ms |
15984 KB |
Output is correct |
56 |
Correct |
127 ms |
16052 KB |
Output is correct |
57 |
Correct |
84 ms |
16052 KB |
Output is correct |
58 |
Correct |
87 ms |
16072 KB |
Output is correct |
59 |
Correct |
82 ms |
15944 KB |
Output is correct |
60 |
Correct |
95 ms |
15944 KB |
Output is correct |
61 |
Correct |
115 ms |
15992 KB |
Output is correct |
62 |
Correct |
94 ms |
16052 KB |
Output is correct |
63 |
Correct |
92 ms |
16016 KB |
Output is correct |
64 |
Correct |
83 ms |
16008 KB |
Output is correct |
65 |
Correct |
103 ms |
15908 KB |
Output is correct |
66 |
Correct |
118 ms |
16092 KB |
Output is correct |
67 |
Correct |
103 ms |
16048 KB |
Output is correct |
68 |
Correct |
80 ms |
16080 KB |
Output is correct |
69 |
Correct |
1 ms |
512 KB |
Output is correct |
70 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
33 ms |
6256 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
16048 KB |
Output is correct |
2 |
Correct |
97 ms |
16052 KB |
Output is correct |
3 |
Correct |
95 ms |
16048 KB |
Output is correct |
4 |
Correct |
98 ms |
15992 KB |
Output is correct |
5 |
Correct |
103 ms |
16052 KB |
Output is correct |
6 |
Correct |
93 ms |
16052 KB |
Output is correct |
7 |
Correct |
97 ms |
16004 KB |
Output is correct |
8 |
Correct |
94 ms |
16048 KB |
Output is correct |
9 |
Correct |
102 ms |
16052 KB |
Output is correct |
10 |
Correct |
117 ms |
16048 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
3 ms |
848 KB |
Output is correct |
15 |
Correct |
71 ms |
12224 KB |
Output is correct |
16 |
Correct |
116 ms |
19644 KB |
Output is correct |
17 |
Correct |
129 ms |
19644 KB |
Output is correct |
18 |
Correct |
147 ms |
19644 KB |
Output is correct |
19 |
Correct |
89 ms |
14472 KB |
Output is correct |
20 |
Runtime error |
33 ms |
6256 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
592 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
592 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |