QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#311734#6106. Making NumberlinlexiaoWA 58ms25592kbC++204.6kb2024-01-22 18:21:052024-01-22 18:21:05

Judging History

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

  • [2024-01-22 18:21:05]
  • 评测
  • 测评结果:WA
  • 用时:58ms
  • 内存:25592kb
  • [2024-01-22 18:21:05]
  • 提交

answer

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

using ll=long long;
inline int read(){int f=1,x=0;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
inline ll readll(){ll f=1,x=0;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
#define all(x) x.begin(),x.end()

const int N = 1e5+5;
int cnt[10];
char X[N],Y[N];


int n;
struct Node{
    int s[10];
    int minv;
    Node(){memset(s,0,sizeof(s));minv=1e9;}
}t[N*4];
Node operator+(const Node& l,const Node& r){
    Node ret;
    for(int i=0;i<10;i++)ret.s[i]=l.s[i]+r.s[i];
    ret.minv=min(l.minv,r.minv);
    return ret;
}

Node s[N];
void build(int o,int l,int r){
    if(l==r){
        if(1<=l&&l<=n)t[o].s[Y[l]]=1;
        
        if(l<n)t[o].minv=Y[l+1];
        else t[o].minv=1e9;
        
        return;
    }
    int lch=o<<1,rch=o<<1|1,mid=(l+r)>>1;
    build(lch,l,mid),build(rch,mid+1,r);
    t[o]=t[lch]+t[rch];
}
void modify(int o,int l,int r,int qi){
    if(l==r){
        memset(t[o].s,0,sizeof(t[o].s));
        t[o].s[Y[l]]++;
        if(l<n)t[o].minv=Y[l+1];
        else t[o].minv=1e9;
        return;
    }
    int lch=o<<1,rch=o<<1|1,mid=(l+r)>>1;
    if(qi<=mid)modify(lch,l,mid,qi);
    else modify(rch,mid+1,r,qi);
    t[o]=t[lch]+t[rch];
}

Node qsum(int o,int l,int r,int ql,int qr){
    if(ql>qr)return Node();
    if(ql<=l&&r<=qr)return t[o];
    
    int lch=o<<1,rch=o<<1|1;
    int mid=(l+r)>>1;
    if(qr<=mid)return qsum(lch,l,mid,ql,qr);
    if(ql>mid)return qsum(rch,mid+1,r,ql,qr);
    return qsum(lch,l,mid,ql,qr)+qsum(rch,mid+1,r,ql,qr);
}

int query(int o,int l,int r){
    if(l==r)return l;
    int lch=o<<1,rch=o<<1|1,mid=(l+r)>>1;

    bool ok = 1;
    for(int i=0;i<10;i++){
        if(cnt[i]<t[lch].s[i]){ok=0;break;}
    }
    
    if(!ok)return query(lch,l,mid);
    
    for(int i=0;i<10;i++)cnt[i]-=t[lch].s[i];

    int ans = query(rch,mid+1,r);
    for(int i=0;i<10;i++)cnt[i]+=t[lch].s[i];
    return ans;
}

bool check(const Node& d,int minval){
    for(int i=minval+1;i<10;i++)if(d.s[i])return 1;
    return 0;
}

int min_left(int o,int l,int r,int qr,Node& d){
    if(l>qr)return 0;
    if(qr>=r){
        auto dd = d+t[o];
        if(!check(dd,dd.minv) || Y[l]==9){
            d=dd;
            return 0;
        }
        if(l==r)return r;
    }

    int lch=o<<1,rch=o<<1|1,mid=(l+r)>>1;

    int pos = min_left(rch,mid+1,r,qr,d);
    if(pos)return pos;
    return min_left(lch,l,mid,qr,d);
}

Node getf1r(int r){
    Node n2;
    auto minus = qsum(1,1,n,1,r);
    for(int i=0;i<10;i++)n2.s[i]=s[n].s[i]-minus.s[i];
    return n2;
}

int main(){
    scanf("%s%s",X+1,Y+1);
    n=strlen(X+1);
    for(int i=1;i<=n;i++)X[i]-='0',Y[i]-='0';


    for(int i=1;i<=n;i++){
        cnt[X[i]]++;
        s[i]=s[i-1];
        s[i].s[X[i]]++;
    }

    build(1,1,n);

    int q=read();
    while(q--){
        int o=read();
        if(o==1){
            int i=read(),x=read();
            Y[i]=x;
            modify(1,1,n,i);
            if(i)modify(1,1,n,i-1);
        }
        else{
            int pos = read();

            bool same = 1;
            for(int i=0;i<10;i++){
                if(t[1].s[i]!=cnt[i]){
                    same=0;break;
                }
            }
            if(same){
                printf("%d\n",(int)Y[pos]);
                continue;
            }
            
            int p = query(1,1,n);
            
            
            auto nd = getf1r(p-1);
            int p2 = min_left(1,1,n,p-1,nd);

            auto n2 = getf1r(p2);
            if(!check(n2,Y[p2+1])){
                // assert(!p2);
                puts("-1");
                continue;
            }

            if(pos<=p2){
                printf("%d\n",(int)Y[pos]);
                continue;
            }

            if(pos==p2+1){
                for(int i=Y[p2+1]+1;i<10;i++){
                    if(n2.s[i]){
                        printf("%d\n",i);
                        break;
                    }
                }
                continue;
            }

            for(int i=Y[p2+1]+1;i<10;i++){
                if(n2.s[i]){
                    n2.s[i]--;break;
                }
            }

            int L = p2+2;
            for(int i=0;i<10;i++){
                int R = L+n2.s[i]-1;
                if(L<=pos&&pos<=R){
                    printf("%d\n",i);
                }
                L += n2.s[i];
            }
        }
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3304 1615
6
2 3
2 4
1 1 3
2 2
1 2 4
2 1

output:

3
4
0
3

result:

ok 4 number(s): "3 4 0 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 25324kb

input:

838046 780357
10
2 1
2 2
1 2 4
2 3
2 4
1 4 5
2 5
2 6
1 1 9
2 2

output:

8
0
3
4
6
8
-1

result:

ok 7 numbers

Test #3:

score: 0
Accepted
time: 3ms
memory: 25352kb

input:

2950 9052
4
2 1
2 2
2 3
2 4

output:

9
0
5
2

result:

ok 4 number(s): "9 0 5 2"

Test #4:

score: -100
Wrong Answer
time: 58ms
memory: 25592kb

input:

677626993975892499704039030543272430899275243910257911804799779050091046516452988178013531014268918811173939650642586439566406179191340032290242471164620397693718115937469230865639050202204033086180013798392250945514997389392089836361472892923108709052296874567465127993121832785182254870655135620662...

output:

7
9
3
6
0
4
7
1
1
4
1
5
4
5
2
1
3
8
3
8
4
9
5
8
1
5
6
1
3
9
4
7
2
2
4
1
2
0
1
8
3
7
0
8
5
9
9
4
9
4
4
4
7
1
5
6
3
0
3
7
4
6
2
4
9
6
4
8
2
0
4
4
2
0
6
4
8
-1
-1
-1
-1
-1
4
6
8
1
9
-1
5
8
3
1
5
4
8
8
7
7
4
0
0
6
2
0
3
9
8
6
8
2
9
2
7
6
1
4
3
8
9
6
4
8
3
5
4
7
5
7
4
1
7
5
3
9
0
9
8
9
1
5
6
8
9
8
5
5
0
...

result:

wrong answer 78th numbers differ - expected: '4', found: '-1'