QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#241643#7106. Infinite Parenthesis SequencehewanyingRE 1ms6516kbC++141.5kb2023-11-06 15:00:452023-11-06 15:00:46

Judging History

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

  • [2023-11-06 15:00:46]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:6516kb
  • [2023-11-06 15:00:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N=2e5+5;
int T,m,n,q,lg[N],st[20][N];
char s[N];

int read(){
  int x=0,f=1;char ch=getchar();
  while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
  while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
  return x*f;
}

void print(ll x){
  int p[25],tmp=0;
  if(x==0) putchar('0');
  if(x<0) putchar('-'),x=-x;
  while(x) p[++tmp]=x%10,x/=10;
  for(int i=tmp;i>=1;i--) putchar(p[i]+'0');
  putchar('\n');
}

void init(){
  m=0;
  for(int i=0;i<n;i++) if(s[i]=='(') st[0][m++]=i;
  for(int i=m;i<m*2;i++) st[0][i]=st[0][i-m]+n;
  for(int i=0;i<m*2;i++) st[0][i]-=i*2; 
  for(int i=1;i<20;i++)
    for(int j=0;j+(1<<i)-1<m*2;j++)
      st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
}

ll calc(ll k,ll i){
  ll L,R;
  if(m*2<=n) L=i,R=min(i+k,L+m-1);
  else R=i+k,L=max(R-m+1,i);
  ll div=(L>=0?L/m:(L+1)/m-1),delta=1ll*n*div-2ll*m*div;
  int len=R-L+1,p=lg[len],nw=(L%m+m)%m;
  return min(st[p][nw],st[p][nw+len-(1<<p)])+delta+k+2*i;
}

ll query(ll k,ll x){
  ll l=-3e9,r=3e9;
  while(l<r){
  	ll mid=(l+r+1)/2;
  	if(calc(k,mid)<=x) l=mid;
  	else r=mid-1;
  }
  return l;
}

void sol(){
  scanf("%s",s); 
  n=strlen(s);init();
  q=read();
  for(int i=1;i<=q;i++){
    ll k=1ll*read(),l=1ll*read(),r=1ll*read();
    print(query(k,r)-query(k,l-1));
  }
}

int main(){
  lg[1]=0;
  for(int i=2;i<N;i++) lg[i]=lg[i>>1]+1;
  T=read();
  while(T--) sol();
  return 0;
} 

详细

Test #1:

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

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: