QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#168043 | #7106. Infinite Parenthesis Sequence | ucup-team1479 | AC ✓ | 1969ms | 18792kb | C++14 | 2.5kb | 2023-09-07 20:20:29 | 2023-09-07 20:20:29 |
Judging History
answer
//prepare for coding{{{
#include<bits/stdc++.h>
#define RELEASE
#ifdef RELEASE
#define DB(...) ;
#else
#define FL
#ifdef FL
#define DB(...) fprintf(stderr,__VA_ARGS__)
#else
#define DB printf
#endif
#endif
#define int long long
const int N=100000,Q=100000,LGN=20;
const int INF=1000000000;
using namespace std;
inline int Read(){
char c=getchar();
int res=0;
bool b=0;
while(c>'9'||c<'0')
b=(c=='-'),c=getchar();
while(c>='0'&&c<='9')
res=(res<<3)+(res<<1)+c-'0',c=getchar();
return b?-res:res;
}
inline void ReadS(int &n,bool a[]){
char c=getchar();
while(c!='('&&c!=')')
c=getchar();
n=0;
while(c=='('||c==')')
a[++n]=(c=='('),c=getchar();
}
inline int Min(int a,int b){
return a>b?b:a;
}
inline int Mod(int x,int mod){
return ((x-1)%mod+mod)%mod+1;
}
int n,q;
bool a[N+10];
//}}}
//st table{{{
int mn[LGN+10][N+10],tl;
int lg[N+10];
int bs,dj;
inline void ST(){
lg[1]=0;
for(int i=2;i<=tl;++i)
lg[i]=lg[i>>1]+1;
for(int i=1,s=2;s<=tl;++i,s<<=1)
for(int j=1;j+s-1<=tl;++j)
mn[i][j]=Min(mn[i-1][j],mn[i-1][j+(s>>1)]);
dj=n-2*tl;
bs=Min(mn[lg[tl]][1],mn[lg[tl]][tl-(1<<lg[tl])+1]);
}
inline int ST_Min(int l,int r){
int t=lg[r-l+1];
return Min(mn[t][l],mn[t][r-(1<<t)+1]);
}
inline int Query(int l,int r){
int tnl=(l+tl-1>0?(l+tl-1)/tl:l/tl),tnr=(r>0?(r-1)/tl:r/tl-1);
if(tnl==tnr+1)
return ST_Min(Mod(l,tl),Mod(r,tl))+tnr*dj;
int a=ST_Min(Mod(l,tl),tl)+(tnl-1)*dj,b=ST_Min(1,Mod(r,tl))+tnr*dj;
return Min(Min(a,b),tnl==tnr?INF:bs+dj*(dj>0?tnl:tnr-1));
}
//}}}
//solve{{{
inline int Get(int tn,int x){
return Query(x,x+tn)+x+x+tn;
}
inline int Lower_Bound(int tn,int lim){
int l=-INF,r=INF-1,ans=INF;
while(l<=r){
int mid=(l+r)>>1;
if(Get(tn,mid)>=lim)
r=(ans=mid)-1;
else
l=mid+1;
}
return ans;
}
inline int Upper_Bound(int tn,int lim){
int l=-INF,r=INF-1,ans=INF;
while(l<=r){
int mid=(l+r)>>1;
if(Get(tn,mid)>lim)
r=(ans=mid)-1;
else
l=mid+1;
}
return ans;
}
//}}}
//main{{{
inline void Solve(){
tl=0;
ReadS(n,a);
for(int i=1;i<=n;++i)
if(a[i])
++tl,mn[0][tl]=i-1-2*tl;
ST();
q=Read();
for(int i=1;i<=q;++i){
int tn=Read(),l=Read(),r=Read();
printf("%lld\n",tl?Upper_Bound(tn,r)-Lower_Bound(tn,l):0);
}
}
signed main(){
#ifdef FL
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
int T=Read()+1;
while(--T)
Solve();
return 0;
}
//}}}
//《象》曰:泽灭木,大过。君子以独立不惧,遁世无闷。
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5868kb
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: 479ms
memory: 11756kb
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: 1517ms
memory: 18792kb
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: 1969ms
memory: 17260kb
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