QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#162777 | #7106. Infinite Parenthesis Sequence | ucup-team1209# | WA | 0ms | 11688kb | C++20 | 2.9kb | 2023-09-03 16:35:39 | 2023-09-03 16:35:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,y,x) for (int i=(y);i>=(x);i--)
#define pii pair<int,ll>
#define fir first
#define sec second
#define MP make_pair
#define templ template<typename T>
templ bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ bool chkmax(T &x,T y){return x<y?x=y,1:0;}
void file() {
#ifdef zqj
freopen("a.in","r",stdin);
#endif
}
typedef long long ll;
#define sz 201010
int n;
char s[sz];
int a[sz];
int pos[sz],m;
int Q;
ll K[sz],L[sz],R[sz];
int rev;
int fa[sz];
pii jmp[sz][21];
ll qpos(ll id,ll K) {
ll delta=id/m*n; id%=m;
if (id<0) id+=m,delta-=n;
int x=pos[id]; ++K;
ll prev=x;
drep(i,19,0) if (jmp[x][i].sec<K) K-=jmp[x][i].sec,prev+=jmp[x][i].sec-(1<<i),x=jmp[x][i].fir;
return prev+K-1+delta;
}
// ll work(ll l,ll r,ll L,ll R,ll K) {
// if (l>r) return 0;
// ll mid=(l+r)/2;
// ll pos=qpos(mid,K);
// if (pos<L) return work(mid+1,r,L,R,K);
// if (pos>R) return work(l,mid-1,L,R,K);
// return 1+work(mid+1,r,L,R,K)+work(l,mid-1,L,R,K);
// }
void work() {
cin>>s; n=strlen(s);
cin>>Q;
rep(i,1,Q) cin>>K[i]>>L[i]>>R[i];
int cc=0;
rep(i,0,n-1) a[i]=(s[i]=='('),cc+=a[i];
if (cc==0||cc==n) {
rep(i,1,Q) {
cout<<(cc==n?R[i]-L[i]+1:0)<<'\n';
}
return;
}
if (cc*2>n) {
reverse(a,a+n);
rep(i,0,n-1) a[i]^=1;
rep(i,1,Q) swap(L[i],R[i]),L[i]=n-1-L[i],R[i]=n-1-R[i];
rev=1;
// CHECK
} else rev=0;
int mx=0,c=0,p=n;
drep(i,n-1,1) {
c+=(a[i]==1?1:-1);
if (chkmax(mx,c)) p=i;
}
rep(i,1,Q) L[i]+=n-p,R[i]+=n-p;
rotate(a,a+p,a+n); // CHECK
m=0;
rep(i,0,n-1) if (a[i]) pos[m++]=i;
static int sum[sz];
sum[0]=(a[0]?1:-1);
rep(i,1,2*n-1) sum[i]=sum[i-1]+(a[i%n]?1:-1);
stack<int>st; st.push(n*2); sum[n*2]=1e9;
drep(i,2*n-1,0) if (a[i]) {
while (sum[st.top()]<=sum[i]) st.pop();
if (i<n) {
fa[i]=st.top();
jmp[i][0]={fa[i]==n*2?n:fa[i]%n,(fa[i]==n*2?1e12:(fa[i]-i-1)/2)+1};
}
st.push(i);
}
jmp[n][0]={n,0};
rep(j,1,19) rep(i,0,n) jmp[i][j]={jmp[jmp[i][j-1].fir][j-1].fir,jmp[i][j-1].sec+jmp[jmp[i][j-1].fir][j-1].sec};
rep(i,1,Q) {
ll k=K[i],l=L[i],r=R[i];
// ll ans=work(-1e12/m*n,1e12/m*n,l,r,k);
auto lw=[](ll p,ll k) {
ll l=-1e12/m*n,r=1e12/m*n,mid,res=-1;
while (l<=r) {
mid=(l+r)/2;
if (qpos(mid,k)>=p) res=mid,r=mid-1;
else l=mid+1;
}
return res;
};
ll ans=lw(r+1,k)-lw(l,k);
if (rev) ans=r-l+1-ans;
cout<<ans<<'\n';
}
}
int main() {
file();
ios::sync_with_stdio(false),cin.tie(0);
int T; cin>>T;
while (T--) work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 11688kb
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 46 3
result:
wrong answer 9th lines differ - expected: '45', found: '46'