QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#162925#7106. Infinite Parenthesis Sequenceucup-team1209#WA 951ms26196kbC++204.1kb2023-09-03 17:48:392023-09-03 17:48:40

Judging History

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

  • [2023-09-03 17:48:40]
  • 评测
  • 测评结果:WA
  • 用时:951ms
  • 内存:26196kb
  • [2023-09-03 17:48:39]
  • 提交

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,iid[sz];
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=id; ++K;
//     ll prev=pos[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;
// }

vector<int>V[sz];
ll up[sz],dep[sz],w[sz];
int fa[sz],siz[sz],dfn[sz],top[sz],son[sz],cc;
void dfs1(int x,int f) {
    siz[x]=1; son[x]=-1;
    for (auto v:V[x]) {
        dfs1(v,x),siz[x]+=siz[v];
        if (son[x]==-1||siz[v]>siz[son[x]]) son[x]=v;
    }
}
void dfs2(int x,int f,int tp) {
    top[x]=tp, fa[x]=f;
    dfn[x]=++cc,
    dep[x]=dep[f]+up[x],w[cc]=dep[x];
    if (son[x]!=-1) dfs2(son[x],x,tp);
    for (auto v:V[x]) if (v!=son[x]) dfs2(v,x,v);
} 

ll qpos(ll id,ll K) {
    ll delta=id/m*n; id%=m;
    if (id<0) id+=m,delta-=n;
    ++K;
    int x=id; ll ed=dep[x]-K;
    int cc=0;
    while (233) {
        int tp=top[x];
        if (tp==m||dep[fa[tp]]<=ed) break;
        cc+=dfn[x]-dfn[tp]+1;
        x=fa[tp];
    }
    int p=upper_bound(w+dfn[top[x]],w+dfn[x]+1,ed)-w;
    cc+=dfn[x]-p;
    return delta+pos[id]+K-1-cc;
}

// 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]) iid[i]=m,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%n]) {
        while (sum[st.top()]<=sum[i]) st.pop();
        if (i<n) {
            int f=st.top();
            // jmp[iid[i]][0]={f==n*2?m:iid[f%n],(f==n*2?1e12:(f-i-1)/2)+1};
            V[f==n*2?m:iid[f%n]].push_back(iid[i]);
            up[iid[i]]=(f==n*2?1e12:(f-i-1)/2)+1;
        }
        st.push(i);
    }
    dfs1(m,m);dfs2(m,m,m);
    rep(i,1,Q) {
        ll k=K[i],l=L[i],r=R[i],_r=r;
        ll ans=(r-l+1)/n*m;  r-=(r-l+1)/n*n;
        // ll ans=work(-1e12/m*n,1e12/m*n,l,r,k);
        auto lw=[](ll p,ll k,ll begin) {
            ll l=begin,r=begin+m*4,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 p=qpos(0,k);
        ll beg=(l-p-n)/n*m;
        // assert(qpos(beg,k)<l&&qpos(beg+4*m,k)>r);
        ans+=lw(r+1,k,beg)-lw(l,k,beg);
        if (rev) ans=_r-l+1-ans;
        cout<<ans<<'\n';
    }
    rep(i,0,m+1) dfn[i]=w[i]=dep[i]=up[i]=son[i]=siz[i]=top[i]=fa[i]=cc=0,V[i].clear();
}

int main() {
    file();
    ios::sync_with_stdio(false),cin.tie(0);
    int T; cin>>T;
    while (T--) work();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 20260kb

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: 167ms
memory: 22348kb

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: -100
Wrong Answer
time: 951ms
memory: 26196kb

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:

wrong answer 400026th lines differ - expected: '18951030', found: '18951015'