QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#161542#7106. Infinite Parenthesis Sequenceucup-team004#TL 2ms3648kbC++202.3kb2023-09-03 00:24:452023-09-03 00:24:46

Judging History

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

  • [2023-09-03 00:24:46]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3648kb
  • [2023-09-03 00:24:45]
  • 提交

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;
    
    int n = s.size();
    
    std::fill(fen, fen + n, 0);
    
    int q;
    std::cin >> q;
    
    std::vector<std::array<int, 4>> qry(q);
    for (int i = 0; i < q; i++) {
        int k, l, r;
        std::cin >> k >> l >> r;
        r++;
        qry[i] = {k, l, r, i};
    }
    std::vector<int> ans(q);
    std::sort(qry.begin(), qry.end());
    
    std::set<int> p;
    auto check = [&](int i) {
        if (s[i] == '(' && s[(i - 1 + n) % n] != s[(i + 1) % n]) {
            p.insert(i);
        }
    };
    for (int i = 0; i < n; i++) {
        check(i);
    }
    int cur = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '(') {
            add(i, 1);
        }
    }
    auto get = [&](int x) {
        int v = x / n;
        if (x - v * n < 0) {
            v -= 1;
        }
        int ans = sum(n) * v;
        x -= v * n;
        ans += sum(x);
        return ans;
    };
    for (auto [k, l, r, i] : qry) {
        while (cur < k && !p.empty()) {
            std::vector<int> v(p.begin(), p.end());
            for (auto j : v) {
                j = (j - 1 + n) % n;
                if (s[j] == '(') {
                    add(j, -1);
                    s[j] = ')';
                } else {
                    add(j, 1);
                    s[j] = '(';
                }
            }
            p.clear();
            for (auto j : v) {
                check((j - 2 + n) % n);
                check((j - 1 + n) % n);
                check(j);
            }
            cur++;
        }
        l -= k;
        r -= k;
        ans[i] = get(r) - get(l);
    }
    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: 2ms
memory: 3648kb

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
Time Limit Exceeded

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:


result: