QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#164247 | #7106. Infinite Parenthesis Sequence | linnins | AC ✓ | 1993ms | 8108kb | C++14 | 4.1kb | 2023-09-04 20:54:25 | 2023-09-04 20:54:25 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <cstring>
#pragma GCC optimize(3)
const int maxn = 100000;
const int maxlog = 17;
int sl;
char s[maxn + 10];
int n;
int log_2[maxn + 10];
int st[maxlog + 1][maxn + 10];
int query_st(int l, int r){
int len = r - l + 1;
int lg = log_2[len];
return std::min(st[lg][l], st[lg][r - (1 << lg) + 1]);
}
int read_int(){
char c;
while(((c = getchar()) < '0' || c > '9') && c != '-');
bool flag = false;
int x = 0;
if(c == '-'){
flag = true;
}
else{
x = c - '0';
}
while((c = getchar()) >= '0' && c <= '9'){
x = x * 10 + c - '0';
}
if(flag)
return -x;
return x;
}
int read_string(char *sp){
char c;
while((c = getchar()) != '(' && c != ')');
sp[0] = c;
int p = 1;
while((c = getchar()) == '(' || c == ')'){
sp[p] = c;
++p;
}
sp[p] = '\0';
return p;
}
long long get_p(int k, long long i){
//左端点所在段号
long long t1 = 0;
if(i < 0){
t1 = (i - (n - 1)) / n;
}
else{
t1 = i / n;
}
//右端点所在段号
long long t2 = 0;
if(i + k < 0){
t2 = (i + k - (n - 1)) / n;
}
else{
t2 = (i + k) / n;
}
//答案的一部分
long long ans;
//左右端点
int l = (i % n + n) % n;
int r = ((i + k) % n + n) % n;
if(sl > 2 * n){
if(k + 1 > n){
if(l == 0){
ans = query_st(l, n - 1) - 2 * t1 * n + t1 * sl;
}
else{
ans = std::min(query_st(l, n - 1) - 2 * t1 * n + t1 * sl, query_st(0, l - 1) - 2 * (t1 + 1) * n + (t1 + 1) * sl);
}
}
else{
if(r < l){
ans = std::min(query_st(0, r) - 2 * t2 * n + t2 * sl, query_st(l, n - 1) - 2 * t1 * n + t1 * sl);
}
else if(l < r){
ans = query_st(l, r) - 2 * t1 * n + t1 * sl;
}
else{
if(k == 0){
ans = query_st(l, r) - 2 * t1 * n + t1 * sl;
}
else{
ans = std::min(query_st(0, r) - 2 * t2 * n + t2 * sl, query_st(l, n - 1) - 2 * t1 * n + t1 * sl);
}
}
}
}
else{
if(k + 1 > n){
if(r == n - 1){
ans = query_st(0, r) - 2 * t2 * n + t2 * sl;
}
else{
ans = std::min(query_st(0, r) - 2 * t2 * n + t2 * sl, query_st(r + 1, n - 1) - 2 * (t2 - 1) * n + (t2 - 1) * sl);
}
}
else{
if(r < l){
ans = std::min(query_st(0, r) - 2 * t2 * n + t2 * sl, query_st(l, n - 1) - 2 * t1 * n + t1 * sl);
}
else if(l < r){
ans = query_st(l, r) - 2 * t1 * n + t1 * sl;
}
else{
if(k == 0){
ans = query_st(l, r) - 2 * t1 * n + t1 * sl;
}
else{
ans = std::min(query_st(0, r) - 2 * t2 * n + t2 * sl, query_st(l, n - 1) - 2 * t1 * n + t1 * sl);
}
}
}
}
return ans + 2 * i + k;
}
int main(){
log_2[1] = 0;
for(int i = 2; i <= maxn; ++i){
log_2[i] = log_2[(i >> 1)] + 1;
}
int T;
T = read_int();
while(T--){
sl = read_string(s);
n = 0;
for(int i = 0; i < sl; ++i){
if(s[i] == '('){
st[0][n] = i - 2 * n;
++n;
}
}
for(int i = 1; i <= log_2[n]; ++i){
int t = (1 << (i - 1));
int t2 = (1 << i);
for(int j = 0; j + t2 - 1 < n; ++j){
st[i][j] = std::min(st[i - 1][j], st[i - 1][j + t]);
// printf("%d,%d:%d\n", i, j, st[i][j]);
}
}
/*for(int j = 3; j <= 100; ++j){
printf("---%d---\n", j);
for(int i = -2; i <= 10; ++i){
printf("*%d,%lld\n", i, get_p(j, i));
}
}*/
int q;
int k, rl, rr;
q = read_int();
if(n == 0){
while(q--){
k = read_int();
rl = read_int();
rr = read_int();
printf("0\n");
}
}
else{
while(q--){
k = read_int();
rl = read_int();
rr = read_int();
long long l = -5e9, r = 5e9;
while(l != r){
long long mid = (l + r) >> 1;
if(get_p(k, mid) < rl)
l = mid + 1;
else
r = mid;
}
int ansl = l;
l = -5e9;
r = 5e9;
while(l != r){
long long mid = (l + r + 1) >> 1;
if(get_p(k, mid) <= rr)
l = mid;
else
r = mid - 1;
}
printf("%lld\n", l - ansl + 1);
}
}
}
return 0;
}
/*
4
))()(
3
3 -4 -1
0 -3 4
2 1 3
(())
3
0 -3 2
1 -2 3
2 0 0
))()(()(
4
1234 -5678 9012
123 -456 789
12 -34 56
1 -2 3
)))))
5
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
*/
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 1824kb
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: 514ms
memory: 8108kb
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: 0
Accepted
time: 1485ms
memory: 8100kb
input:
7 ((()(((()(((()((()((()((()(((()(((()((()(((()(((()((()(((()(((()(((()(((()((()(((()((()((()(((()(((()(((()((()((()(((()((()((()(((()(((()(((()((()(((()((()(((()((()((()(((()(((()(((()(((()((()(((()(((()((()((()((()(((()(((()((()(((()(((()(((()((()(((()(((()((()((()(((()(((()(((()((()((()(((()((()(...
output:
185704843 446800089 255099554 1156402 212636323 416191407 12472890 247228052 489620931 107469522 16222287 341270888 29184920 107393597 163613521 175919552 118376824 76183214 805506070 206363476 326077675 54361969 121810843 684646392 716061472 697723268 23956954 588434738 4870237 305505833 489380166 ...
result:
ok 700000 lines
Test #4:
score: 0
Accepted
time: 1993ms
memory: 8044kb
input:
5571 ()()(((() 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 971661 lines
Extra Test:
score: 0
Extra Test Passed