QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#163592#7106. Infinite Parenthesis Sequenceucup-team004TL 1949ms3696kbC++203.0kb2023-09-04 11:52:302023-09-04 11:52:30

Judging History

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

  • [2023-09-04 11:52:30]
  • 评测
  • 测评结果:TL
  • 用时:1949ms
  • 内存:3696kb
  • [2023-09-04 11:52:30]
  • 提交

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);
        if (rev) {
            ans[i] = r - l - ans[i];
        }
    }
    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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
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: 0
Accepted
time: 1949ms
memory: 3696kb

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
530591272
345487789
415550920
701803793
413364407
187916462
386485772
125057026
296666743
470522533
367131179
635722815
58970215
379425066
18...

result:

ok 271661 lines

Test #3:

score: -100
Time Limit Exceeded

input:

7
((()(((()(((()((()((()((()(((()(((()((()(((()(((()((()(((()(((()(((()(((()((()(((()((()((()(((()(((()(((()((()((()(((()((()((()(((()(((()(((()((()(((()((()(((()((()((()(((()(((()(((()(((()((()(((()(((()((()((()((()(((()(((()((()(((()(((()(((()((()(((()(((()((()((()(((()(((()(((()((()((()(((()((()(...

output:


result: