QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#163980 | #7106. Infinite Parenthesis Sequence | TsReaper | RE | 2ms | 6104kb | C++17 | 1.7kb | 2023-09-04 17:39:03 | 2023-09-04 17:39:04 |
Judging History
answer
#include <bits/stdc++.h>
#define MAXN ((int) 1e5)
#define MAXP 20
using namespace std;
int n, m, q;
char s[MAXN + 10];
int lg[MAXN + 10], rmq[MAXP][MAXN * 2 + 10];
void preRmq() {
m = 0;
for (int i = 0; i < n; i++) if (s[i] == '(') rmq[0][m++] = i;
for (int i = m; i < m * 2; i++) rmq[0][i] = rmq[0][i - m] + n;
for (int i = 0; i < m * 2; i++) rmq[0][i] -= i * 2;
for (int p = 1; p < MAXP; p++) for (int i = 0, ii = (1 << p) - 1; ii < m * 2; i++, ii++) {
int j = i + (1 << (p - 1));
rmq[p][i] = min(rmq[p - 1][i], rmq[p - 1][j]);
}
}
long long calc(long long K, long long i) {
long long L, R;
if (m * 2 <= n) {
L = i;
R = min(i + K, L + m - 1);
} else {
R = i + K;
L = max(i, R - m + 1);
}
long long div = (L >= 0 ? L / m : (L + 1) / m - 1);
int mod = (L % m + m) % m;
long long delta = n * div - 2 * div * m;
int len = R - L + 1, p = lg[len];
return min(rmq[p][mod], rmq[p][mod + len - (1 << p)]) + delta + K + 2 * i;
}
long long query(long long K, long long lim) {
long long head = -1e18, tail = 1e18;
while (head < tail) {
long long mid = (head + tail + 1) >> 1;
if (calc(K, mid) <= lim) head = mid;
else tail = mid - 1;
}
return head;
}
void solve() {
scanf("%s", s); n = strlen(s);
preRmq();
scanf("%d", &q);
while (q--) {
long long K, l, r; scanf("%lld%lld%lld", &K, &l, &r);
printf("%lld\n", query(K, r) - query(K, l - 1));
}
}
int main() {
lg[1] = 0;
for (int i = 2; i <= MAXN; i++) lg[i] = lg[i >> 1] + 1;
int tcase; scanf("%d", &tcase);
while (tcase--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 6104kb
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...