Advertisement
a53

StringQuery

a53
May 15th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #define Nmax 5000001
  3. using namespace std;
  4. int F[26];
  5. int n,poz,x,y;
  6. char c,arb[Nmax];
  7.  
  8. void creare(int nod,int st,int dr)
  9. {
  10. if(st==dr)
  11. cin>>arb[st];
  12. else
  13. {
  14. int mij=(st+dr)/2;
  15. creare(2*nod,st,mij);
  16. creare(2*nod+1,mij+1,dr);
  17. }
  18. }
  19.  
  20. void citire()
  21. {
  22. cin>>n;
  23. creare(1,1,n);
  24. }
  25.  
  26. void adauga(int nod,int st,int dr)
  27. {
  28. if(st==dr)
  29. arb[nod]=c;
  30. else
  31. {
  32. int mij=(st+dr)/2;
  33. if(poz<=mij)
  34. adauga(2*nod,st,mij);
  35. else
  36. adauga(2*nod+1,mij+1,dr);
  37. }
  38. }
  39.  
  40. void calcul(int nod,int st,int dr)
  41. {
  42. if(x<=st&&dr<=y)
  43. ++F[arb[st]-'a'],++F[arb[dr]-'a'];
  44. else
  45. {
  46. int mij=(st+dr)/2;
  47. if(x<=mij)
  48. calcul(2*nod,st,mij);
  49. if(mij+1<=y)
  50. calcul(2*nod+1,mij+1,dr);
  51. }
  52. }
  53.  
  54. void rez()
  55. {
  56. int q,op;
  57. cin>>q;
  58. for(int i=1;i<=q;i++)
  59. {
  60. cin>>op;
  61. if(op==1) /// Elementul de pe pozitia poz se inlocuieste cu c
  62. {
  63. cin>>poz>>c;
  64. arb[poz]=c;
  65. adauga(1,1,n);
  66. }
  67. else /// se va afisa numarul de caractere distincte ale lui s din intervalul [a, b].
  68. {
  69. cin>>x>>y;
  70. for(int p=0;p<26;++p)
  71. F[i]=0;
  72. ++F[arb[x]-'a'];
  73. calcul(1,1,n);
  74. int ncd=0;
  75. for(int p=0;p<26;++p)
  76. if(F[p])
  77. ++ncd;
  78. cout<<ncd<<'\n';
  79. }
  80. }
  81. }
  82.  
  83. int main()
  84. {
  85. citire();
  86. rez();
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement