QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#425838 | #6106. Making Number | HKOI0# | WA | 42ms | 7864kb | C++20 | 4.2kb | 2024-05-30 17:28:04 | 2024-05-30 17:28:07 |
Judging History
answer
#include <bits/stdc++.h>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
struct BIT {
int n;
vi a;
BIT(int n) : n(n), a(n+2) {}
void add(int i, int x) {
for(;i<=n;i+=i&-i) a[i]+=x;
}
int sum(int i) {
int ans=0;
for(;i;i-=i&-i) ans+=a[i];
return ans;
}
};
void solve() {
string s1,s2;
cin>>s1>>s2;
int n=sz(s1);
vector<int> sum(10);
for(auto c:s1) sum[c-'0']++;
vector bit(10,BIT(n));
BIT bit2(n-1);
for(int i=1;i<=n;i++) bit[s2[i-1]-'0'].add(i,1);
for(int i=1;i<n;i++) bit2.add(i,s2[i-1]<s2[i]);
int q;
cin>>q;
int mx=0;
while(1<<(mx+1)<=n) mx++;
while(q--) {
int t;
cin>>t;
if(t==1) {
int i,x;
cin>>i>>x;
bit[s2[i-1]-'0'].add(i,-1);
if(i>1) bit2.add(i-1,-(s2[i-2]<s2[i-1]));
if(i<n) bit2.add(i,-(s2[i-1]<s2[i]));
s2[i-1]=(char)(x+'0');
bit[s2[i-1]-'0'].add(i,1);
if(i>1) bit2.add(i-1,(s2[i-2]<s2[i-1]));
if(i<n) bit2.add(i,(s2[i-1]<s2[i]));
}
else {
int i;
cin>>i;
int ans=0;
vector<int> prev(10);
for(int j=mx;j>=0;j--) {
if(ans+(1<<j)>n) continue;
auto cur=prev;
for(int k=0;k<10;k++) cur[k]+=bit[k].a[ans+(1<<j)];
auto ok = [&] {
for(int k=0;k<10;k++)
if(sum[k]<cur[k]) return false;
return true;
// if(ans+(1<<j)==n) return true;
// int next_bit = s2[ans+(1<<j)]-'0';
// for(int k=next_bit+1;k<10;k++)
// if(sum[k]-cur[k]) return true;
// return false;
};
if(ok()) {
ans+=1<<j;
prev=cur;
}
}
if(ans<n) {
int next_bit=s2[ans]-'0';
auto diff=sum;
for(int j=0;j<10;j++) diff[j]-=prev[j];
bool ok=false;
for(int j=next_bit+1;j<10;j++)
if(diff[j]) {
ok=true;
break;
}
if(!ok) {
int l=1,r=ans-1;
while(l<r) {
int mid=(l+r+1)>>1;
if(bit2.sum(ans-1)-bit2.sum(mid-1)) l=mid;
else r=mid-1;
}
ans=l-1;
if(ans==0) {
int first_bit = s2[0]-'0';
bool ok2=false;
for(int k=first_bit+1;k<10;k++)
if(sum[k]) ok2=true;
if(!ok2) {
cout<<"-1\n";
continue;
}
}
}
}
if(i<=ans) cout<<s2[i-1]<<'\n';
else {
auto diff=sum;
for(int j=0;j<10;j++) diff[j]-=bit[j].sum(ans);
int remain=i-ans;
int next_bit=s2[ans]-'0';
int fi=0;
for(int j=next_bit+1;j<10;j++)
if(diff[j]) {
diff[j]--;
remain--;
fi=j;
break;
}
if(remain==0) {
cout<<fi<<'\n';
continue;
}
for(int j=0;j<10;j++) {
if(remain<=diff[j]) {
cout<<j<<'\n';
break;
}
else remain-=diff[j];
}
}
}
}
}
int main() {
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int T = 1;
// cin >> T;
while (T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3804kb
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: 3804kb
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: 1ms
memory: 3700kb
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: 42ms
memory: 7864kb
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 41356th numbers differ - expected: '4', found: '5'