QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#163586 | #7106. Infinite Parenthesis Sequence | ucup-team004 | WA | 1911ms | 3764kb | C++20 | 2.9kb | 2023-09-04 11:45:17 | 2023-09-04 11:45:17 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 1E5;
int fen[N];
void add(int x, int y) {
for (int i = x + 1; i <= N; i += i & -i) {
fen[i - 1] += y;
}
}
int sum(int x) {
int ans = 0;
for (int i = x; i; i &= i - 1) {
ans += fen[i - 1];
}
return ans;
}
void solve() {
std::string s;
std::cin >> s;
// std::mt19937 rng;
// for (int i = 0; i < 100000; i++) {
// s += (rng() % 2 ? ')' : '(');
// }
int n = s.size();
int cnt = std::count(s.begin(), s.end(), '(');
// std::cerr << "cnt : " << cnt << "\n";
bool rev = false;
if (cnt > n / 2) {
rev = true;
std::reverse(s.begin() + 1, s.end());
for (auto &c : s) {
c ^= '(' ^ ')';
}
}
std::fill(fen, fen + n, 0);
int q;
std::cin >> q;
std::vector<std::array<i64, 4>> qry(q);
for (int i = 0; i < q; i++) {
int k, l, r;
std::cin >> k >> l >> r;
if (rev) {
l *= -1;
r *= -1;
std::swap(l, r);
}
r++;
qry[i] = {k, l, r, i};
}
std::vector<int> ans(q);
std::sort(qry.begin(), qry.end());
std::vector<int> pre(n + 1);
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] + (s[i] == '(' ? 1 : -1);
}
int b = std::max_element(pre.begin(), pre.end()) - pre.begin();
std::rotate(s.begin(), s.begin() + b, s.end());
std::vector<int> pos;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '(') {
pos.push_back(i + 2 * pos.size());
}
}
const int C = pos.size();
std::vector<int> L(C, -1), R(C, C);
std::vector<int> stk;
for (int i = 0; i < C; i++) {
while (!stk.empty() && pos[i] < pos[stk.back()]) {
R[stk.back()] = i;
stk.pop_back();
}
L[i] = stk.empty() ? -1 : stk.back();
stk.push_back(i);
}
auto get = [&](i64 x, i64 k) {
i64 v = x / n;
if (x - v * n < 0) {
v -= 1;
}
i64 ans = 1LL * cnt * v;
x -= v * n;
// ans += sum(x);
for (int i = 0; i < C; i++) {
int v = pos[i];
for (int j = std::max(0LL, i - k); j <= i; j++) {
v = std::min(v, pos[j]);
}
v -= 2 * i;
ans += (v < x);
}
return ans;
};
for (auto [k, l, r, i] : qry) {
l -= k;
r -= k;
l -= b;
r -= b;
ans[i] = get(r, k) - get(l, k);
}
for (int i = 0; i < q; i++) {
std::cout << ans[i] << "\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3552kb
input:
3 (()) 3 0 -3 2 1 -2 3 2 0 0 ))()( 3 0 -3 4 2 1 3 3 -4 -1 ))()(()( 4 1234 -5678 9012 123 -456 789 12 -34 56 1 -2 3
output:
3 3 0 4 1 1 7345 623 45 3
result:
ok 10 lines
Test #2:
score: -100
Wrong Answer
time: 1911ms
memory: 3764kb
input:
5564 ()()(((() 16 0 -825489608 537105171 0 481386502 824237183 0 -32590250 515314371 0 -634830457 908018223 3 -494274112 299679416 125527658 81646800 208166552 632660143 -774899605 -551752339 4 -874787302 127206822 4 -102348267 122390255 2 -881139944 898929361 0 -656262874 -233671414 111787130 -5925...
output:
908396520 228567124 365269751 1028565786 529302353 84346501 148764842 667996081 149825682 1186712873 281727642 995600515 63752580 740373708 867951695 27044669 530591270 345487788 415550917 701803793 413364407 187916465 386485770 125057027 296666742 470522532 367131179 635722815 58970215 379425066 18...
result:
wrong answer 2nd lines differ - expected: '228567121', found: '228567124'