QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#179530 | #6106. Making Number | Zhou_JK | WA | 71ms | 20480kb | C++23 | 4.8kb | 2023-09-14 22:02:41 | 2023-09-14 22:02:42 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<array>
using namespace std;
const int N=100005;
int n,q;
int x[N],y[N];
string sx,sy;
int cntx[10],cntr[10];
int s[N][10];
struct Segment_Tree
{
#define ls i*2
#define rs i*2+1
struct Node
{
int l,r;
int len;
array<int,10>cnt;
friend Node operator+(const Node &a,const Node &b)
{
Node c;
c.l=a.l,c.r=b.r;
if(b.len==b.r-b.l+1) c.len=b.len+a.len;
else c.len=b.len;
for(int j=0;j<=9;j++)
c.cnt[j]=a.cnt[j]+b.cnt[j];
return c;
}
}tree[N<<2];
void push_up(int i)
{
tree[i]=tree[ls]+tree[rs];
return;
}
void build(int i,int l,int r)
{
tree[i].l=l,tree[i].r=r;
if(l==r)
{
if(y[l-1]>=y[l]) tree[i].len=1;
else tree[i].len=0;
for(int j=0;j<=9;j++)
tree[i].cnt[j]=0;
tree[i].cnt[y[l]]++;
return;
}
int mid=(l+r)/2;
build(ls,l,mid);
build(rs,mid+1,r);
push_up(i);
return;
}
int find_kth(int i,int c,int k)
{
if(k>tree[i].cnt[c]) return n+1;
if(tree[i].l==tree[i].r) return tree[i].l;
if(k<=tree[ls].cnt[c]) return find_kth(ls,c,k);
else return find_kth(rs,c,k-tree[ls].cnt[c]);
}
void modify(int i,int u,int v)
{
if(tree[i].l==tree[i].r)
{
if(y[u-1]>=v) tree[i].len=1;
else tree[i].len=0;
tree[i].cnt[y[u]]--;
tree[i].cnt[v]++;
y[u]=v;
return;
}
if(u<=tree[ls].r) modify(ls,u,v);
else modify(rs,u,v);
push_up(i);
return;
}
Node query(int i,int l,int r)
{
if(l<=tree[i].l&&tree[i].r<=r) return tree[i];
if(r<=tree[ls].r) return query(ls,l,r);
else if(l>=tree[rs].l) return query(rs,l,r);
else return query(ls,l,r)+query(rs,l,r);
}
#undef ls
#undef rs
}T;
int pos,t;
void calc()
{
pos=n;
for(int j=0;j<=9;j++)
pos=min(pos,T.find_kth(1,j,cntx[j]+1)-1);
array<int,10> len{};
if(1<=pos) len=T.query(1,1,pos).cnt;
t=-1;
if(pos==n) t=n+1;
else
{
for(int j=y[pos+1]+1;j<=9;j++)
if(cntx[j]-len[j]>0)
{
t=pos+1;
// cerr<<"find"<<j<<"\n";
break;
}
if(t==-1) t=pos-T.query(1,1,pos+1).len;
}
// cerr<<"done"<<pos<<" "<<t<<"\n";
array<int,10>sum{};
if(1<=t-1) sum=T.query(1,1,t-1).cnt;
for(int j=0;j<=9;j++)
cntr[j]=cntx[j]-sum[j];
for(int j=0;j<=9;j++)
if(cntr[j]<0) exit(1);
// for(int j=0;j<=9;j++)
// cerr<<cntr[j]<<" ";cerr<<"\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
// cin.tie(nullptr),cout.tie(nullptr);
cin>>sx>>sy;
n=sx.size();
for(int i=1;i<=n;i++)
x[i]=sx[i-1]-'0',y[i]=sy[i-1]-'0';
for(int i=1;i<=n;i++)
cntx[x[i]]++;
T.build(1,1,n);
calc();
cin>>q;
while(q--)
{
int op,i,x;
cin>>op;
if(op==1)
{
cin>>i>>x;
T.modify(1,i,x);
calc();
}
else
{
cin>>i;
if(i<t)
{
cout<<y[i]<<"\n";
continue;
}
if(t==0)
{
cerr<<"error1\n";
cout<<-1<<"\n";
continue;
}
int v=-1;
for(int j=y[t]+1;j<=9;j++)
if(cntr[j]>0)
{
v=j;
break;
}
cerr<<pos<<" "<<t<<" "<<y[t]<<" val "<<v<<'\n';
if(v==-1)
{
// cout<<n<<","<<pos<<","<<t<<"\n";
cout<<-1<<"\n";
continue;
}
if(i==t)
{
cout<<v<<"\n";
continue;
}
int sum=0;
bool flag=false;
for(int j=0;j<=9;j++)
{
sum+=cntr[j];
if(j==v) sum--;
if(sum>=i-t)
{
flag=true;
cout<<j<<"\n";
break;
}
}
if(!flag)
{
exit(1);
}
}
// cerr<<"Y:";
// for(int i=1;i<=n;i++)
// cerr<<y[i];cerr<<"\n";
}
return 0;
}
/*
2950 9052
4
2 1
2 2
2 3
2 4
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3464kb
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: 1ms
memory: 5632kb
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: 0ms
memory: 3472kb
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: 71ms
memory: 20480kb
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 4 9 9 9 0 4 6 8 1 9 6 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 1 8 0 ...
result:
wrong answer 41339th numbers differ - expected: '3', found: '-1'