#include <stdio.h>
#include <string.h>
#define N 100000
int hh[N * 2], rr[N * 2], iq[1 + N], pq[N * 2], cnt;
int lt(int i, int j) { return rr[i] < rr[j]; }
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add(int i) {
pq[i] = ++cnt, pq_up(i);
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
static char cc[N * 2 + 1], tt[N];
static int dd[N * 2];
int n, h, i, l, r, d;
scanf("%d%s", &n, cc);
memset(dd, 0, n * 2 * sizeof *dd);
memset(hh, -1, n * 2 * sizeof *hh), memset(rr, -1, n * 2 * sizeof *rr);
for (h = 0; h < n; h++) {
int l, r;
scanf("%d%d", &l, &r), l--, r--;
if (cc[l] == '(' && cc[r] == '(')
tt[h] = 0, dd[l]++;
else if (cc[l] == ')' && cc[r] == ')')
tt[h] = 1, dd[r]--;
else {
hh[l] = h, rr[l] = r;
if (cc[l] == '(')
tt[h] = 1, dd[r]--;
else
tt[h] = 0, dd[l]--;
}
}
memset(pq, 0, n * 2 * sizeof *pq), cnt = 0;
for (i = 0, d = 0; i < n * 2; i++) {
if (rr[i] != -1)
pq_add(i);
d += dd[i];
if (d < 0) {
if (cnt == 0) {
d = -1;
break;
}
l = pq_remove_first(), h = hh[l], r = rr[l];
tt[h] ^= 1;
if (r <= i)
d += 2;
else
d++, dd[r]++;
}
}
if (d != 0)
printf("-1\n");
else {
for (h = 0; h < n; h++)
printf("%d ", tt[h]);
printf("\n");
}
}
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:46:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d", &t);
| ^~~~~~~~~~~~~~~
Main.c:52:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:58:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &l, &r), l--, r--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
568 KB |
Output is correct |
2 |
Correct |
19 ms |
412 KB |
Output is correct |
3 |
Correct |
26 ms |
548 KB |
Output is correct |
4 |
Correct |
25 ms |
532 KB |
Output is correct |
5 |
Correct |
24 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
452 KB |
Output is correct |
2 |
Correct |
15 ms |
724 KB |
Output is correct |
3 |
Correct |
14 ms |
1128 KB |
Output is correct |
4 |
Correct |
19 ms |
1468 KB |
Output is correct |
5 |
Correct |
14 ms |
1748 KB |
Output is correct |
6 |
Correct |
10 ms |
2480 KB |
Output is correct |
7 |
Correct |
13 ms |
2976 KB |
Output is correct |
8 |
Correct |
17 ms |
3536 KB |
Output is correct |
9 |
Correct |
18 ms |
3916 KB |
Output is correct |
10 |
Correct |
21 ms |
4452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
20 ms |
492 KB |
Output is correct |
3 |
Correct |
21 ms |
832 KB |
Output is correct |
4 |
Correct |
22 ms |
1192 KB |
Output is correct |
5 |
Correct |
24 ms |
1596 KB |
Output is correct |
6 |
Correct |
24 ms |
1924 KB |
Output is correct |
7 |
Correct |
10 ms |
2536 KB |
Output is correct |
8 |
Correct |
18 ms |
3156 KB |
Output is correct |
9 |
Correct |
17 ms |
3552 KB |
Output is correct |
10 |
Correct |
25 ms |
4340 KB |
Output is correct |
11 |
Correct |
29 ms |
4684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
568 KB |
Output is correct |
2 |
Correct |
19 ms |
412 KB |
Output is correct |
3 |
Correct |
26 ms |
548 KB |
Output is correct |
4 |
Correct |
25 ms |
532 KB |
Output is correct |
5 |
Correct |
24 ms |
492 KB |
Output is correct |
6 |
Correct |
18 ms |
452 KB |
Output is correct |
7 |
Correct |
15 ms |
724 KB |
Output is correct |
8 |
Correct |
14 ms |
1128 KB |
Output is correct |
9 |
Correct |
19 ms |
1468 KB |
Output is correct |
10 |
Correct |
14 ms |
1748 KB |
Output is correct |
11 |
Correct |
10 ms |
2480 KB |
Output is correct |
12 |
Correct |
13 ms |
2976 KB |
Output is correct |
13 |
Correct |
17 ms |
3536 KB |
Output is correct |
14 |
Correct |
18 ms |
3916 KB |
Output is correct |
15 |
Correct |
21 ms |
4452 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
20 ms |
492 KB |
Output is correct |
18 |
Correct |
21 ms |
832 KB |
Output is correct |
19 |
Correct |
22 ms |
1192 KB |
Output is correct |
20 |
Correct |
24 ms |
1596 KB |
Output is correct |
21 |
Correct |
24 ms |
1924 KB |
Output is correct |
22 |
Correct |
10 ms |
2536 KB |
Output is correct |
23 |
Correct |
18 ms |
3156 KB |
Output is correct |
24 |
Correct |
17 ms |
3552 KB |
Output is correct |
25 |
Correct |
25 ms |
4340 KB |
Output is correct |
26 |
Correct |
29 ms |
4684 KB |
Output is correct |
27 |
Correct |
28 ms |
1236 KB |
Output is correct |
28 |
Correct |
30 ms |
1700 KB |
Output is correct |
29 |
Correct |
25 ms |
2164 KB |
Output is correct |
30 |
Correct |
36 ms |
2588 KB |
Output is correct |
31 |
Correct |
27 ms |
2836 KB |
Output is correct |
32 |
Correct |
17 ms |
2596 KB |
Output is correct |
33 |
Correct |
23 ms |
3220 KB |
Output is correct |
34 |
Correct |
35 ms |
3784 KB |
Output is correct |
35 |
Correct |
32 ms |
4300 KB |
Output is correct |
36 |
Correct |
35 ms |
4732 KB |
Output is correct |