QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#424137#7106. Infinite Parenthesis Sequenceucup-team3215RE 1ms3688kbC++204.4kb2024-05-28 22:22:162024-05-28 22:22:16

Judging History

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

  • [2024-05-28 22:22:16]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3688kb
  • [2024-05-28 22:22:16]
  • 提交

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 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) {
      if (i == n || cur.elem.size() && cur.elem.back()[0] == s[i] && cur.c[!s[i]]) {
        for (int s = 0; auto& [i, j]: cur.elem) j = s, s += i;
        slopes.push_back(cur);
        cur = {};
      }
      if (i == n) break;
      if (cur.elem.size() && cur.elem.back()[0] == s[i]) ++cur.c[s[i]];
      cur.elem.push_back({s[i]});
    }
    int k = 0;
    while (slopes.size() > 1) {
      for (int i = 0, j = 0, s = 0; que.size() && que.back()[0] == k; ++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;
      }
      vector<array<int, 2>> fl(slopes.size());
      for (int i = 0; i < slopes.size(); ++i) {
        fl[i] = {slopes[i].elem[0][0], slopes[i].elem.back()[0]};
      }
      vector<Slope> nslopes;
      for (int i = 0; i < slopes.size(); ++i) {
        if (slopes[i].elem.size() == 1) {
          slopes[i].elem[0][0] = slopes[i].elem[0][0]? fl[i + 1 == slopes.size()? 0: i + 1][0]: (i? fl[i - 1]: fl.back())[1];
        } else if (slopes[i].c[1]) {
          auto shiftin = slopes[i].elem.back()[0]? fl[i + 1 == slopes.size()? 0: i + 1][0]: slopes[i].elem.end()[-2][0];
          slopes[i].c[1] -= slopes[i].elem[0][0] == slopes[i].elem[1][0];
          slopes[i].elem.pop_front();
          slopes[i].c[1] += slopes[i].elem.back()[0] == shiftin;
          slopes[i].elem.push_back({shiftin, slopes[i].elem.back()[1] + slopes[i].elem.back()[0]});
        } else {
          auto shiftin = slopes[i].elem[0][0]? slopes[i].elem[1][0]: (i? fl[i - 1]: fl.back())[1];
          slopes[i].c[0] -= slopes[i].elem.back()[0] == slopes[i].elem.end()[-2][0];
          slopes[i].elem.pop_back();
          slopes[i].c[0] += slopes[i].elem[0][0] == shiftin;
          slopes[i].elem.push_front({shiftin, slopes[i].elem[0][1] - shiftin});
        }
        if (nslopes.size() && (!nslopes.back().c[1] && !(nslopes.back().elem.back()[0] && slopes[i].elem[0][0]) && !slopes[i].c[1] ||
                               !nslopes.back().c[0] &&  (nslopes.back().elem.back()[0] || slopes[i].elem[0][0]) && !slopes[i].c[0])) {
          nslopes.back().c[0] += slopes[i].c[0] + !(nslopes.back().elem.back()[0] || slopes[i].elem[0][0]);
          nslopes.back().c[1] += slopes[i].c[1] +  (nslopes.back().elem.back()[0] && slopes[i].elem[0][0]);
          if (nslopes.back().elem.size() > slopes[i].elem.size()) {
            auto t = nslopes.back().elem.back()[1];
            for (auto& [x, _]: slopes[i].elem) nslopes.back().elem.push_back({x, t}), t += x;
          } else {
            auto t = slopes[i].elem.front()[1];
            for (int j = nslopes.back().elem.size(); j--; ) if (auto& [x, _] = nslopes.back().elem[j]; 1) {
              slopes[i].elem.push_front({x, t -= x});
            }
            swap(nslopes.back().elem, slopes[i].elem);
          }
        } else {
          nslopes.push_back(move(slopes[i]));
        }
      }
      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';
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3688kb

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
Runtime Error

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: