Advertisement
a53

StringQuery_CF

a53
May 16th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAX_N=(int)1e7+777; /// limit for array size
  4. int n; /// array size
  5. int TREE[2*MAX_N];
  6. string str;
  7.  
  8. void build() /// build the tree
  9. {
  10. for(int i=n-1;i>=1;--i)
  11. TREE[i]=TREE[i<<1]|TREE[(i<<1)|1];
  12. }
  13.  
  14. void modify(char chr,int p) /// set value at position p
  15. {
  16. --p,p+=n;
  17. int code=(chr-'a');
  18. TREE[p]=(1<<code);
  19. for(;p>0;p>>=1)
  20. TREE[p>>1]=TREE[p]|TREE[p^1];
  21. }
  22.  
  23. int func(int l,int r){
  24. int ans=0;
  25. --l,--r,l+=n,r+=n,++r;
  26. while(l<r)
  27. {
  28. if(l&1)
  29. ans|=TREE[l++];
  30. if(r&1)
  31. ans|=TREE[--r];
  32. l>>=1,r>>=1;
  33. }
  34. return __builtin_popcount(ans);
  35. }
  36.  
  37. int main()
  38. {
  39. ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  40. cin>>n>>str;
  41. for(int i=0;i<n;++i)
  42. TREE[i+n]=(1<<(str[i]-'a'));
  43. build();
  44. int q,a,l,r;
  45. char chr;
  46. cin>>q;
  47. while(q--)
  48. {
  49. cin>>a>>l;
  50. if(a==1)
  51. cin>>chr,modify(chr,l);
  52. else
  53. cin>>r,cout<<func(l,r)<<'\n';
  54. }
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement