This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |