#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int T, n, m, ans[maxn], lnk[maxn], id[maxn];
char str[maxn];
#define mid ((l + r) >> 1)
#define ls (p << 1)
#define rs (p << 1 | 1)
struct node { int tag, val; } t[maxn << 2];
void pushdown(int p) {
t[ls].tag += t[p].tag, t[rs].tag += t[p].tag;
t[ls].val += t[p].tag, t[rs].val += t[p].tag, t[p].tag = 0;
}
void clear(int p, int l, int r) {
t[p].tag = t[p].val = 0;
if (l == r) return;
clear(ls, l, mid), clear(rs, mid + 1, r);
}
void modify(int p, int l, int r, int ql, int qr, int val) {
if (ql <= l && r <= qr) { t[p].tag += val, t[p].val += val; return; }
pushdown(p);
if (ql <= mid) modify(ls, l, mid, ql, qr, val);
if (mid < qr) modify(rs, mid + 1, r, ql, qr, val);
t[p].val = min(t[ls].val, t[rs].val);
}
int ask(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return t[p].val;
int ret = 1e9;
pushdown(p);
if (ql <= mid) ret = min(ret, ask(ls, l, mid, ql, qr));
if (mid < qr) ret = min(ret, ask(rs, mid + 1, r, ql, qr));
return ret;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d\n%s", &n, str + 1);
clear(1, 1, m = n << 1);
for (int i = 1; i <= m; i++) {
lnk[i] = id[i] = 0;
}
int all = 0;
for (int i = 1, a, b; i <= n; i++) {
scanf("%d %d", &a, &b);
lnk[b] = a, id[b] = i;
if (str[a] == str[b]) {
if (str[a] == '(') {
ans[i] = 0;
all++;
modify(1, 1, m, a, m, 1);
} else {
ans[i] = 1;
all--;
modify(1, 1, m, b, m, -1);
}
} else {
if (str[a] == '(') {
ans[i] = 0;
all++;
modify(1, 1, m, a, m, 1);
} else {
ans[i] = 1;
all++;
modify(1, 1, m, b, m, 1);
}
}
}
if (t[1].val < 0) { puts("-1"); continue; }
for (int i = m; i >= 1; i--) {
if (!lnk[i]) continue;
if (str[lnk[i]] == str[i]) continue;
int res1 = ask(1, 1, m, lnk[i], i - 1);
int res2 = ask(1, 1, m, i, m);
if (res1 >= 1 && res2 >= 2) {
modify(1, 1, m, lnk[i], m, -1);
modify(1, 1, m, i, m, -1);
ans[id[i]] ^= 1;
all -= 2;
}
}
if (t[1].val == 0 && all == 0) {
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
putchar('\n');
} else {
puts("-1");
}
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d", &T);
| ~~~~~^~~~~~~~~~
Main.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d\n%s", &n, str + 1);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:51:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
508 KB |
Output is correct |
2 |
Correct |
31 ms |
352 KB |
Output is correct |
3 |
Correct |
41 ms |
520 KB |
Output is correct |
4 |
Correct |
39 ms |
504 KB |
Output is correct |
5 |
Correct |
38 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
356 KB |
Output is correct |
2 |
Correct |
36 ms |
1096 KB |
Output is correct |
3 |
Correct |
35 ms |
1944 KB |
Output is correct |
4 |
Correct |
41 ms |
2140 KB |
Output is correct |
5 |
Correct |
35 ms |
3280 KB |
Output is correct |
6 |
Correct |
23 ms |
3428 KB |
Output is correct |
7 |
Correct |
30 ms |
3660 KB |
Output is correct |
8 |
Correct |
35 ms |
5960 KB |
Output is correct |
9 |
Correct |
42 ms |
6184 KB |
Output is correct |
10 |
Correct |
56 ms |
6348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
40 ms |
376 KB |
Output is correct |
3 |
Correct |
52 ms |
1204 KB |
Output is correct |
4 |
Correct |
47 ms |
1824 KB |
Output is correct |
5 |
Correct |
56 ms |
2072 KB |
Output is correct |
6 |
Correct |
55 ms |
3380 KB |
Output is correct |
7 |
Correct |
21 ms |
3404 KB |
Output is correct |
8 |
Correct |
43 ms |
3808 KB |
Output is correct |
9 |
Correct |
45 ms |
5996 KB |
Output is correct |
10 |
Correct |
60 ms |
6280 KB |
Output is correct |
11 |
Correct |
67 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
508 KB |
Output is correct |
2 |
Correct |
31 ms |
352 KB |
Output is correct |
3 |
Correct |
41 ms |
520 KB |
Output is correct |
4 |
Correct |
39 ms |
504 KB |
Output is correct |
5 |
Correct |
38 ms |
464 KB |
Output is correct |
6 |
Correct |
28 ms |
356 KB |
Output is correct |
7 |
Correct |
36 ms |
1096 KB |
Output is correct |
8 |
Correct |
35 ms |
1944 KB |
Output is correct |
9 |
Correct |
41 ms |
2140 KB |
Output is correct |
10 |
Correct |
35 ms |
3280 KB |
Output is correct |
11 |
Correct |
23 ms |
3428 KB |
Output is correct |
12 |
Correct |
30 ms |
3660 KB |
Output is correct |
13 |
Correct |
35 ms |
5960 KB |
Output is correct |
14 |
Correct |
42 ms |
6184 KB |
Output is correct |
15 |
Correct |
56 ms |
6348 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
40 ms |
376 KB |
Output is correct |
18 |
Correct |
52 ms |
1204 KB |
Output is correct |
19 |
Correct |
47 ms |
1824 KB |
Output is correct |
20 |
Correct |
56 ms |
2072 KB |
Output is correct |
21 |
Correct |
55 ms |
3380 KB |
Output is correct |
22 |
Correct |
21 ms |
3404 KB |
Output is correct |
23 |
Correct |
43 ms |
3808 KB |
Output is correct |
24 |
Correct |
45 ms |
5996 KB |
Output is correct |
25 |
Correct |
60 ms |
6280 KB |
Output is correct |
26 |
Correct |
67 ms |
6492 KB |
Output is correct |
27 |
Correct |
51 ms |
508 KB |
Output is correct |
28 |
Correct |
68 ms |
1240 KB |
Output is correct |
29 |
Correct |
64 ms |
1996 KB |
Output is correct |
30 |
Correct |
75 ms |
2240 KB |
Output is correct |
31 |
Correct |
66 ms |
3388 KB |
Output is correct |
32 |
Correct |
45 ms |
3492 KB |
Output is correct |
33 |
Correct |
52 ms |
3732 KB |
Output is correct |
34 |
Correct |
70 ms |
6068 KB |
Output is correct |
35 |
Correct |
77 ms |
6368 KB |
Output is correct |
36 |
Correct |
85 ms |
6600 KB |
Output is correct |