#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stack>
#include <memory.h>
#include <assert.h>
#include <stdlib.h>
#include <algorithm>
#include <set>
#include <queue>
#include <functional>
using namespace std;
void IMPOSSIBLE() {
puts("IMPOSSIBLE");
exit(0);
}
const int N_ = 300500;
const int LEAF = 1<<20;
const int TSZ = LEAF + LEAF + 3;
int N, A[N_ + N_];
bool used[N_];
int L[N_], R[N_];
struct segtree {
int tree[TSZ];
int type;
segtree (int type): type(type) {
if(type == 1) fill(tree, tree + TSZ, -987654321);
if(type == 2) fill(tree, tree + TSZ, +987654321);
if(type == 3) fill(tree, tree + TSZ, 0);
}
inline int func (int a, int b) {
switch(type) {
case 1: return a > b ? a : b;
case 2: return a < b ? a : b;
case 3: return a + b;
}
return -1;
}
void update (int pos, int val) {
tree[pos += LEAF] = val;
while(pos >>= 1) tree[pos] = func(tree[pos+pos], tree[pos+pos+1]);
}
int query (int l, int r) {
int ret = -1987654321;
bool wr = false;
l += LEAF; r += LEAF;
while(l <= r) {
if((l & 1) == 1) ret = wr ? func(ret, tree[l]) : tree[l], wr = true;
if((r & 1) == 0) ret = wr ? func(ret, tree[r]) : tree[r], wr = true;
l = (l + 1) >> 1;
r = (r - 1) >> 1;
}
return ret;
}
// for type 3
int kth (int k) {
if(type != 3) return -1;
int nd = 1;
while(nd < LEAF) {
if(tree[nd + nd] <= k) {
nd = nd + nd;
}else {
k -= tree[nd + nd];
nd = nd + nd + 1;
}
}
return nd - LEAF;
}
};
segtree tree1(1), tree2(2), tree3(3);
/*
1
2 3
4 5 6 7
8 9 0 1 2 3 4 5
2
1 1
1 0 1 0
0 1 0 0 0 1 0 0
*/
queue<int> cand, que;
vector<int> inside[N_];
stack<int> stk;
int res[N_ + N_];
void que_push(int v, int r = 1) {
if (used[v]) return;
que.push(v); res[v] = r; used[v] = true;
tree1.update (L[v], -987654321);
tree2.update (R[v], +987654321);
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
scanf("%d", &N);
for(int i = 1; i <= N+N; i++) {
scanf("%d", A+i);
if(L[A[i]] == 0) L[A[i]] = i; else R[A[i]] = i;
}
for(int i = 1; i <= N; i++) {
tree1.update (L[i], R[i]);
tree2.update (R[i], L[i]);
}
for(int i = 1; i <= N+N; i++) {
int a = A[i];
if(L[a] == i) {
int t = tree3.query(1, R[a]);
if(tree3.tree[1] == t) cand.push(a);
else inside[A[tree3.kth (t + 1)]].push_back(a);
tree3.update (R[a], 0);
}else {
tree3.update (R[a], 1);
}
}
int cnt = 0;
while (cnt < N) {
que_push(cand.front()); cand.pop();
while (!que.empty()) {
int u = que.front(); que.pop(); ++cnt;
// 1. L[x] < L[u] < R[x] < R[u]
for (int r; (r = tree1.query(1, L[u])) > 0;) {
if (r < L[u]) break;
que_push(A[r], 3 - res[u]);
}
// 2. L[u] < L[x] < R[u] < R[x]
for (int l; (l = tree2.query(R[u], N + N)) <= N + N;) {
if (R[u] < l) break;
que_push(A[l], 3 - res[u]);
}
for (int i = 0; i < inside[u].size(); i++) {
cand.push(inside[u][i]);
}
}
}
for (int cur = 1; cur <= 2; cur++) {
memset(used, 0, sizeof used);
while (!stk.empty()) stk.pop();
for (int i = 1; i <= N + N; i++) if (res[A[i]] == cur) {
if (!used[A[i]]) {
used[A[i]] = true;
stk.push(A[i]);
}
else {
if (stk.top() != A[i]) IMPOSSIBLE();
stk.pop();
}
}
}
for (int i = 1; i <= N + N; i++) printf("%c", res[A[i]] == 1 ? '^' : 'v');
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
40200 KB |
Output is correct |
2 |
Correct |
0 ms |
40200 KB |
Output is correct |
3 |
Correct |
0 ms |
40200 KB |
Output is correct |
4 |
Correct |
12 ms |
40200 KB |
Output is correct |
5 |
Correct |
0 ms |
40200 KB |
Output is correct |
6 |
Correct |
0 ms |
40200 KB |
Output is correct |
7 |
Correct |
0 ms |
40200 KB |
Output is correct |
8 |
Correct |
4 ms |
40200 KB |
Output is correct |
9 |
Correct |
4 ms |
40200 KB |
Output is correct |
10 |
Correct |
0 ms |
40200 KB |
Output is correct |
11 |
Correct |
16 ms |
40200 KB |
Output is correct |
12 |
Correct |
4 ms |
40200 KB |
Output is correct |
13 |
Correct |
0 ms |
40200 KB |
Output is correct |
14 |
Correct |
8 ms |
40200 KB |
Output is correct |
15 |
Correct |
4 ms |
40200 KB |
Output is correct |
16 |
Correct |
0 ms |
40200 KB |
Output is correct |
17 |
Correct |
8 ms |
40200 KB |
Output is correct |
18 |
Correct |
4 ms |
40200 KB |
Output is correct |
19 |
Correct |
8 ms |
40200 KB |
Output is correct |
20 |
Correct |
12 ms |
40200 KB |
Output is correct |
21 |
Correct |
4 ms |
40200 KB |
Output is correct |
22 |
Correct |
0 ms |
40200 KB |
Output is correct |
23 |
Correct |
8 ms |
40200 KB |
Output is correct |
24 |
Correct |
8 ms |
40200 KB |
Output is correct |
25 |
Correct |
0 ms |
40200 KB |
Output is correct |
26 |
Correct |
0 ms |
40200 KB |
Output is correct |
27 |
Correct |
8 ms |
40200 KB |
Output is correct |
28 |
Correct |
4 ms |
40200 KB |
Output is correct |
29 |
Correct |
4 ms |
40200 KB |
Output is correct |
30 |
Correct |
4 ms |
40200 KB |
Output is correct |
31 |
Correct |
4 ms |
40200 KB |
Output is correct |
32 |
Correct |
0 ms |
40200 KB |
Output is correct |
33 |
Correct |
4 ms |
40200 KB |
Output is correct |
34 |
Correct |
4 ms |
40200 KB |
Output is correct |
35 |
Correct |
16 ms |
40200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
40200 KB |
Output is correct |
2 |
Correct |
12 ms |
40200 KB |
Output is correct |
3 |
Correct |
8 ms |
40200 KB |
Output is correct |
4 |
Correct |
8 ms |
40200 KB |
Output is correct |
5 |
Correct |
0 ms |
40200 KB |
Output is correct |
6 |
Correct |
0 ms |
40200 KB |
Output is correct |
7 |
Correct |
8 ms |
40200 KB |
Output is correct |
8 |
Correct |
4 ms |
40200 KB |
Output is correct |
9 |
Correct |
0 ms |
40200 KB |
Output is correct |
10 |
Correct |
0 ms |
40200 KB |
Output is correct |
11 |
Correct |
12 ms |
40200 KB |
Output is correct |
12 |
Correct |
20 ms |
40200 KB |
Output is correct |
13 |
Correct |
0 ms |
40200 KB |
Output is correct |
14 |
Correct |
4 ms |
40200 KB |
Output is correct |
15 |
Correct |
0 ms |
40200 KB |
Output is correct |
16 |
Correct |
0 ms |
40200 KB |
Output is correct |
17 |
Correct |
12 ms |
40200 KB |
Output is correct |
18 |
Correct |
12 ms |
40200 KB |
Output is correct |
19 |
Correct |
4 ms |
40200 KB |
Output is correct |
20 |
Correct |
0 ms |
40200 KB |
Output is correct |
21 |
Correct |
4 ms |
40200 KB |
Output is correct |
22 |
Correct |
8 ms |
40200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
40200 KB |
Output is correct |
2 |
Correct |
12 ms |
40200 KB |
Output is correct |
3 |
Correct |
80 ms |
40592 KB |
Output is correct |
4 |
Correct |
128 ms |
40464 KB |
Output is correct |
5 |
Correct |
108 ms |
40464 KB |
Output is correct |
6 |
Correct |
100 ms |
40332 KB |
Output is correct |
7 |
Correct |
140 ms |
40464 KB |
Output is correct |
8 |
Correct |
20 ms |
40200 KB |
Output is correct |
9 |
Correct |
24 ms |
40200 KB |
Output is correct |
10 |
Correct |
88 ms |
40464 KB |
Output is correct |
11 |
Correct |
96 ms |
40464 KB |
Output is correct |
12 |
Correct |
96 ms |
40332 KB |
Output is correct |
13 |
Correct |
100 ms |
40460 KB |
Output is correct |
14 |
Correct |
120 ms |
40464 KB |
Output is correct |
15 |
Correct |
132 ms |
40464 KB |
Output is correct |
16 |
Correct |
32 ms |
40200 KB |
Output is correct |
17 |
Correct |
28 ms |
40200 KB |
Output is correct |
18 |
Correct |
100 ms |
40592 KB |
Output is correct |
19 |
Correct |
104 ms |
40464 KB |
Output is correct |
20 |
Correct |
180 ms |
40596 KB |
Output is correct |
21 |
Correct |
120 ms |
40464 KB |
Output is correct |
22 |
Correct |
148 ms |
40464 KB |
Output is correct |
23 |
Correct |
196 ms |
40596 KB |
Output is correct |
24 |
Correct |
180 ms |
40596 KB |
Output is correct |
25 |
Correct |
200 ms |
40596 KB |
Output is correct |
26 |
Correct |
184 ms |
40596 KB |
Output is correct |
27 |
Correct |
168 ms |
40992 KB |
Output is correct |
28 |
Correct |
172 ms |
40992 KB |
Output is correct |
29 |
Correct |
168 ms |
40992 KB |
Output is correct |
30 |
Correct |
172 ms |
40992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
40860 KB |
Output is correct |
2 |
Correct |
464 ms |
41120 KB |
Output is correct |
3 |
Correct |
300 ms |
40860 KB |
Output is correct |
4 |
Correct |
432 ms |
40992 KB |
Output is correct |
5 |
Correct |
460 ms |
41144 KB |
Output is correct |
6 |
Correct |
644 ms |
41536 KB |
Output is correct |
7 |
Correct |
700 ms |
41540 KB |
Output is correct |
8 |
Correct |
548 ms |
41144 KB |
Output is correct |
9 |
Correct |
344 ms |
40860 KB |
Output is correct |
10 |
Correct |
340 ms |
40992 KB |
Output is correct |
11 |
Correct |
448 ms |
41144 KB |
Output is correct |
12 |
Correct |
352 ms |
40860 KB |
Output is correct |
13 |
Correct |
696 ms |
41540 KB |
Output is correct |
14 |
Correct |
604 ms |
41408 KB |
Output is correct |
15 |
Correct |
336 ms |
40992 KB |
Output is correct |
16 |
Correct |
328 ms |
40860 KB |
Output is correct |
17 |
Correct |
356 ms |
40860 KB |
Output is correct |
18 |
Correct |
320 ms |
40860 KB |
Output is correct |
19 |
Correct |
504 ms |
41144 KB |
Output is correct |
20 |
Correct |
668 ms |
41540 KB |
Output is correct |
21 |
Correct |
640 ms |
41408 KB |
Output is correct |
22 |
Correct |
624 ms |
42596 KB |
Output is correct |
23 |
Correct |
576 ms |
42596 KB |
Output is correct |
24 |
Correct |
628 ms |
42596 KB |
Output is correct |
25 |
Correct |
620 ms |
42596 KB |
Output is correct |