fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define pb push_back
  5. #define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
  6. #define afor(i,a,n) for(int i=a; i<n; i++)
  7. #define dfor(i,a,n) for(int i=n-1; i>= a; i--)
  8. #define all(v) v.begin(),v.end()
  9.  
  10. int digsum(int x){
  11. int sum=0;
  12. while(x){
  13. sum+=(x)%10;
  14. x/=10;
  15. }
  16. return sum;
  17. }
  18.  
  19. void solve(){
  20. int n,q;
  21. cin>>n>>q;
  22. int a[n]={0},a1[n]={0},a2[n]={0},a3[n]={0},times[n]={0};
  23. set<int> st;
  24. afor(i,0,n) {
  25. cin>>a[i];
  26. st.insert(i+1);
  27. }
  28. afor(i,0,n){
  29. a1[i]=digsum(a[i]);
  30. }
  31. afor(i,0,n){
  32. a2[i]=digsum(a1[i]);
  33. }
  34. afor(i,0,n){
  35. a3[i]=digsum(a2[i]);
  36. }
  37. int cnt=0;
  38. while(q--){
  39. int type=0;
  40. cin>>type;
  41. if (type==1){
  42. int l,r;
  43. cin>>l>>r;
  44. //l--,r--;
  45. auto itr = st.lower_bound(l);
  46. for(auto it=itr;it!=st.end();it++){
  47. //cout<<(*it)<<" "<<times[(*it)]<<";";
  48. if ((*it)>r) break;
  49. if (times[(*it)-1]<3) times[(*it)-1]++;
  50. if (times[(*it)-1]==3) st.erase(it);
  51. }
  52. //afor(i,0,n) cout<<times[i]<<" ";
  53. }
  54. else{
  55. int x;
  56. cin>>x;
  57. if (times[x-1]==0) cout<<a[x-1]<<"\n";
  58. else if (times[x-1]==1) cout<<a1[x-1]<<"\n";
  59. else if (times[x-1]==2) cout<<a2[x-1]<<"\n";
  60. else cout<<a3[x-1]<<"\n";
  61. }
  62. //afor(i,0,n) cout<<times[i]<<" "; cout<<"\n";
  63. }
  64. }
  65.  
  66. int32_t main(){
  67. int t;
  68. cin>>t;
  69. while(t--){
  70. solve();
  71. }
  72.  
  73. return 0;
  74. }
Runtime error #stdin #stdout 0.01s 5512KB
stdin
2
9 8
1 5 4 7 9 3 4 8 8 
2 2
2 5
1 5 7
1 4 5
1 7 7
2 5
1 5 8
1 2 9
6 2
6 7 7 6 9 9 
2 2
1 1 5
stdout
5
9
9