QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#424189 | #7106. Infinite Parenthesis Sequence | ucup-team3215 | WA | 166ms | 4212kb | C++20 | 5.8kb | 2024-05-29 01:30:34 | 2024-05-29 01:30:34 |
Judging History
answer
#include <algorithm>
#include <array>
#include <deque>
#include <iostream>
#include <vector>
using namespace std;
struct Slope {
deque<array<int, 2>> elem;
int c[2];
};
int inc(int i, int n) { return i < n - 1? i + 1: 0; }
int dec(int i, int n) { return i? i - 1: n - 1; }
int main() {
cin.tie(0)->sync_with_stdio(0);
for (int tc = (cin >> tc, tc); tc--; ) {
string s; int q; cin >> s >> q;
int n = s.size(), cu = count(s.begin(), s.end(), '(');
for (auto& c: s) c = c == '(';
vector<int> ans(q);
vector<array<int, 3>> que;
for (int i = 0; i < q; ++i) {
int k, l, r; cin >> k >> l >> r; ++r;
int l1 = (l % n + n) % n, r1 = (r % n + n) % n;
ans[i] = (r - l - r1 + l1) / n * cu;
if (l1) que.push_back({k, l1, ~i});
if (r1) que.push_back({k, r1, i});
}
sort(que.rbegin(), que.rend());
vector<Slope> slopes;
if (Slope cur{}; 1)
for (int i = 0; ; ++i) {
int c[2]{};
if (i == n || (c[s[i]] = s[dec(i, n)] != s[inc(i, n)], cur.elem.size() && c[0] + cur.c[0] && c[1] + cur.c[1])) {
for (int s = 0; auto& [i, j]: cur.elem) j = s, s += i;
slopes.push_back(cur);
cur = {};
}
if (i == n) break;
cur.c[s[i]] += c[s[i]];
cur.elem.push_back({s[i]});
}
int k = -1;
while (++k, slopes.size() > 1) {
for (int i = 0, j = 0, s = 0; que.size() && que.back()[0] == k; ) {
auto [_, jj, t] = que.back(); que.pop_back();
while (j + slopes[i].elem.size() <= jj) s += slopes[i].elem.back()[1] + slopes[i].elem.back()[0] - slopes[i].elem[0][1], j += slopes[i++].elem.size();
int a = s + slopes[i].elem[jj - j][1] - slopes[i].elem[0][1];
t < 0? ans[~t] -= a: ans[t] += a;
}
int m = slopes.size();
vector<array<int, 2>> fl(m);
for (int i = 0; i < m; ++i) {
fl[i] = {slopes[i].elem[0][0], slopes[i].elem.back()[0]};
}
for (int i = 0; i < m; ++i) {
if (slopes[i].elem.size() == 1) {
slopes[i].c[slopes[i].elem[0][0]] -= fl[dec(i, m)][1] != fl[inc(i, m)][0];
slopes[i].elem[0][0] = slopes[i].elem[0][0]? fl[inc(i, m)][0]: fl[dec(i, m)][1];
} else if (slopes[i].c[1]) {
slopes[i].c[1] -= slopes[i].elem[0][0] && fl[dec(i, m)][1] != slopes[i].elem[1][0];
slopes[i].c[1] -= slopes[i].elem[1][0] && slopes[i].elem[0][0] != (slopes[i].elem.size() > 2? slopes[i].elem[2][0]: fl[inc(i, m)][0]);
slopes[i].elem.pop_front();
slopes[i].elem.push_back({fl[inc(i, m)][0], slopes[i].elem.back()[0] + slopes[i].elem.back()[1]});
} else {
slopes[i].c[0] -= !slopes[i].elem.back()[0] && fl[inc(i, m)][0] != slopes[i].elem.end()[-2][0];
slopes[i].c[0] -= !slopes[i].elem.end()[-2][0] && slopes[i].elem.back()[0] != (slopes[i].elem.size() > 2? slopes[i].elem.end()[-3][0]: fl[dec(i, m)][1]);
slopes[i].elem.pop_back();
slopes[i].elem.push_front({fl[dec(i, m)][1], slopes[i].elem[0][1] - fl[dec(i, m)][1]});
}
}
for (int i = 0; i < m; ++i) {
if (slopes[i].elem.size() == 1) {
slopes[i].c[slopes[i].elem[0][0]] += slopes[dec(i, m)].elem.back()[0] != slopes[inc(i, m)].elem[0][0];
} else {
slopes[i].c[slopes[i].elem[0][0]] += slopes[dec(i, m)].elem.back()[0] != slopes[i].elem[1][0];
slopes[i].c[slopes[i].elem.back()[0]] += slopes[inc(i, m)].elem[0][0] != slopes[i].elem.end()[-2][0];
}
}
for (int i = 0; i < m; ++i) if (slopes[i].c[0] && slopes[i].c[1])
for (auto force: {0, 1}) {
if (slopes[i].c[slopes[i].elem[0][0]] == 1 && slopes[i].elem[1][0] != slopes[dec(i, m)].elem.back()[0] && (force || slopes[dec(i, m)].c[slopes[i].elem[0][0]])) {
slopes[dec(i, m)].elem.push_back({slopes[i].elem[0][0], slopes[dec(i, m)].elem.back()[0] + slopes[dec(i, m)].elem.back()[1]});
++slopes[dec(i, m)].c[slopes[i].elem[0][0]];
--slopes[i].c[slopes[i].elem[0][0]];
slopes[i].elem.pop_front();
break;
} else if (slopes[i].c[slopes[i].elem.back()[0]] == 1 && slopes[i].elem.end()[-2][0] != slopes[inc(i, m)].elem[0][0] && (force || slopes[inc(i, m)].c[slopes[i].elem.back()[0]])) {
slopes[inc(i, m)].elem.push_front({slopes[i].elem.back()[0], slopes[inc(i, m)].elem[0][1] - slopes[i].elem.back()[0]});
++slopes[inc(i, m)].c[slopes[i].elem.back()[0]];
--slopes[i].c[slopes[i].elem.back()[0]];
slopes[i].elem.pop_back();
break;
}
}
vector<Slope> nslopes;
for (int i = 0; i < m; ++i) {
if (nslopes.empty() || nslopes.back().c[0] + slopes[i].c[0] && nslopes.back().c[1] + slopes[i].c[1]) {
nslopes.push_back(move(slopes[i]));
continue;
}
nslopes.back().c[0] += slopes[i].c[0];
nslopes.back().c[1] += slopes[i].c[1];
if (nslopes.back().elem.size() < slopes[i].elem.size()) {
for (int j = nslopes.back().elem.size(); j--; ) {
slopes[i].elem.push_front({nslopes.back().elem[j][0], slopes[i].elem[0][1] - nslopes.back().elem[j][0]});
}
swap(slopes[i].elem, nslopes.back().elem);
} else {
for (auto& [x, _]: slopes[i].elem) {
nslopes.back().elem.push_back({x, nslopes.back().elem.back()[0] + nslopes.back().elem.back()[1]});
}
}
}
swap(slopes, nslopes);
}
auto slope = move(slopes[0]);
while (que.size()) {
auto [kk, r, t] = que.back(); que.pop_back();
int l = (kk - k) % n;
if (slope.c[0]) l = (n - l) % n;
r = (r + l) % n;
int a = slope.elem[r][1] - slope.elem[l][1] + (r >= l? 0: cu);
t < 0? ans[~t] -= a: ans[t] += a;
}
for (auto& x: ans) cout << x << '\n';
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3656kb
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: 166ms
memory: 4212kb
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 228567121 365269747 1028565788 529302353 84346502 148764845 667996084 149825682 1186712870 281727640 995600518 63752581 740373707 867951696 27044667 530591273 345487789 415550920 701803794 413364406 187916462 386485772 125057025 296666742 470522532 367131179 635722815 58970215 379425066 18...
result:
wrong answer 17th lines differ - expected: '530591272', found: '530591273'