QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#245121#7106. Infinite Parenthesis SequenceEdwin_VanCleefTL 3ms36508kbC++232.5kb2023-11-09 19:04:192023-11-09 19:04:20

Judging History

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

  • [2023-11-09 19:04:20]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:36508kb
  • [2023-11-09 19:04:19]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace my_IO{
    #define ll long long
    #define ull unsigned long long
    #define ld long double
    #define spc putchar(' ')
    #define et putchar('\n')
    template<class T>
    void read(T &num){
        T x=0,f=1;
        char c=getchar();
        while(!isdigit(c)){
            if(c=='-') f=-1;
            c=getchar();
        }
        while(isdigit(c)){
            x=(x<<3)+(x<<1)+c-48;
            c=getchar();
        }
        num=f*x;
    }
    template<class T>
    void write(T x){
        static char buf[40];
        static int len=-1;
        if(x<0) putchar('-'),x=-x;
        do{
            buf[++len]=x%10+48;
            x/=10;
        }while(x);
        while(len>=0) putchar(buf[len--]);
    }
}
using namespace my_IO;
const ll inf=1e9;
const ll maxn=1e5+10;
ll n,m,F[maxn<<1][20],lg[maxn<<1];
char s[maxn];
ll query(ll l,ll r){
    if(l<0||r>m*2-1){
        cout<<"fuck\n";
        exit(0);
    }
    // cout<<l<<" "<<r<<"\n";
    ll k=lg[r-l+1];
    return min(F[l][k],F[r-(1<<k)+1][k]);
}
ll que(ll l,ll r){
    // cout<<l<<" "<<r<<"\n";
    ll k=l/m;
    ll nl=l%m,nr=nl+r-l,res=(n-2*m)*k;
    if(nl<0) nl+=m,nr+=m,res-=n-2*m;
    // cout<<nl<<" "<<nr<<" "<<res<<"\n";
    return query(nl,nr)+res;
}
ll f(ll k,ll i){
    // cout<<k<<" "<<i<<":\n";
    if(k<=m) return que(i,i+k)+2*i+k;
    if(n>=2*m) return que(i,i+m-1)+2*i+k;
    return que(i+k-m+1,i+k)+2*i+k;
}
ll g(ll k,ll p){
    // cout<<k<<" "<<p<<"\n";
    ll l=-inf,r=inf,res=0;
    while(l<=r){
        ll mid=(l+r)>>1;
        // cout<<mid<<":\n";
        if(f(k,mid)<=p){
            // cout<<"yes\n";
            res=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    // cout<<res<<"\n";
    return res;
}
void solve(){
    memset(F,0x3f,sizeof(F));
    scanf("%s",s);
    n=strlen(s);
    ll cnt=-1;
    for(ll i=0;i<n;i++){
        if(s[i]=='('){
            cnt++;
            F[cnt][0]=i-2*cnt;
        }
    }
    m=cnt+1;
    for(ll i=0;i<m;i++) F[i+m][0]=F[i][0]+n-2*m;
    for(ll j=1;j<=19;j++) for(ll i=0;i+(1<<j)-1<=2*m-1;i++) F[i][j]=min(F[i][j-1],F[i+(1<<(j-1))][j-1]);
    ll q;
    read(q);
    while(q--){
        ll l,r,k;
        read(k),read(l),read(r);
        if(!m) puts("0");
        else write(g(k,r)-g(k,l-1)),et;
    }
}
int main(){
    for(int i=2;i<maxn<<1;i++) lg[i]=lg[i>>1]+1;
    int t=1;
    read(t);
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 36508kb

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
Time Limit Exceeded

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: