QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#163589#7106. Infinite Parenthesis Sequenceucup-team004WA 1953ms3668kbC++202.9kb2023-09-04 11:47:332023-09-04 11:47:33

Judging History

你现在查看的是最新测评结果

  • [2023-09-04 11:47:33]
  • 评测
  • 测评结果:WA
  • 用时:1953ms
  • 内存:3668kb
  • [2023-09-04 11:47:33]
  • 提交

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 * C * 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: 0ms
memory: 3496kb

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: 1953ms
memory: 3668kb

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:

454198260
114283561
182634875
514282893
264651176
42173251
74382422
333998041
74912841
593356436
140863821
497800258
31876290
370186854
433975847
13522334
265295636
172743894
207775459
350901896
206682203
93958232
193242885
62528513
148333371
235261266
183565589
635722815
58970215
379425066
18993734...

result:

wrong answer 1st lines differ - expected: '908396520', found: '454198260'