主要思路:存好需要改变的下标然后用函数找到下表,然后改变。参考牛客寒假训练营1中鸡格线。
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
typedef long long ll;
using namespace std;
ll a[200005];
ll sum=0;
ll change(ll x){
long long tmp=0;
while(x>0){
tmp+=x%10;
x/=10;
}
return tmp;
}
void solve(){
set<ll>st;
ll n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]!=change(a[i])){
st.insert(i);
}
}
st.insert(1e9+1);//防止全部都已经不需要更新后的死循环。
while(m--){
ll op;
cin>>op;
if(op==2) {
ll hh;
cin>>hh;
cout<<a[hh]<<endl;
}
else {
ll l,r;
cin>>l>>r;
ll tmp=l;
while(1){
ll nex=*(st.lower_bound(tmp));//找到第一个小于等于tmp的下标.
if(nex>r) break;
a[nex]=change(a[nex]);
if(a[nex]==change(a[nex])){
st.erase(nex);
}
tmp=nex+1;
}
}
}
}
int main(){
ll t;
cin>>t;
while(t--){
solve();
}
}