QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#780426 | #1194. Parehtneses Editor | vwxyz | WA | 2ms | 13020kb | C++23 | 3.9kb | 2024-11-25 10:54:51 | 2024-11-25 10:55:01 |
Judging History
answer
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <cassert>
#include <climits>
using namespace std;
class SegmentTree {
private:
vector<int> segment_tree;
int N;
function<int(int, int)> f;
int e;
public:
SegmentTree(int N, function<int(int, int)> f, int e, const vector<int>& lst = {})
: N(N), f(f), e(e) {
segment_tree.assign(2 * N, e);
if (!lst.empty()) {
assert((int)lst.size() <= N);
for (int i = 0; i < (int)lst.size(); i++) {
segment_tree[N + i] = lst[i];
}
for (int i = N - 1; i > 0; i--) {
segment_tree[i] = f(segment_tree[i << 1], segment_tree[i << 1 | 1]);
}
}
}
int operator[](int i) const {
if (i < -N || i >= N) {
throw out_of_range("list index out of range");
}
if (i < 0) i += 2 * N;
return segment_tree[i + N];
}
void set(int i, int x) {
if (i < -N || i >= N) {
throw out_of_range("list index out of range");
}
if (i < 0) i += 2 * N;
i += N;
segment_tree[i] = x;
while (i > 1) {
i >>= 1;
segment_tree[i] = f(segment_tree[i << 1], segment_tree[i << 1 | 1]);
}
}
int fold(int L = 0, int R = -1) const {
if (R == -1) R = N;
L += N;
R += N;
int vL = e, vR = e;
while (L < R) {
if (L & 1) vL = f(vL, segment_tree[L++]);
if (R & 1) vR = f(segment_tree[--R], vR);
L >>= 1;
R >>= 1;
}
return f(vL, vR);
}
int bisect_left(int R, function<bool(int)> check) const {
if (R == 0) return 0;
R += N;
int v = e;
while (R > 1) {
R--;
while (R > 1 && (R % 2 == 0)) R >>= 1;
int vv = f(segment_tree[R], v);
if (check(vv)) {
v = vv;
} else {
while (R < N) {
R = R * 2 + 1;
vv = f(segment_tree[R], v);
if (check(vv)) {
v = vv;
R--;
}
}
return R + 1 - N;
}
}
return 0;
}
};
int main() {
string S;
cin >> S;
int N = S.size();
const int inf = INT_MAX;
SegmentTree ST(N + 1, [](int a, int b) { return min(a, b); }, inf);
vector<int> queue = {0};
ST.set(0, 0);
long long ans = 0;
const int M = 200000;
vector<vector<int>> lst(2 * M + 10);
lst[M].push_back(0);
for (int i = 0; i < N; i++) {
char s = S[i];
if (s == '(') {
queue.push_back(queue.back() + 1);
lst[queue.back() + M].push_back(queue.size() - 1);
ST.set(queue.size() - 1, queue.back());
} else if (s == ')') {
queue.push_back(queue.back() - 1);
lst[queue.back() + M].push_back(queue.size() - 1);
ST.set(queue.size() - 1, queue.back());
int l = ST.bisect_left(queue.size(), [&](int x) { return x >= queue.back(); });
ans += lst[queue.back() + M].size() -
(lower_bound(lst[queue.back() + M].begin(), lst[queue.back() + M].end(), l) - lst[queue.back() + M].begin()) - 1;
} else {
int l = ST.bisect_left(queue.size(), [&](int x) { return x >= queue.back(); });
ans -= lst[queue.back() + M].size() -
(lower_bound(lst[queue.back() + M].begin(), lst[queue.back() + M].end(), l) - lst[queue.back() + M].begin()) - 1;
lst[queue.back() + M].pop_back();
ST.set(queue.size() - 1, inf);
queue.pop_back();
}
cout << ans << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 12476kb
input:
(()())---)
output:
0 0 1 1 3 4 3 1 1 2
result:
ok 10 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 12564kb
input:
()--()()----)(()()))
output:
0 1 0 0 0 1 1 3 1 1 0 0 0 0 0 1 1 3 4 4
result:
ok 20 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 13020kb
input:
))(((-)(()((---(-)(-())-(()()(-)--(())))--()((())-)(()(())((-))))(-(((()((()()()()))-(())((((--))-())-)(-(--))))((((-)(-(-)((((()--(---)(-))()(-)(()()-(())()(()()((()()))))(()(()(-(--)-()((()(((()-))-)(()-()()-(-((-)(-)(((()-)))))-())()-(()((()(-)()))((-))())))()()()(-(-(())-()(()-)-))((()))((--(-()...
output:
0 0 0 0 0 0 1 1 1 2 2 2 2 2 1 1 1 2 2 2 2 4 6 4 4 4 5 5 7 7 7 10 7 5 5 5 6 7 9 12 9 7 7 9 9 9 9 10 11 10 11 11 11 12 12 12 13 15 15 15 15 18 20 23 25 25 25 25 25 25 25 26 26 26 26 27 27 29 29 32 32 36 37 39 37 37 37 38 40 40 40 40 40 40 40 41 44 41 41 43 46 43 46 46 46 46 46 43 46 48 49 50 50 50 50 ...
result:
ok 20000 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 12392kb
input:
(()())---)
output:
0 0 1 1 3 4 3 1 1 2
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 12640kb
input:
()--()()----)(()()))
output:
0 1 0 0 0 1 1 3 1 1 0 0 0 0 0 1 1 3 4 4
result:
ok 20 numbers
Test #6:
score: 0
Accepted
time: 0ms
memory: 12352kb
input:
(
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 0ms
memory: 12532kb
input:
)
output:
0
result:
ok 1 number(s): "0"
Test #8:
score: 0
Accepted
time: 2ms
memory: 12580kb
input:
((
output:
0 0
result:
ok 2 number(s): "0 0"
Test #9:
score: 0
Accepted
time: 0ms
memory: 12548kb
input:
()
output:
0 1
result:
ok 2 number(s): "0 1"
Test #10:
score: 0
Accepted
time: 2ms
memory: 12616kb
input:
(-
output:
0 0
result:
ok 2 number(s): "0 0"
Test #11:
score: 0
Accepted
time: 0ms
memory: 12576kb
input:
)(
output:
0 0
result:
ok 2 number(s): "0 0"
Test #12:
score: 0
Accepted
time: 2ms
memory: 12544kb
input:
))
output:
0 0
result:
ok 2 number(s): "0 0"
Test #13:
score: 0
Accepted
time: 2ms
memory: 12620kb
input:
)-
output:
0 0
result:
ok 2 number(s): "0 0"
Test #14:
score: -100
Wrong Answer
time: 0ms
memory: 12672kb
input:
(((((()((())()((()))
output:
0 0 0 0 0 0 1 1 1 1 2 3 3 5 5 5 5 6 10 14
result:
wrong answer 19th numbers differ - expected: '7', found: '10'